-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDeterminsticQS .c
73 lines (66 loc) · 1.25 KB
/
DeterminsticQS .c
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
68
69
70
71
72
73
#include<stdio.h>
#include<stdlib.h>
#define swap(a,b) {int (temp);temp =a;a=b;b=temp;}
int Partition(int *A,int i, int j,int pivot)
{
int l = i,r =j;
while(l<=r)
{
while(l<=r && A[l] <=pivot) l++;
while(l<=r && A[r] > pivot) r--;
if(l<=r)
{
swap(A[l],A[r]);
l++;
r--;
}
}
int k = l-1;
A[i-1] = A[k];
A[k] = pivot;
return k;
}
int RFR(int* A,int i,int j,int r)
{
if(i<=j)
{
int p = (rand() % (j-i+1)) +i;
swap(A[p],A[i]);
int k = Partition(A,i+1,j,A[i]);
if(r == j-k+1)
return k;
else if(r<j-k+1)
RFR(A,k+1,j,r);
else
RFR(A,i,k-1,r-j+k-1);
}
else
return -1;
}
void RQS(int *A,int i,int j)
{
if(i<j)
{
int p = RFR(A,i,j,(j-i+1)/2);
swap(A[i],A[p]);
int k = Partition(A,i+1,j,A[i]);
RQS(A,i,k-1);
RQS(A,k+1,j);
}
}
int main()
{
int n;
scanf("%d",&n);
int *arr = malloc(n*sizeof(int));
for(int i = 0; i < n; i++)
{
scanf("%d",arr+i);
}
RQS(arr,0,n-1);
for(int a = 0 ; a < n ; a++)
{
printf("%d ",arr[a]);//printing the sorted array using for loop
}
return 0;
}