fork(7) download
  1. class ShellSort {
  2.  
  3. public static int[] shellSort(int[] array) {
  4. int N = array.length;
  5.  
  6. // determine the initial h value
  7. int h = 1;
  8. while (h < N / 3)
  9. h = 3 * h + 1; // 1, 4, 13, 40, 121, 364, 1093, ...
  10.  
  11. while (h >= 1) {
  12. // h-sort the array
  13. for (int i = h; i < array.length; i++) {
  14. // do insertion sort for h-sized array
  15. for (int j = i; j >= h && array[j] < array[j - h]; j -= h)
  16. swap(array, j, j - h);
  17. }
  18.  
  19. h = h / 3; // reverse the h-size
  20. }
  21.  
  22. return array;
  23. }
  24.  
  25. /*
  26. * swaps data elements between 2 indexes of an array
  27. */
  28. private static void swap(int[] array, int i, int j) {
  29. int temp = array[i];
  30. array[i] = array[j];
  31. array[j] = temp;
  32. }
  33.  
  34. public static void main(String args[]){
  35. ShellSort sSort = new ShellSort();
  36. int[] array = { 77, 99, 44, 55, 22, 88, 11, 0, 66, 33 };
  37. int[] sortedArray = sSort.shellSort(array);
  38. for (int i = 0; i < array.length; i++)
  39. System.out.println(sortedArray[i]);
  40.  
  41. }
  42. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
0
11
22
33
44
55
66
77
88
99