forked from ZenanZha/btp500-f16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick.cpp
66 lines (59 loc) · 1.25 KB
/
quick.cpp
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "timer.h"
using namespace std;
void InsertionSort(int arr[],int left,int right){
int curr;
int i, j;
for(i=left+1;i<=right;i++){
curr=arr[i];
for(j=i;j>0 && arr[j-1] > curr;j--){
arr[j]=arr[j-1];
}
arr[j]=curr;
}
}
void QuickSort(int arr[], int left, int right){
if(right-left <=3){
InsertionSort(arr,left,right);
}
else{
int pivotpt=right;
int pivot=arr[pivotpt];
int i=left;
int j=right-1;
while(i<j){
while(i< right-1 && arr[i]<pivot) i++;
while(j > 0 && arr[j]>pivot) j--;
if(i<j)
swap(arr[i++],arr[j--]);
}
if(i==j && arr[i] < arr[pivotpt])
i++;
swap(arr[i],arr[pivotpt]);
QuickSort(arr,left,i-1);
QuickSort(arr,i+1,right);
}
}
void QuickSort(int arr[],int size){
QuickSort(arr,0,size-1);
}
int main(int argc, char* argv[]){
int size=atoi(argv[1]);
int* myarr=new int[size];
Timer stopwatch;
ofstream log("quick.log");
for(int i=0;i<size;i++){
myarr[i]=rand();
// myarr[i]=i+1;
}
stopwatch.start();
QuickSort(myarr,size);
stopwatch.stop();
cout << stopwatch.currtime() << endl;
/* for(int i=0;i<size;i++){
log <<myarr[i]<< endl;
}*/
return 0;
}