Hey i got the program to work but i cant get the merge sorter from big to little
import java.util.*;
class MergeSorter
{
public static void sort(int[] a)
{
if (a.length <= 1) { return; }
int[] first = new int[a.length / 2];
int[] second = new int[a.length - first.length];
for (int i = 0; i < first.length; i++)
{
first[i] = a[i];
}
for (int i = 0; i < second.length; i++)
{
second[i] = a[first.length + i];
}
sort(first);
sort(second);
merge(first, second, a);
}
private static void merge(int[] first, int[] second,
int[] a)
{
int iFirst = 0;
int iSecond = 0;
int j = 0;
while (iFirst < first.length && iSecond <
second.length)
{
if (first[iFirst] < second[iSecond])
{
a[j] = first[iFirst];
iFirst++;
}
else
{
a[j] = second[iSecond];
iSecond++;
}
j++;
}
while (iFirst < first.length)
{
a[j] = first[iFirst];
iFirst++; j++;
}
while (iSecond < second.length)
{
a[j] = second[iSecond];
iSecond++; j++;
}
}
}
public class MergeSortDemo888888{
public static void main(String[] args) {
int [] myAry = {3,2,6,7};
System.out.println("myAry is " + Arrays.toString(myAry));
MergeSorter.sort(myAry);
System.out.println("myAry is sorted descendingly using selection
sort: "+Arrays.toString(myAry));
}
}
The code does not sort in descending order because of the logical error in the if condition below:
while (iFirst < first.length && iSecond <
second.length)
{
if (first[iFirst] < second[iSecond]) //This
if condition, sorts the elements from small to big. Hence, we have
to change the '<' to '>' so that it would sort the elements
from big to small.
{
a[j] = first[iFirst];
iFirst++;
}
else
{
a[j] = second[iSecond];
iSecond++;
}
Note: If you have trouble in copying the code, kindly check the code in the screenshots provided below
Correct Code:
import java.util.*;
class MergeSorter
{
public static void sort(int[] a)
{
if (a.length <= 1) { return; }
int[] first = new int[a.length / 2];
int[] second = new int[a.length - first.length];
for (int i = 0; i < first.length; i++)
{
first[i] = a[i];
}
for (int i = 0; i < second.length; i++)
{
second[i] = a[first.length + i];
}
sort(first);
sort(second);
merge(first, second, a);
}
private static void merge(int[] first, int[] second, int[]
a)
{
int iFirst = 0;
int iSecond = 0;
int j = 0;
while (iFirst < first.length && iSecond <
second.length)
{
if (first[iFirst] > second[iSecond]) //modified if condition
with '>'
{
a[j] = first[iFirst];
iFirst++;
}
else
{
a[j] = second[iSecond];
iSecond++;
}
j++;
}
while (iFirst < first.length)
{
a[j] = first[iFirst];
iFirst++; j++;
}
while (iSecond < second.length)
{
a[j] = second[iSecond];
iSecond++; j++;
}
}
}
public class MergeSortDemo888888{
public static void main(String[] args) {
int [] myAry = {3,2,6,7};
System.out.println("myAry is " + Arrays.toString(myAry));
MergeSorter.sort(myAry);
System.out.println("myAry is sorted descendingly using selection
sort: "+Arrays.toString(myAry));
}
}
Code Screenshot:
Output:
Hey i got the program to work but i cant get the merge sorter from big...
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...
Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...
Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++)...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....
use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...
For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*; public class GFG { // This is the temporary stack static Stack<Integer> res = new Stack<Integer>(); static Stack<Integer> tmpStack = new Stack<Integer>(); //...