Sorting algorithms/Shell sort: Difference between revisions
Content added Content deleted
(New page: {{task}}{{Sorting Algorithm}} In this task, the goal is to sort an array of elements using the Shell sort algorithm, a diminishing increment sort. =={{header|Fortran}}== MODULE sort ...) |
(Added Java) |
||
Line 55: | Line 55: | ||
END PROGRAM Shellsort |
END PROGRAM Shellsort |
||
=={{header|Java}}== |
|||
{{trans|Fortran}} |
|||
This method will sort in place. If you want to preserve your unsorted array, use a copy of the array as an argument to this method. |
|||
<java>public static void shell(int[] a) { |
|||
int increment = a.length / 2; |
|||
while (increment > 0) { |
|||
for (int i = increment; i < a.length; i++) { |
|||
int j = i; |
|||
int temp = a[i]; |
|||
while (j >= increment && a[j - increment] > temp) { |
|||
a[j] = a[j - increment]; |
|||
j -= increment; |
|||
} |
|||
a[j] = temp; |
|||
} |
|||
if (increment == 2) { |
|||
increment = 1; |
|||
} else { |
|||
increment *= (5.0 / 11); |
|||
} |
|||
} |
|||
}</java> |