-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22_shell_sort.c
More file actions
49 lines (42 loc) · 1005 Bytes
/
22_shell_sort.c
File metadata and controls
49 lines (42 loc) · 1005 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
#include<stdio.h>
// Function to Sort Array using Shell Sort Algorithm
void shell_sort(int arr[], int n)
{
int index1, temp, gap = n / 2;
while(gap > 0)
{
for(index1 = 0 ; index1 < (n - gap) ; index1++)
if(arr[index1] > arr[index1 + gap])
{
temp = arr[index1];
arr[index1] = arr[index1 + gap];
arr[index1 + gap] = temp;
}
gap /= 2;
}
}
int main()
{
int arr[10];
int n, index;
printf("Enter Number of Elements : ");
scanf("%d", &n);
if(n > 10 || n < 0)
{
printf("Invalid Input");
return 0;
}
printf("Enter Elements\n");
for(index = 0 ; index < n ; index++)
{
printf("Element %d : ", index + 1);
scanf("%d", &arr[index]);
}
shell_sort(arr, n);
printf("\nSorted Array\n");
for(index = 0 ; index < n ; index++)
{
printf("Element %d : %d\n", index + 1, arr[index]);
}
return 0;
}