简体   繁体   中英

MergeSort Algorithm from largest to smallest in java

So I want to use the mergesort algorithm to sort an array filled with numbers from largest to smallest. I have working code for this but I can't seem to make it sort from largest to smallest. I tried playing around with the for loop that has all of those if statements in there but I just couldn't figure it out. Could someone please help.

public class MergeSorter
{
    public void merge(int[] a, int l, int h) {
        if (h <= l) return;

        int result = (l + h) / 2;
        merge(a, l, result);
        merge(a, result + 1, h);
        sort_descend(a, l, result, h);
    }
    
    public void sort_descend(int[] a, int l, int result, int h) {
    
        int first_replace[] = new int[result - l + 1];
        int second_replace[] = new int[h - result];
        
        for (int i = 0; i < first_replace.length; i++)
            first_replace[i] = a[l + i];
        for (int i = 0; i < second_replace.length; i++)
            second_replace[i] = a[result+ i + 1];
        
        int first_i = 0;
        int second_i = 0;
            
        for (int i = l; i < h + 1; i++) {
            
            if (first_i < first_replace.length && second_i < second_replace.length) {
                if (first_replace[first_i] < second_replace[second_i]) {
                   a[i] = first_replace[first_i];
                   first_i++;
                } else {
                    a[i] = second_replace[second_i];
                    second_i++;
                }
            } else if (first_i < first_replace.length) {
                a[i] = first_replace[first_i];
                first_i++;
            } else if (second_i < second_replace.length) {
                a[i] = second_replace[second_i];
                second_i++;
            }
        }
    } 
}
import java.util.Arrays; 
public class MergeSortTest
{
    public static void main(String args[]) {
        int[] array = new int[]{ 6, 1, 3, 8, 3, 9, 2 };
        MergeSorter ms = new MergeSorter();
        ms.merge(array, 0, array.length - 1);
        System.out.println(Arrays.toString(array));
    }  
}

Your entire logic is correct except one thing. In the sort_descend function, after you copy the array a into first_replace and second_replace , you start comparing the elements using the if condition if (first_replace[first_i] < second_replace[second_i]) .

Here, you essentially assign the smaller of the two elements into your array a and this the step which determines whether your array will be sorted in ascending order or descending order.

To sort in descending order, you need to just reverse this sign and you will get the desired output ie change the if condition to if (first_replace[first_i] > second_replace[second_i]) .

Please refer to the below code to sort an array of integers in descending order. It is similar to your solution but only one change of the comparator operator on line number 38.

import java.util.Arrays;
public class C
{
    public static void main(String args[]) {
        int[] array = new int[]{ 6, 1, 3, 8, 3, 9, 2 };
        MergeSorter ms = new MergeSorter();
        ms.merge(array, 0, array.length - 1);
        System.out.println(Arrays.toString(array));
    }
}

class MergeSorter {
    public void merge(int[] a, int l, int h) {
        if (h <= l) return;

        int result = (l + h) / 2;
        merge(a, l, result);
        merge(a, result + 1, h);
        sort_descend(a, l, result, h);
    }

    public void sort_descend(int[] a, int l, int result, int h) {

        int first_replace[] = new int[result - l + 1];
        int second_replace[] = new int[h - result];

        for (int i = 0; i < first_replace.length; i++)
            first_replace[i] = a[l + i];
        for (int i = 0; i < second_replace.length; i++)
            second_replace[i] = a[result + i + 1];

        int first_i = 0;
        int second_i = 0;

        for (int i = l; i < h + 1; i++) {

            if (first_i < first_replace.length && second_i < second_replace.length) {
                if (first_replace[first_i] >= second_replace[second_i]) {
                    a[i] = first_replace[first_i];
                    first_i++;
                } else {
                    a[i] = second_replace[second_i];
                    second_i++;
                }
            } else if (first_i < first_replace.length) {
                a[i] = first_replace[first_i];
                first_i++;
            } else if (second_i < second_replace.length) {
                a[i] = second_replace[second_i];
                second_i++;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM