-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.java
More file actions
49 lines (39 loc) · 1002 Bytes
/
shell.java
File metadata and controls
49 lines (39 loc) · 1002 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
45
46
47
48
49
/*
Implement the Heap/Shell sort algorithm implemented in Java demonstrating heap/shell data
structure with modularity of programming language.
*/
import java.util.Scanner;
class shell
{
static void shellsort(int[] arr, int num)
{
int i, j, k, tmp;
for (i = num / 2; i> 0; i = i / 2)
{
for (j = i; j<num; j++)
{
tmp=arr[j];
for(k = j; k>= i && arr[k-i]>tmp; k = k - i)
arr[k]=arr[k-i];
arr[k]=tmp;
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int k, num;
System.out.print("Enter no. of elements : ");
num = sc.nextInt();
int arr[]= new int[num];
System.out.println("Enter elements : ");
for (k = 0 ; k<num; k++)
{
arr[k]=sc.nextInt();
}
shellsort(arr, num);
System.out.println("\nSorted array is: ");
for (k = 0; k<num; k++)
System.out.println(arr[k]);
}
}