Selection sort implementation in Java

Selection sort implementation


public class Main {
  public static void main(String[] args) {
    int maxSize = 100;
    SelectionSort arr = new SelectionSort(maxSize); // create the array
    arr.insert(2);//  w w w.jav  a 2 s  . c  om
    arr.insert(1);
    arr.insert(4);
    arr.insert(3);
    arr.insert(6);
    arr.insert(5);
    arr.insert(9);
    arr.insert(8);
    arr.insert(10);
    arr.insert(3);

    arr.display();
    arr.selectionSort();
    arr.display();
  }
}

class SelectionSort {
  private long[] a;

  private int nElems;

  public SelectionSort(int max) {
    a = new long[max];
    nElems = 0;
  }

  public void insert(long value) {
    a[nElems] = value;
    nElems++;
  }

  public void display() {
    for (int j = 0; j < nElems; j++)
      System.out.print(a[j] + " ");
    System.out.println("");
  }

  public void selectionSort() {
    int out, in, min;

    for (out = 0; out < nElems - 1; out++) // outer loop
    {
      min = out; // minimum
      for (in = out + 1; in < nElems; in++)
        // inner loop
        if (a[in] < a[min]) // if min greater,
          min = in; // a new min
      swap(out, min); // swap them
    }
  }

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

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization