java mergesort from the biggest to the smallest

Viewed 222

this class sort from the smallest to the biggest number, how to sort the opposite (from biggest to smallest).

I try to change some characters in divide() and merger() but this result in stop working of sort.

    public void divide(int startIndex, int endIndex) {
        // Divide till you breakdown your list to single element
        if (startIndex < endIndex && (endIndex - startIndex) >= 1) {
            int mid = (endIndex + startIndex) / 2;
            divide(startIndex, mid);
            divide(mid + 1, endIndex);        

            //merging Sorted array produce above into one sorted array
            merger(startIndex, mid, endIndex);            
        }       
    }   

    public void merger(int startIndex, int midIndex, int endIndex) {
        // Below is the merged array that will be sorted array Array[i - midIndex], Array[(midIndex + 1) - endIndex]
        ArrayList<Integer> mergedSortedArray = new ArrayList<Integer>();

        int leftIndex = startIndex;
        int rightIndex = midIndex + 1;

        while (leftIndex <= midIndex && rightIndex <= endIndex) {
            if (inputArray.get(leftIndex) <= inputArray.get(rightIndex)) {
                mergedSortedArray.add(inputArray.get(leftIndex));
                leftIndex++;
            } else {
                mergedSortedArray.add(inputArray.get(rightIndex));
                rightIndex++;
            }
        }       

        // Either of below while loop will execute
        while (leftIndex <= midIndex) {
            mergedSortedArray.add(inputArray.get(leftIndex));
            leftIndex++;
        }

        while (rightIndex <= endIndex) {
            mergedSortedArray.add(inputArray.get(rightIndex));
            rightIndex++;
        }

        int i = 0;
        int j = startIndex;
        // Setting sorted array to original one
        while (i < mergedSortedArray.size()) {
            inputArray.set(j, mergedSortedArray.get(i++));
            j++;
        }
    }
}
1 Answers

You need to change your "comparison operation" which defines the order of your result:

if(inputArray.get(leftIndex) <= inputArray.get(rightIndex)) {

Sorts items in ascending order. In order (pun intended) to get the reverse, change how the values compare; the opposite is:

if(inputArray.get(leftIndex) > inputArray.get(rightIndex)) {

If you want to make your algorithm more modular, you could change its interface to accept a Comparator<T> instance which is then used to compare the values, e.g.

public void merger(
        int startIndex,
        int midIndex,
        int endIndex,
        Comparator<Integer> comparator) {
    // ...
    if(comparator.compare(inputArray.get(leftIndex), inputArray.get(rightIndex)) < 0) {
    // ...
}

and then call e.g.:

merger(start, mid, end, Integer::compareTo); // or even:
merger(start, mid, end, Comparator.naturalOrder());

for ascending; or

Comparator<Integer> comparator = Integer::compareTo;
merger(start, mid, end, comparator.reversed());
// or as one-liner:
merger(start, mid, end, Collections.reverseOrder(Integer::compareTo)); // or even:
merger(start, mid, end, Comparator.reverseOrder());

to get the result in descending order.

Related