Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Is it a must ti use this very method ? –  Shahzeb 14 hours ago
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). –  Elliott Frisch 14 hours ago
    
There are algorithms for sorting. Use that, your task will be simplified and elegant way it is –  Hurix 13 hours ago
    
@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. –  ajb 13 hours ago
    
@ajb very well. But then, OP's underlying assumption about the sort implementation is suspect. –  Elliott Frisch 13 hours ago

3 Answers 3

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.

share|improve this answer
2  
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"...) –  ajb 13 hours ago
    
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. –  Uma Lakshmi Kanth 13 hours ago
    
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... :) –  ajb 13 hours ago

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.

share|improve this answer
    
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. –  Stan Harris 6 hours ago

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.