Advertisement
keneduck

SortableArrayListWithInsertionSort.java

Feb 7th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. class SortableArrayListWithInsertionSort<T extends Comparable<T>>
  2.       extends SortableArrayList<T>
  3. {
  4.  
  5.   public SortableArrayListWithInsertionSort(int capacity)
  6.   {
  7.      super(capacity);
  8.   }
  9.  
  10.  
  11.  
  12.  
  13.   // Sorts the sublist using Insertion Sort
  14.   protected  void sortSublist( int lowIndex, int highIndex) {
  15.         if(lowIndex >= highIndex)
  16.                 return;
  17.         int i = lowIndex + 1;
  18.         int j = i;
  19.         T temp = listItem[i];
  20.         j=subHelper( j, temp);
  21.         listItem[j] = temp;
  22.         sortSublist( lowIndex + 1, highIndex);
  23.     }
  24.  
  25.      int subHelper( int j, T b) {
  26.         if(j <= 0 || listItem[j-1].compareTo(b) <= 1)
  27.             return j;
  28.         listItem[j] = listItem[j - 1];
  29.         return subHelper( j-1, b);
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement