0

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?

5
  • Is it a must ti use this very method ? Commented May 26, 2015 at 5:24
  • 1
    Why not sort the array normally, and then reverse it? Alternatively, implement it with a Comparator (and then you should be able swap from an ascending Comparator to a descending Comparator or the other way around). Commented May 26, 2015 at 5:26
  • There are algorithms for sorting. Use that, your task will be simplified and elegant way it is Commented May 26, 2015 at 5:31
  • @ElliottFrisch I don't get your comment... The only difference between a "normal" sort and a "reverse" sort is that you change the < to >, or vice versa, when comparing elements. If the algorithm for a reverse sort isn't right, the algorithm for a normal sort isn't going to be right either. Commented May 26, 2015 at 5:34
  • @ajb very well. But then, OP's underlying assumption about the sort implementation is suspect. Commented May 26, 2015 at 5:41

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Insertion sort is a perfectly good way to learn about arrays and loops, which was the OP's goal. If the goal were to sort in the most efficient way, the best solution would be to use the built-in Arrays.sort() method. (Unless I misunderstood what was meant by "coding challenge"...)
A coding challange means a competitive programming questions challange where you get to test your programming standards. It takes a lot more than knowledge of writing code to solve these type of questions.
Or, since it's for a course, it could just be an exercise to help one's learning. I think we'd have to ask his professor to find out. In any case, if he's still trying to learn about arrays and loops, then learning about heapsort may be just a tad too advanced at this point... :)
1

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.

1 Comment

That was exatly right ajb, It's always something simple... thank you so much. And thank you for wording it in a way that helps me understand the issue.
-1

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

1 Comment

this solution doesn't work because the parameter is an array of primitive type int instead of reference type Integer. If you change the method signature from selectionReverseSort(int[] array) to selectionReverseSort(Integer[] array), then this method will work. This method is just plain wrong at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.