-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombSort.cpp
44 lines (41 loc) · 973 Bytes
/
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
template<typename T>
void comb_sort(T* input, int size, bool ascending)
{
int temp;
int i, gap = size;
bool swapped = false;
//Checking to see if the gap value has reached one and no swaps have occurred.
//If so, then the set has been sorted.
while ((gap > 1) || swapped)
{
//Calculation of the gap value.
if (gap > 1)
{
gap = (int)((double)gap / 1.3);
}
swapped = false;
for (i = 0; gap + i < size; ++i)
{
if(ascending) //ascending ordering
{
if (input[i] - input[i + gap] > 0)
{
temp = input[i];
input[i] = input[i + gap];
input[i + gap] = temp;
swapped = true;
}
}
else // descending ordering
{
if (input[i] - input[i + gap] < 0)
{
temp = input[i + gap];
input[i + gap] = input[i];
input[i] = temp;
swapped = true;
}
}
}
}
}