-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksortn2.cpp
62 lines (59 loc) · 1.29 KB
/
quicksortn2.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
/*
Author: Dhameliya Vatsalkumar
Email: [email protected]
*/
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
// int len;
void print(int arr[],int low,int high){
for(int i=low;i<high;i++)
printf("%d ",arr[i]);
printf("\n");
}
int partition(int arr[],int low,int high){
int i=low+1,j=high;
while(i<=j){
while(i<=high && arr[i]<=arr[low])
i++;
while(j>= low+1 && arr[j]>arr[low])
j--;
if(i<j){
swap(arr[i],arr[j]);
}
}
swap(arr[i-1],arr[low]);
return i-1;
}
void quicksort(int arr[],int low,int high,int arrLen){
if(low<high){
int i=partition(arr,low,high);
quicksort(arr,low,i-1,arrLen);
quicksort(arr,i+1,high,arrLen);
}
}
int main(){
// int arr[]={10,60,50,20,85,40,15};
int maxVal=100;
int arrLen;
cin>>arrLen;
int arr[arrLen];
//len = sizeof(arr)/sizeof(int);
for(int i=0;i<arrLen;i++){
//arr[i] = rand()%maxVal;
arr[i] = arrLen - i;
}
//print(arr,0,arrLen);
//cout<<endl;
const clock_t now = clock();
cout<<now<<endl;
quicksort(arr,0,arrLen-1,arrLen);
const clock_t then = clock();
cout<<then<<endl;
//print(arr,0,arrLen);
//cout<<endl;
cout<<then-now;
return 0;
}