-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick Sort using random pivot in C.c
68 lines (61 loc) · 1.36 KB
/
Quick Sort using random pivot in C.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
//Time complexity for average case scenario is O(nlogn), for best case scenario is O(logn) and for worst case is O(n^2)
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int partition(int a[], int low, int high);
void QuickSort(int a[], int low, int high);
int c=0;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int arr[100],i,n;
printf("Size of array? \n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("arr[%d]: \n", i+1);
scanf("%d", &arr[i]);
}
QuickSort(arr, 0, n-1);
printf("The sorted array is : \n");
for(i=0;i<n;i++)
printf("%d ", arr[i]);
printf("\n The number of comparisons is %d", c);
return 0;
}
int partition(int a[], int low, int high)
{
int index,i,j,pivot;
//Generating random index for pivot
index=rand()%(high-low+1)+low;
swap(&a[index],&a[high]);
pivot=a[high];
i=low-1;
for(j=low;j<high;j++)
{
c++;
if(a[j]<pivot)
{
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i+1], &a[high]);
return (i+1);
}
void QuickSort(int a[], int low, int high)
{
int loc;
if(low<high)
{
loc=partition(a,low,high);
QuickSort(a,low,loc-1);
QuickSort(a,loc+1,high);
}
}