forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellSort.c
78 lines (74 loc) · 1.94 KB
/
ShellSort.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//C Program to sort an array using Shell Sort technique
#include <stdio.h>
//ShellSort Function
void ShellSort(int arr[], int num)
{
int gap, i, j, tmp;
//this for loop of gap is the heart of Shell sort
//In each pass w check the valu of gap
//if gap=1 then we stop this loop
for (gap = num / 2; gap > 0; gap = gap / 2)
{
for (i = gap; i < num; i++)
{
//loop continue for index lesser then gap
for(j = i - gap; j >= 0; j = j - gap)
{
//if lesser index value is small
//compared to bigger index
//then continue
if (arr[j+gap] >= arr[j])
break;
//else
// Swap values of bigger index with smaller one
else
{
tmp = arr[j];
arr[j] = arr[j+gap];
arr[j+gap] = tmp;
}
}
}
}
}
//Display function to print the result
void display(int arr[],int n)
{
int i;
printf("\n\nSorted Array in ascending order is : \n");
for(i=0; i < n; ++i)
printf("%5d",arr[i]);
}
//Main Function
int main()
{
int i,k, num;
//Enter the number of element array have
printf("Enter total number of elements :: ");
scanf("%d",&num);
//store the element of array
int arr[num];
//Input values of array
printf("Enter elements :: ");
for(i = 0; i < num; i++)
{
scanf("%d",&arr[i]);
}
//Call shell sort function
ShellSort(arr,num);
//call display function
display(arr,num);
return 0;
}
//end of Main function
/*Sample Input Output
Enter total number of elements :: 10
Enter elements : 36 432 43 44 57 63 94 3 5 6
Sorted Array in ascending order is :
3 5 6 36 43 44 57 63 94 432
*/
/*Complexity
Worst case time complexity is O(n2)
Best case complexity is O(nlog(n)).
Space complexity is O(1).
*/