-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.c
99 lines (85 loc) · 1.99 KB
/
QuickSort.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
void display(int*, int);
void swap(int *a, int *b);
void quickSortHoare(int *a, int l, int r);
void quickSortLomuto(int *a, int l, int r);
int HoarePartition(int *a, int s, int e);
int LomutoPartition(int *a, int l, int r);
int main() {
int n;
int *a,*b;
printf("Enter the number of elements: ");
scanf("%d",&n);
a = (int *)malloc(n * sizeof(int));
b = (int *)malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
scanf("%d",&a[i]);
b[i] = a[i];
}
display(a,n);
printf("Using Hoare Partition Scheme \n");
quickSortHoare(a,0,n-1);
display(a,n);
printf("\n");
display(b,n);
printf("Using Lomuto Partition Scheme \n");
quickSortLomuto(b,0,n-1);
display(b,n);
}
void display(int *a, int n) {
for(int i = 0; i < n; i++) {
printf("%d ",a[i]);
}
printf("\n");
}
// https://www.geeksforgeeks.org/hoares-vs-lomuto-partition-scheme-quicksort/
// Note the difference in implementation of partition. What I have done is, as soon as we get the partition index, I swap the pivot
// with the partition index element and then return the partition index.
void quickSortHoare(int *a, int l, int r) {
if(l < r) {
int partition = HoarePartition(a,l,r);
quickSortHoare(a,l,partition-1);
quickSortHoare(a,partition+1,r);
}
}
void quickSortLomuto(int *a, int l, int r) {
if(l < r) {
int partition = LomutoPartition(a,l,r);
quickSortLomuto(a,l,partition-1);
quickSortLomuto(a,partition+1,r);
}
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int HoarePartition(int *a, int s, int e) {
int pivot = a[s];
int i = s + 1;
int j = e;
while(i <= j) {
while(i <= j && a[i] < pivot) i++;
while(i <= j && a[j] > pivot) j--;
if(i < j) {
swap(&a[i],&a[j]);
i++;
j--;
}
}
swap(&a[j], &a[s]);
return j;
}
int LomutoPartition(int *a, int l, int r) {
int pivot = a[r];
int pIndex = l;
for(int i = l; i < r; i++) {
if(a[i] <= pivot) {
swap(&a[i],&a[pIndex]);
pIndex++;
}
}
swap(&a[pIndex],&a[r]);
return pIndex;
}