Stan Harris
Stan Harris

Reputation: 15

Java: Reverse sorting array method

Preface: This isn't for homework, but it is for a "coding challenge", it's not worth any points in my course but i would like a fuller understanding of arrays and loops so i want this to work before i move on in the course.

So here's my problem. I'm trying to design a method that reverse sorts an array and places the numbers from highest to lowest. The code i have now is :

public static void selectionReverseSort (int[] array)
{
    int startScan, index, maxIndex, maxValue;
    for (startScan = 0; startScan < (array.length - 1); startScan++)
    {
        maxIndex = startScan;
        maxValue = array[startScan];

        for(index = startScan + 1; index < array.length; index++)
        //index is 1

        {
            if (array[index] > maxValue)
                {
                maxValue = array[index];
                //maxValue now becomes the second array number
                maxIndex = index;
                //maxIndex becomes the current index number.
                }


                array[maxIndex] = array[startScan];
                array[startScan] = maxValue;

        }
    }
}

The problem with this code, is that it seems to only reverse sort the arrays if they were in ascending order to start with, otherwise it just repeats the highest number for the first few array slots. Anyone wanna help me understand what's going on here and what i could do to fix this?

Upvotes: 0

Views: 264

Answers (3)

Avyaan
Avyaan

Reputation: 1313

This is just a one-line solution by using java API:

public static void selectionReverseSort (int[] array){
    Collections.sort(Arrays.asList(array),Collections.reverseOrder());
}

Keep it for future purpose :).

Upvotes: -1

ajb
ajb

Reputation: 31689

It appears that the algorithm you've chosen is to find the largest value in the array, then use a swap to move the largest value to the first position in the array. Then you do the same for the subarray starting with the second element, then with the subarray starting with the third, and so on.

This will work, but your code does the swap too early. You need to wait until your inner loop is done finding the largest element before you do the swap. I haven't tested it, but I think all you need to do is move these two statements:

array[maxIndex] = array[startScan];
array[startScan] = maxValue;

outside of the inner loop.

Upvotes: 1

Uma Kanth
Uma Kanth

Reputation: 5629

Your algorithm is correct. But you are swapping unnecessarily even if the you have a small number. I updated you logic.

import java.io.*;
public class Hey{
    public static void main(String args[])throws IOException{
        int []ar = {1,2,5,4,6,8,7,9,10};
        selectionReverseSort(ar);
    }
    public static void selectionReverseSort (int[] array){
        int startScan, index, maxIndex, maxValue;
        for (startScan = 0; startScan < (array.length - 1); startScan++){
            maxIndex = startScan;
            maxValue = array[startScan];

            for(index = startScan + 1; index < array.length; index++){
                //index is 1
                if (array[index] > maxValue){
                    maxValue = array[index];
                    //maxValue now becomes the second array number
                    maxIndex = index;
                    //maxIndex becomes the current index number.
                    //swap only if the condition is true
                    array[maxIndex] = array[startScan];
                    array[startScan] = maxValue;
                }
            }
        }
    }
    for(int i = 0; i < array.length; i++ )
        System.out.println(array[i]+"");
   }
}

And I suggest you to use any other better sorting algorithm than Insertion sort.

Upvotes: 1

Related Questions