Share this page 

Do a selection sortTag(s): Varia


public class SelectionSort {
  public static void sort(int array[]) {
    sort(array, 0, array.length - 1);
  }

  public static void sort(int array[], int min, int max) {
    if (min == max)
       return;

    // Find the smallest.
    int index = select(array, min, max);

    // Swap the smallest with the first.
    int temp = array[min];
    array[min] = array[index];
    array[index] = temp;

    // Sort the rest.
    sort(array, min + 1, max);
    }

  private static int select(int array[], int min, int max) {
    int index = min;
    for (int i = min + 1; i <= max; ++i)
      if (array[i] < array[index])
         index = i;
    return index;
    }
 }