-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.c
34 lines (26 loc) · 843 Bytes
/
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
#include "swap.c"
void quicksort(int v[], int left, int right);
/**
* @brief Quick sort algorithm using the middle element as pivot.
*
* @param v Array to be sorted.
* @param left Left index of the array.
* @param right Right index of the array.
*/
void quicksort(int v[], int left, int right) {
int i, last;
void swap(int v[], int i, int j);
// Base case of recursion.
// If there is only one element in the array:
if (left >= right)
return;
// Partition the array.
swap(v, left, (left + right) / 2);
last = left; // move the pivot to v[0] position.
for (i = left + 1; i <= right; i++)
if (v[i] < v[left])
swap(v, ++last, i);
swap(v, left, last); // move the pivot to its final position.
quicksort(v, left, last - 1);
quicksort(v, last + 1, right);
}