Question

Hey i got the program to work but i cant get the merge sorter from big...

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));
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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[aif (first[iFirst] > second[isecond]), { a[j] = first[iFirst]; iFirst++; else a[j] = second[isecond]; isecond++; } j++; while

Output:

if (first[iFirst] > second[isecond]), 39 - a[j] = first[iFirst]; iFirst++; 43 else 44 a[j] = second[isecond]; isecond++; 48 4

Add a comment
Know the answer?
Add Answer to:
Hey i got the program to work but i cant get the merge sorter from big...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    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...

    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 ti...

    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...

    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...

    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...

    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...

    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...

    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...

    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...

    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>();            //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT