-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombSort.cpp
54 lines (46 loc) · 1.08 KB
/
CombSort.cpp
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
#include <stdio.h>
#include <stdlib.h>
int newgap(int gap) {
gap = (gap * 10) / 13;
if (gap == 9 || gap == 10)
gap = 11;
if (gap < 1)
gap = 1;
return gap;
}
void combsort(int a[], int aSize) {
int gap = aSize;
int temp, i;
while(true) {
gap = newgap(gap);
int swapped = 0;
for (i = 0; i < aSize - gap; i++) {
int j = i + gap;
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
swapped = 1;
}
}
if (gap == 1 && !swapped)
break;
}
}
int main ()
{
int n, i, a[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nBefore Sorting:\n");
for(i=0; i<n; i++)
printf("%d ", a[i]);
combsort(a, n);
printf("\n\nAfter Sorting:\n");
for(i = 0;i < n;i++)
printf("%d ", (a[i]));
return 0;
}