Skip to content

Commit bb7d627

Browse files
Shell Sort in Java
1 parent b3dca9e commit bb7d627

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

sorting/ShellSort.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class ShellSort
9+
{
10+
private ShellSort(){} //this class is not for instantiation
11+
12+
public static void sort(int[] a, int n)
13+
{
14+
int i,j,temp,h;
15+
16+
Scanner scan = new Scanner(System.in);
17+
System.out.print("Enter maximum increment(odd value) : ");
18+
h=scan.nextInt();
19+
20+
while(h>=1)
21+
{
22+
for(i=h; i<n; i++)
23+
{
24+
temp=a[i];
25+
for(j=i-h; j>=0 && a[j]>temp; j=j-h)
26+
a[j+h]=a[j];
27+
a[j+h]=temp;
28+
}
29+
h=h-2;
30+
}
31+
scan.close();
32+
}
33+
34+
public static void main(String[] args)
35+
{
36+
int i,n;
37+
int[] a = new int[20];
38+
Scanner scan = new Scanner(System.in);
39+
40+
System.out.print("Enter the number of elements : ");
41+
n = scan.nextInt();
42+
43+
for(i=0; i<n; i++)
44+
{
45+
System.out.print("Enter element " + (i+1) + " : ");
46+
a[i] = scan.nextInt();
47+
}
48+
49+
sort(a,n);
50+
51+
System.out.println("Sorted array is : ");
52+
for(i=0; i<n; i++)
53+
System.out.print(a[i] + " ");
54+
System.out.println();
55+
scan.close();
56+
}
57+
}

0 commit comments

Comments
 (0)