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

Organization and Cocktail Sort #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 0 additions & 52 deletions ShellSort.cpp

This file was deleted.

107 changes: 107 additions & 0 deletions SortingAlgorithms/Cocktail_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Sorts the vector in Ascending Order
* Time complexity: O(n^2)
*/

#include <iostream>
#include <vector>
#include <random>
using namespace std;

vector<int> cocktail_sort(vector<int>& arr)
{
//loop through the vector n/2 times (n = number of elements)
for(int i=0;i<arr.size()/2;i++)
{
//bubble short: move larger number to right
for(int j=1; j<arr.size(); j++)
{
if(arr[j-1] > arr[j])
{
swap(arr[j-1], arr[j]);
}
}

//buuble sort: move smaller number to left
for(int j=arr.size()-1; j>1; j--)
{
if(arr[j] < arr[j-1])
{
swap(arr[j-1], arr[j]);
}
}
}

return arr;
}

int main()
{
static random_device rd;
static mt19937 rng(rd());
static uniform_int_distribution<int> randRange(-100, 100);

vector<int> test;

//test
cout << "test1:" << endl;
cout << "Orginal vector: ";
for(int i=0; i < 10; i++)
{
test.push_back(randRange(rd));
cout<<test[i] << ", ";
}
cout << endl;

cocktail_sort(test);

cout << "Cocktail Sort result: ";
for(int i=0; i < 10; i++)
{
cout<<test[i] << ", ";
}
cout << endl;

//more test cases
test.clear();
cout << "test2:" << endl;
cout << "Orginal vector: ";
for(int i=10; i > 0; i--)
{
test.push_back(i);
cout<< i << ", ";
}
cout << endl;

cocktail_sort(test);

cout << "Cocktail Sort result: ";
for(int i=0; i < test.size(); i++)
{
cout<<test[i] << ", ";
}
cout << endl;

test.clear();
cout << "test3:" << endl;
cout << "Orginal vector: ";
for(int i=10; i > 0; i-=2)
{
test.push_back(i);
test.push_back(i);
cout<< i << ", ";
cout<< i << ", ";
}
cout << endl;

cocktail_sort(test);

cout << "Cocktail Sort result: ";
for(int i=0; i < test.size(); i++)
{
cout<<test[i] << ", ";
}
cout << endl;

return(0);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 0 additions & 52 deletions count_sort.cpp

This file was deleted.

67 changes: 0 additions & 67 deletions countingsort_negative.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions merge_sort.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions selection_sort.cpp

This file was deleted.

Loading