-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.cpp
More file actions
55 lines (44 loc) · 1.03 KB
/
lab1.cpp
File metadata and controls
55 lines (44 loc) · 1.03 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
#include <bits/stdc++.h>
using namespace std;
int count1 = 0;
int partition(int arr[], int p, int q){
int z = p-1;
int pivot = arr[q];
for(int j = p; j < q; j++){
if(arr[j]<pivot){
z++;
swap(arr[z],arr[j]);
}
}
count1 = count1 + (q-p);
swap(arr[z+1],arr[q]);
return z+1;
}
int randomize_partition(int arr[],int p, int q){
int t = p + rand()%(q-p+1);
swap(arr[t],arr[q]);
return(partition(arr,p,q));
}
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
void randomize_quickSort(int arr[], int p, int q){
if(p < q){
int mid = randomize_partition(arr,p,q);
randomize_quickSort(arr,p,mid-1);
randomize_quickSort(arr,mid+1,q);
}
}
int main()
{
srand(time(NULL));
int arr[1000];
for(int i = 0; i < 1000; i++){
arr[i] = 1000-i;
}
randomize_quickSort(arr,0,999);
cout<<"no of comparision"<<count1<<" ";
return 0;
}