forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell_Sort.java
42 lines (30 loc) · 984 Bytes
/
Shell_Sort.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
42
/* Java implementation of Shell Sort*/
public class Shell_Sort
{
public void sort(int input[]) // Function implementing Insertion Sort
{
int n = input.length, temp, j;
for (int gap = n / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i++) // Gapped insertion sort
{
temp = input[i];
for (j = i; j >= gap && input[j - gap] > temp; j = j - gap)
input[j] = input[j - gap];
input[j] = temp;
}
}
}
public static void main(String[] args)
{
int input[] = {5, 6, 8, 4, 6, 5, 3, 3, 2, 7, 8, 45, 85, 96}; //Input array
shellSort ob = new shellSort();
ob.sort(input);
System.out.print("Output : ");
for (int i = 0; i < input.length; ++i) // Printing Sorted array
System.out.print(input[i] + " ");
}
}
/*
Output : 2 3 3 4 5 5 6 6 7 8 8 45 85 96
*/