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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
const int INPUT_SIZE = 10000;
void print(int *input)
{
for ( int i = 0; i < INPUT_SIZE; i++ )
cout << input[i] << " ";
cout << endl;
}
// The partition function
int partition(int* input, int p, int r)
{
int pivot = input[r];
while ( p < r )
{
while ( input[p] < pivot )
p++;
while ( input[r] > pivot )
r--;
if ( input[p] == input[r] )
p++;
else if ( p < r )
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
}
}
return r;
}
void quicksort(int* input, int p, int r)
{
if ( p < r )
{
quick++;
int j = partition(input, p, r);
quicksort(input, p, j-1);
quicksort(input, j+1, r);
}
}
int main()
{
clock_t clock1,clock2;
int input[INPUT_SIZE] = {};
for(int i = 0; i<INPUT_SIZE; i++)
{
int random_num = rand() % 1000;
input[i] = random_num;
}
// int input[INPUT_SIZE] = {500, 700, 800, 100, 300, 200, 900, 400, 1000, 600};
cout << "Input: ";
print(input);
clock1 = clock();
quicksort(input, 0, 9);
clock2 = clock();
cout<< "Quicksort time "<< (float)(clock2 - clock1)/ CLOCKS_PER_SEC << " "<<endl;;
cout << "Output: ";
cout << " ";
cout << " ";
print(input);
cout <<endl;
cout <<endl;
}
| |