1 /* Quicksort algorithm */
3 /* Michel Vallieres, 2009
5 * ref: http://cprogramminglanguage.net/quicksort-algorithm-c-source-code.aspx
9 void swap(double *x, double *y)
17 int choose_pivot(int i, int j)
19 return i + (j - i) / 2;
22 /* sort the list from list[m] to list[n] */
23 void quicksort(double list[], int m, int n)
29 k = choose_pivot(m, n);
30 swap(&list[m], &list[k]);
36 while ((i <= n) && (list[i] <= key))
38 while ((j >= m) && (list[j] > key))
42 swap(&list[i], &list[j]);
45 // put the pivot back in
46 swap(&list[m], &list[j]);
48 // recursively sort the sub-lists
49 quicksort(list, m, j - 1);
50 quicksort(list, j + 1, n);
54 void sort(int list_size, double *list)
56 quicksort(list, 0, list_size - 1);