forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellSort.java
41 lines (39 loc) · 1.18 KB
/
ShellSort.java
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
36
37
38
39
40
41
public class ShellSort {
/**
* sorting function
* Worst case time complexity = O(n^2)
* Best case complexity = O(nlog(n))
* n is input size
*/
public static int[] shellSort(int[] data) {
for (int i = data.length / 2; i > 0; i /= 2) {
for (int j = i; j < data.length; ++j) {
for (int k = j - i; k >= 0; k -= i) {
if (data[k + i] >= data[k]) {
break;
} else {
//swap the value
int temp = data[k];
data[k] = data[k + i];
data[k + i] = temp;
}
}
}
}
return data;
}
// print function
public static void print(int[] data) {
for (Integer item : data) {
System.out.println(item);
}
}
public static void main(String[] args) {
int[] data = {1000, 45, -45, 121, 47, 45, 65, 121, -1, 103, 45, 34};
System.out.println("Data to be sorted:");
print(data);
System.out.println("Sorted data:");
data = shellSort(data);
print(data);
}
}