-
Notifications
You must be signed in to change notification settings - Fork 97
/
ascShellSort.java
44 lines (43 loc) · 1.15 KB
/
ascShellSort.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
43
44
import java.util.*;
public class ascShellSort
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n; //size of the array
System.out.println("Enter the size of the array");
n = in.nextInt();
int[] intArray = new int[n];
for(int i=0;i<n;i++)
{
intArray[i] = in.nextInt();
}
shellsort(intArray);
//printing array
System.out.println("The sorted array: ");
for(int i=0;i<intArray.length;i++)
{
System.out.print(intArray[i]+" ");
}
System.out.println();
in.close();
}
public static void shellsort(int[] array)
{
for(int gap=array.length/2;gap>0;gap/=2) //using different gap values
{
for(int i=gap;i<array.length;i++)
{
int raw = array[i];
int j=i;
while(j>=gap && array[j-gap]>raw)
{
//shifting
array[j] = array[j-gap];
j=j-gap;
}
array[j] = raw;
}
}
}
}