forked from AllAlgorithms/c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell_sort.c
More file actions
61 lines (49 loc) · 1.04 KB
/
Copy pathShell_sort.c
File metadata and controls
61 lines (49 loc) · 1.04 KB
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
// C implementation of shellsort
//
// Author: Avigyan Das
#include <stdio.h>
// Implement bubble sort
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++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
// Function to print elements
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
//main function
int main()
{
int arr[] = {46, 24, 33, 10, 2, 81, 50};
int num = sizeof(arr)/sizeof(arr[0]);
int k;
printf("Unsorted array: \n");
printArray(arr, num);
printf("\n");
shellsort(arr, num);
printf("Sorted array is: \n");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
return 0;
}