-
Notifications
You must be signed in to change notification settings - Fork 368
/
comb_sort.c
51 lines (48 loc) · 1.05 KB
/
comb_sort.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
#include <stdio.h>
#include <stdbool.h>
int getNextGap(int gap)
{
gap = (gap * 10) / 13;
if (gap < 1)
return 1;
return gap;
}
void combSort(int a[], int n)
{
int gap = n;
bool swapped = true;
while (gap != 1 || swapped == true)
{
gap = getNextGap(gap);
swapped = false;
for (int i = 0; i < n - gap; i++)
{
if (a[i] > a[i + gap])
{
int temp = a[i];
a[i] = a[i + gap];
a[i + gap] = temp;
swapped = true;
}
}
}
}
int main()
{
printf("Enter the length of the array: ");
int n;
scanf("%d", &n);
int *a = (int *)malloc(n * sizeof(int));
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Original array:\n");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
combSort(a, n);
printf("\nSorted array:\n");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
free(a);
return 0;
}