-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.c
74 lines (64 loc) · 2.04 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
/*---------------------------------------------------- Quicksort -----
| Author: Tarek Alakmeh
| Date: April 2020
| Lang: C (English)
|
| Contents:
|
| Quicksort using LomutoPartition
| - lomutoPartition
| - quicksort
|
|
| (using swap function from ../commons.c)
*-------------------------------------------------------------------*/
/*----------------------------------------------- lomutoPartition -----
| Function lomutoPartition
|
| Purpose: Partition Array into: <= pivot | pivot | > pivot
| A[r] is being selected as pivot.
|
| Parameters:
| int A[] (IN) -- Array to be partitioned
| int l (IN) -- left bound of array to be partitioned
| (start with l = 0)
| int r (IN) -- right bound of array to be partitioned
| (start with r = n-1)
|
| Returns: n/a
*-------------------------------------------------------------------*/
int lomutoPartition(A[], l, r) {
int pivot = A[r];
int i = l-1; // initially l-1 = 0
for(int j = l; j < r; j++) {
if(A[j] <= pivot) {
i = i+1;
swap(&A[i], &A[j]);
}
}
swap(&A[i+1], &A[r]); // swap first element of bigger part (= middle) with pivot
return i+1; // first index of >= pivot part of array (middle)
}
/*--------------------------------------------------- quicksort -----
| Function quicksort
|
| Purpose: Sort Array inplace in O(n log n) time using
| lomutoPartition. Divide and conquer array by recursively
| partitioning array.
|
| Parameters:
| int A[] (IN) -- Array to be sorted
| int l (IN) -- left bound of array to be partitioned
| (start with l = 0)
| int r (IN) -- right bound of array to be partitioned
| (start with r = n-1)
|
| Returns: n/a
*-------------------------------------------------------------------*/
void quicksort(A[], l, r) {
if(l < r) {
int m = lomutoPartition(A,l,r);
quicksort(A, l, m-1);
quicksort(A, m+1, r);
}
}