-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathShellSort.java
More file actions
32 lines (28 loc) · 821 Bytes
/
ShellSort.java
File metadata and controls
32 lines (28 loc) · 821 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
class ShellSort {
public static void main(String args[]) {
int arr[] = {12,43,52,84,53,58,92,68,8};
ShellSort ob = new ShellSort();
ob.sort(arr);
System.out.println("Sorted array: ");
printArray(arr);
}
static void printArray(int arr[]) {
int n = arr.length;
for (int i=0; i<n; ++i) {
System.out.print(arr[i] + " ");
}
}
int sort(int arr[]) {
int n = arr.length;
for (int gap = n/2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i += 1) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
}