-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
49 lines (43 loc) · 935 Bytes
/
QuickSort.cpp
File metadata and controls
49 lines (43 loc) · 935 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
int partition(vector<int> &arr,int l,int r){
int pivot=arr[r];
int idx=l-1;
for(int i=l;i<r;++i){
if(arr[i]<pivot){
idx++;
swap(arr[i],arr[idx]);
}
}
swap(arr[idx + 1], arr[r]);
return (idx+1);
}
void quicksort(vector<int> &arr,int l,int r){
if(l>=r){
return;
}
int x=partition(arr,l,r);
quicksort(arr,l,x-1);
quicksort(arr,x+1,r);
}
int main(){
srand(time(0));
vector<int> arr;
for(int i=0;i<10000;++i){
arr.push_back(rand());
}
cout<<endl;
clock_t time_req;
time_req=clock();
quicksort(arr,0,arr.size()-1);
time_req = clock()- time_req;
cout << "Processor time taken: "
<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}