-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtw2Quick.cpp
More file actions
67 lines (58 loc) · 1.15 KB
/
tw2Quick.cpp
File metadata and controls
67 lines (58 loc) · 1.15 KB
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
#include<bits/stdc++.h>
using namespace std;
#define max 1000
int A[max] , B[max];
void Swap(int *a , int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int Partition(int low , int high)
{
int pivot = A[low];
int i = low-1 , j = high+1;
do{
do{i++;}while(A[i] < pivot);
do{j--;}while(A[j] > pivot);
if(i<j)
Swap(&A[i] , &A[j]);
}while(i<j);
Swap(&A[j] , &A[low]);
return j;
}
void QuickSort(int low , int high)
{
if(low < high)
{
int j = Partition(low , high);
QuickSort(low , j-1);
QuickSort(j+1 , high);
}
}
int main()
{
int n;
time_t s , e;
cout << "Enter the Number of Elements : ";
cin >> n;
cout << "The Elements are : ";
for(int i=0;i<n;i++)
{
A[i] = rand()%100;
cout << A[i] << "\t";
}
cout << endl;
s = clock();
for(int i=0;i<100000;i++)
QuickSort(0 , n-1);
e = clock();
cout << "Elements after sorting : ";
for(int i=0;i<n;i++)
{
cout << A[i] <<"\t";
}
cout << endl;
double cpu_time = (double)(e-s)/CLK_TCK;
cout << "Time : " << cpu_time << endl;
}