Thursday, October 22, 2015

SelectionSort in Java

 public static void selectionsort(int[] A) {
  for (int i = 0; i < A.length; i++) {
   int min = i;
   for (int j = i+1; j < A.length; j++) {
    if (A[j] < A[min]) {
     min = j;
    }
   }
   swap(A, i, min);
  }
 }

 public static void swap(int[] A, int i, int j) {
  int temp = A[j];
  A[j] = A[i];
  A[i] = temp;
 }


No comments:

Post a Comment