-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombSort.cpp
69 lines (57 loc) · 1.19 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>
#define MAX_SIZE 1000
using namespace std;
void combSort(int a[], int n);
int main()
{
int numTestCases;
cin >> numTestCases;
for (int i = 0; i < numTestCases; ++i)
{
int num;
int a[MAX_SIZE];
cin >> num;
for (int j = 0; j < num; j++)
cin >> a[j];
combSort(a, num);
cout << endl;
}
return 0;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void combSort(int a[], int n)
{
int countCmpOps = 0;
int countSwaps = 0;
int gap = n;
double shrink = 1.3;
bool sorted = false;
while (sorted == false)
{
gap = floor(gap / shrink);
if (gap <= 1)
{
gap = 1;
sorted = true;
}
int i = 0;
while (i + gap < n)
{
countCmpOps += 1;
if (a[i] > a[i + gap])
{
swap(a[i], a[i + gap]);
countSwaps += 1;
sorted = false;
}
i = i + 1;
}
}
cout << countCmpOps << " " << countSwaps;
}