Advertisement
iocoder

ShellSort

Jan 16th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1.    
  2.     public static void shell(int[] array) {
  3.         int h = 1;
  4.         while (h < array.length)
  5.             h = h*3+1;
  6.         // grouping
  7.         while (h != 1) {
  8.             h = (h-1)/3;
  9.             // looping on each group.
  10.             for (int start = 0; start < h; start++) {
  11.                 int count = array.length/h + (array.length%h == 0 ? 0 : 1);
  12.                 // the insertion
  13.                 for (int i = 1; i < count; i++) {
  14.                     if (start+i*h >= array.length) break;
  15.                     int t = array[start+i*h], j;
  16.                     for (j = i-1; j >= 0 && array[start+j*h] > t; j--)
  17.                         array[start+(j+1)*h] = array[start+j*h];
  18.                     array[start+(j+1)*h] = t;
  19.                 }
  20.             }
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement