Created
July 2, 2013 02:07
-
-
Save ygabo/5906301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sorter.hpp" | |
using namespace std; | |
int main(){ | |
int d[] = {0,1,2,5,5,5,6,5,5}; | |
vector<int> x(begin(d), end(d)); | |
srand(time(0)); | |
Sorter s(x); | |
s.print(); | |
s.quicksort(); | |
s.print(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sorter.hpp" | |
Sorter::Sorter(){ | |
srand(time(0)); | |
for(int i = 0; i < 10; ++i) | |
x.push_back(rand()%10); | |
} | |
Sorter::Sorter(vector<int> &y){ | |
x = y; | |
} | |
void Sorter::print(){ | |
for( auto it = x.begin(); it != x.end(); ++it) | |
cout << *it << ", "; | |
cout << endl; | |
} | |
void Sorter::print(int left, int right){ | |
for( int i = left; i <=right; ++i) | |
cout << x[i] << ", "; | |
cout << endl; | |
} | |
bool Sorter::check(){ | |
for(int i = 1; i < x.size(); ++i){ | |
if( x[i-1] > x[i]){ | |
cout << x[i-1] << " " << x[i]; | |
cout << " NOP" << endl; | |
return false; | |
} | |
} | |
return true; | |
} | |
void Sorter::quicksort(){ | |
qsort_(0, x.size()-1); | |
} | |
void Sorter::qsort_(int left, int right){ | |
int i = left, j = right; | |
int pivot = x[(left+right)/2]; | |
while (i <= j) { | |
while (x[i] < pivot) i++; | |
while (x[j] > pivot) j--; | |
if (i <= j) { | |
swap(x[i++], x[j--]); | |
} | |
} | |
if (left < j) | |
qsort_(left, j); | |
if (i < right) | |
qsort_(i, right); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SORTER_H | |
#define SORTER_H | |
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <time.h> | |
using namespace std; | |
class Sorter{ | |
public: | |
Sorter(); | |
Sorter(vector<int> &y); | |
void quicksort(); | |
void print(); | |
void print(int left, int right); | |
bool check(); | |
private: | |
vector<int> x; | |
void qsort_(int left, int right); | |
void msort_(); | |
}; | |
#endif // SORTER_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment