Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comb sort in c++ #363

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/cpp/CombSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// referenced resource given in README.md to understand comb sort
#include <iostream>
#include <vector>
using namespace std;
vector<int> CombSort(vector<int> vect) {
// gap is size of vector
int gap = vect.size();
// shrink rate is 1.3 according to repo
float shrink = 1.3;
// bool condition for looping
bool looping = true;
while (looping) {
// adjust gap
gap = gap/shrink;
// if gap < 1 then make it 1
if (gap < 1) {
gap = 1;
}
bool done = true;
// for loop for that specific gap
for (int i = 0; i < vect.size()-gap; i++) {
// if out of order, switch them
if (vect.at(i) > vect.at(i+gap)) {
int temp = vect.at(i);
vect.at(i) = vect.at(i+gap);
vect.at(i+gap) = temp;
// condition to show that it is not sorted
done = false;
}
}
// condition that it is sorted when each one was checked (gap = 1), so leave while loop
if (done and gap == 1) {
break;
}
}
return vect;
}
int main() {
// first random vector test
vector<int> check1 = {3, 15, 4, 3, 2, 4, 14, 7, 10, 3, 20, 8, 4, 9, 5,};
// show unsorted vector
cout << "Initial Vector 1: ";
for (int i = 0; i < check1.size(); i++) {
if (i != check1.size()-1) {
cout << check1.at(i) << ", ";
}
else {
cout << check1.at(i) << endl;
}
}
// show sorted vector
vector<int> final1 = CombSort(check1);
cout << "Sorted Vector 1: ";
for (int i = 0; i < final1.size(); i++) {
if (i != final1.size()-1) {
cout << final1.at(i) << ", ";
}
else {
cout << final1.at(i) << endl;
}
}
// second random vector test
vector<int> check2 = {10, 164, 20, 50, 70, 90, 1, 90, 14, 530, 439,};
// show unsorted vector
cout << "Initial Vector 2: ";
for (int i = 0; i < check2.size(); i++) {
if (i != check2.size()-1) {
cout << check2.at(i) << ", ";
}
else {
cout << check2.at(i) << endl;
}
}
// show sorted vector
vector<int> final2 = CombSort(check2);
cout << "Sorted Vector 2: ";
for (int i = 0; i < final2.size(); i++) {
if (i != final2.size()-1) {
cout << final2.at(i) << ", ";
}
else {
cout << final2.at(i) << endl;
}
}
cout << endl;
}