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:
![import java.util.*; class MergeSorter public static void sort(int[] a) if (a.length <= 1) { return; } int[] first = new int[a](http://img.homeworklib.com/questions/e0568b40-0d1a-11ec-b87d-51466b1d87e5.png?x-oss-process=image/resize,w_560)
![if (first[iFirst] > second[isecond]), { a[j] = first[iFirst]; iFirst++; else a[j] = second[isecond]; isecond++; } j++; while](http://img.homeworklib.com/questions/e0ca8a40-0d1a-11ec-adaf-0b342169c89f.png?x-oss-process=image/resize,w_560)
Output:
![if (first[iFirst] > second[isecond]), 39 - a[j] = first[iFirst]; iFirst++; 43 else 44 a[j] = second[isecond]; isecond++; 48 4](http://img.homeworklib.com/questions/e138db70-0d1a-11ec-95f1-bde5366e09cd.png?x-oss-process=image/resize,w_560)
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...
Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and "import edu.princeton.cs.algs4.StdOut"imports: /* import java.util.Queue; import java.util.LinkedList; import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdOut; public class BottomUpMergeSort { public static void main(String[] args) { int N=Integer.parseInt(args[0]); Double [] a = new Double [N]; for(int i=0;i<N;i++) { a[i]=StdRandom.uniform(); } StdOut.println("Unsorted"); printArr(a); Queue que=sort(a); StdOut.println("\nSorted"); BottomUpMergeSort.printQue(que); } public static Queue sort(Double[] a) { int N=a.length; Queue->ques=new LinkedList>(); for(int i=0;i<N;i++) { ...
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++)...
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[]...
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...
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 =...
Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...
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....