forked from jcchurch/C-Linear-Algebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.c
43 lines (38 loc) · 1.23 KB
/
qsort.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
#include "qsort.h"
/*===========================================================================
* quicksort
* This algorithm performs the quicksort algorithm given an array of
* doubles, a start index, and an end index.
*=========================================================================*/
void quicksort(double *a, int start, int end) {
int pivot;
if (start < end) {
pivot = partition (a, start, end);
quicksort (a, start, pivot-1);
quicksort (a, pivot+1, end);
}
}
/*===========================================================================
* partition
* This algorithm partitions an array so that all of the low values are
* near the beginning and high values are near the end.
* The partitioning value is the first element.
*=========================================================================*/
int partition(double *a, int start, int end) {
double val = a[start];
int pivot = start;
double temp;
int i;
for (i = start+1; i <= end; i++) {
if (a[i] < val) {
pivot++;
temp = a[pivot];
a[pivot] = a[i];
a[i] = temp;
}
}
temp = a[start];
a[start] = a[pivot];
a[pivot] = temp;
return pivot;
}