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
|
/*
Quick Sort Implementation 1
Basic implementation of quick sort algorithm
July 21 2013
*/
#include "iostream"
#include "vector"
using namespace std;
int partition(int first, int last, vector<int>& myVector){
int up,down,buffer,pivot;
up = first;
down = last - 1;
pivot = myVector[last];
do{
while(myVector[up] < pivot){
up++;
}
while((myVector[down] > pivot) && (up< down-1)){
down--;
}
buffer = myVector[up];
myVector[up] = myVector[down];
myVector[down]= buffer;
}while(up<down);
buffer = myVector[first];
myVector[first] = myVector[down];
myVector[down] = buffer;
return down;
}
void quick_sort(int low, int high, vector<int>& myVector)
{
if(low<high)
{
int pivot = partition(low,high,myVector);
quick_sort(low,pivot-1,myVector);
quick_sort(pivot,high, myVector);
}
}
void main()
{
int low,high,userInput;
int size;
cout << "Enter the size of myVector" << endl;
cin >> size;
vector<int> myVector;
for(int j=0; j< size; j++){
cout << "Enter the numbers into myVector" << endl;
cin >> userInput;
myVector.push_back(userInput);
}
quick_sort(0,myVector.size()-1,myVector);
}
|