Java Data Structure How to - Implement Selection Sort








Question

We would like to know how to implement Selection Sort.

Answer

public class MainClass {
  public static void main(String[] args) {
    int[] intArray = new int[] { 2, 6, 3, 8, 4, 9, 1 };
//from   w  w w . j  a v  a2  s.  c  om
    for (int i : intArray) {
      System.out.print(i);
    }
    System.out.println();
    selectionSort(intArray);

    for (int i : intArray) {
      System.out.print(i);
    }

  }

  public static void selectionSort(int[] intArray) {
    for (int out = 0; out < intArray.length - 1; out++) {
      int min = out;
      for (int in = out + 1; in < intArray.length; in++)
        if (intArray[in] < intArray[min])
          min = in;
      swap(intArray, out, min);
    }
  }

  private static void swap(int[] intArray, int one, int two) {
    int temp = intArray[one];
    intArray[one] = intArray[two];
    intArray[two] = temp;
  }
}

The code above generates the following result.