forum

Home / DeveloperSection / Forums / Java - Reverse Selection Sort

Java - Reverse Selection Sort

Anonymous User201905-Oct-2013

I am trying to create a program that does a selection sort but backwards. As in, finding the largest number and swapping it with the last. I have no idea why this isn't working.

UPDATED CODE according to comments.

  import java.util.Scanner;

public class Problem20 {

    public static void main(String[] args){
        int data[] = new int[10];
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter 10 numbers: ");
        for (int i = 0; i < 10; i++)
        {
        data[i] = scan.nextInt();
        }
        sortBig(data);
    }
    public static void sortBig(int[] data){
        int i, j, maxIndex, tmp;
          for (i = data.length - 1; i >= 0; i--)
          {
                maxIndex = i;
                for (j = i-1; j >=0; j--)
                    if (data[j] > data[maxIndex])
                      maxIndex = j;
                    if (maxIndex != i)
                    {
                      tmp = data[data.length - 1];
                      data[data.length - 1] = data[maxIndex];
                      data[maxIndex] = tmp;
                    }
          }
        for (int r = 0; r < data.length; r++){
            System.out.print(data[r] + " ");
        }
    }
}

Updated on 05-Oct-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By