Thursday, October 22, 2015

InsertionSort in Java

 public static void insertionsort(int[] A) {
  for (int i = 1; i < A.length; i++) {
   for (int j = i; j > 0 && A[j] < A[j - 1]; j--) {
    swap(A, j, j - 1);
   }
  }
 }

 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