---
title: "Java - Reverse Selection Sort"  
description: "Java - Reverse Selection Sort"  
author: "Anonymous User"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1603/java-reverse-selection-sort  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Java - Reverse Selection Sort

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to create a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) that does a [selection sort](https://www.mindstick.com/forum/156066/how-does-a-selection-sort-work-for-an-array) 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](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) 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] + " ");
        }
    }
}
```

## Replies

### Reply by Anonymous User

Here is a simple test I wrote to find and make it easy to debug the code.

```
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class Problem20 {
    @Test
    public void testSorting() {
        // passes
        assertTrue(Arrays.equals(new int[]{1, 2, 3}, sortBig(3, 2, 1)));
        // failed previously
        assertTrue(Arrays.equals(new int[]{1, 2, 3, 4}, sortBig(4, 3, 2, 1)));
        // create an array of unique values in pseudo random order
        int[] largeArray = new int[1000];
        for (int i = 0; i < largeArray.length; i++)
            largeArray[i] = (i * 29) % largeArray.length;
        int[] sortedArray = sortBig(largeArray);
        for (int i = 0; i < largeArray.length; i++)
            assertEquals(i, sortedArray[i]);
    }
    public static int[] sortBig(int... data) {
        for (int i = data.length - 1; i >= 0; i--) {
            int maxIndex = i;
            for (int j = i - 1; j >= 0; j--)
                if (data[j] > data[maxIndex])
                    maxIndex = j;
            if (maxIndex != i) {
//                int tmp = data[data.length - 1];
                int tmp = data[i];
//                data[data.length - 1] = data[maxIndex];
                data[i] = data[maxIndex];
                data[maxIndex] = tmp;
            }
        }
        return data;
    }
}
```


---

Original Source: https://www.mindstick.com/forum/1603/java-reverse-selection-sort

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
