-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathShellSort.java
More file actions
44 lines (34 loc) · 910 Bytes
/
ShellSort.java
File metadata and controls
44 lines (34 loc) · 910 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
36
37
38
39
40
41
42
43
44
package Sorting;
import java.util.Arrays;
import java.util.Scanner;
public class ShellSort {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Length:- ");
int l=sc.nextInt();
int [] arr=new int[l];
for(int i=0;i<l;i++)
{
arr[i]=sc.nextInt();
}
sort(arr);
System.out.println(Arrays.toString(arr));
}
static void sort(int []arr)
{
int n= arr.length;
for (int gap=n/2; gap>0; gap/=2)
{
for (int i=gap; i<n; i++)
{
int t=arr[i];
int j;
for(j=i; j>=gap && arr[j-gap]>t ; j-=gap)
{
arr[j]=arr[j-gap];
}
arr[j]=t;
}
}
}
}