-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomized_quick_sort.cpp
More file actions
64 lines (63 loc) · 1.13 KB
/
Randomized_quick_sort.cpp
File metadata and controls
64 lines (63 loc) · 1.13 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
#include<stdio.h>
#include<stdlib.h>
int count = 0;
int partition(int A[],int l,int r){
int pivot = A[l];
int x = l;
int y = r;
int temp;
while(x<y){
while(A[x]<=pivot){
x++;
count+=2;
}
while(A[y]>pivot){
y--;
count+=2;
}
if(x<y){
temp = A[x];
A[x] = A[y];
A[y] = temp;
}
count+=6;
}
A[l] = A[y];
A[y] = pivot;
return y;
count+=7;
}
int Rand_partition(int A[],int l, int r){
int m,temp;
m = rand()%(r-l)+l;
temp = A[l];
A[l] = A[m];
A[m] = temp;
return partition(A,l,r);
count+=7;
}
void Rand_QuickSort(int A[],int l,int r){
if(l<r){
int p = Rand_partition(A,l,r);
Rand_QuickSort(A,l,p-1);
Rand_QuickSort(A,p+1,r);
}
count+=4;
}
void display(int A[],int n){
int i;
for(i=0;i<n;i++){
printf("%d\t",A[i]);
}
}
int main(){
int A[] = {2,7,8,9,1,4,3,5,6,2};
int n = sizeof(A)/sizeof(int);
printf("The unsorted array of size %d is:\n",n);
display(A,n);
Rand_QuickSort(A,0,n-1);
printf("\nThe array after Randomized QuickSort:\n");
display(A,n);
printf("\nTotal number of steps = %d with array size = %d",count,n);
return 0;
}