/
combsort.cpp
53 lines (44 loc) · 1.36 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
#include<bits/stdc++.h>
using namespace std;
//Comb sort is an improvement over bubble sort, and works by varying the gap when comparing values. It has better average time, but worst case scenario is still the same.
//Algorithm:
int nextGap(int gap){
//Curiosity: this shrink factor for the gap (~=1.3) has been suggested as the ideal one by the original authors of this sorting algorithm, but can actually be changed.
gap = (gap*10)/13;
if(gap < 1){
return 1;
}
return gap;
}
void combSort(int array[], int n){
int gap = n;
//The purpose of the flag is the same as in bubble sort.
bool flag = true;
while (gap != 1 || flag == true){
gap = nextGap(gap);
flag = false;
for (int i = 0; i < (n - gap); i++){
if(array[i] > array[i + gap]){
swap(array[i], array[i + gap]);
flag = true;
}
}
}
}
//Test:
int main(){
int n;
int array[] = {3, 1, 2, 9, -21, 14, 15, 12, 13, 12};
n = sizeof(array) / sizeof(array[0]);
cout << "Elements before comb sort:\n";
for (int i = 0; i < n; i++){
cout << array[i] << " ";
}
cout << "\n";
combSort(array, n);
cout << "Elements after comb sort:\n";
for(int i = 0; i < n; i++){
cout << array[i] << " ";
}
cout << "\n";
}