forked from Vishal1003/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombsort.cpp
52 lines (41 loc) · 828 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
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
// n is the number of elements
// v is the vector
void combSort(int* v, int n) {
// Initially the gap is n
int gap = n;
bool moved = true;
while (moved == true || gap > 1) {
// Calculate the gap
gap = (gap * 10) / 13;
if (gap < 1) {
gap = 1;
}
moved = false;
for (int i = 0; i < n - gap; i++) {
if (v[i] > v[i + gap]) {
swap(v[i], v[i + gap]);
moved = true;
}
}
}
}
// Printing the vector
void print(int* v, int n) {
for (int i = 0; i < n; i++) {
cout << v[i] << " ";
}
cout << "\n";
}
int main() {
int *v, n;
cout << "Give the number of elements: ";
cin >> n;
cout << "Give the numbers ";
for (int i = 0; i < n; i++) {
cin >> v[i];
}
combSort(v, n);
print(v, n);
}