-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
35 lines (32 loc) · 848 Bytes
/
ShellSort.java
File metadata and controls
35 lines (32 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package algorithms;
/**
*
* @author Gökhan DAĞTEKİN
*
* 2017
*/
public class ShellSort {
public static void main(String[] args) {
int[] array = {5, -5, 1, 47, 2, 65, -44, 8, 5, 21, 58, 3};
shellSort(array, array.length);
}
public static void shellSort(int[] p, int size) {
int i, j, k, temp;
for (k = size; k > 1;) {
k = (k < 5) ? 1 : ((k * 5 - 1) / 11);
for (i = k - 1; ++i < size;) {
temp = p[i];
for (j = i; p[j - k] > temp;) {
p[j] = p[j - k];
if ((j -= k) < k) {
break;
}
}
p[j] = temp;
}
}
for (int l = 0; l < p.length; l++) {
System.out.print(p[l] + " ");
}
}
}