Lecture Videos
  1  public class InsertionSort {
  2    /** The method for sorting the numbers */
  3    public static void insertionSort(int[] list) {
  4      for (int i = 1; i < list.length; i++) {
  5        /** insert list[i] into a sorted sublist list[0..i-1] so that
  6            list[0..i] is sorted. */
  7        int currentElement = list[i];
  8        int k;
  9        for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
 10          list[k + 1] = list[k];
 11        }
 12  
 13        // Insert the current element into list[k+1]
 14        list[k + 1] = currentElement;
 15      }
 16    }
 17    
 18    /** A test method */
 19    public static void main(String[] args) {
 20      int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
 21      insertionSort(list);
 22      for (int i = 0; i < list.length; i++)
 23        System.out.print(list[i] + " ");
 24    }
 25  }