#include <stdio.h>

void swap (double *a, double *b) {
	double temp = *a;
	*a = *b;
	*b = temp;
}

void qsort_inplace_inner (double list[], long start, long end) {
	if (end - start <= 1) { return; }
	long pivotPos = start, partitionStart = start + 1, partitionEnd = end;
	while (partitionEnd - partitionStart > 0) {
		if (list[partitionStart] < list[pivotPos]) {
			swap(&list[pivotPos], &list[partitionStart]);
			pivotPos = partitionStart;
			partitionStart++;
		}
		else {
			swap(&list[partitionStart], &list[partitionEnd-1]);
			partitionEnd--;
		}
	}
	qsort_inplace_inner(list, start, pivotPos);
	qsort_inplace_inner(list, pivotPos + 1, end);
}

void qsort_inplace (double list[], long len) {
	qsort_inplace_inner(list, 0, len);
}

int main(void) {
	int i;
	double test[] = {85.06,
                     76.68,
                     35.32,
                     45.15,
                     9.85,
                     2.31,
                     37.93,
                     74.72,
                     93.11,
                     90.97,
                     30.62,
                     64.23,
                     61.06,
                     40.58,
                     40.56,
                     41.50,
                     88.69,
                     62.26,
                     50.41,
                     7.04};
    printf("Before: ");
	for (i = 0; i < 20; i++) {
		printf("%.2f ", test[i]);
	}
	printf("\n");
	qsort_inplace(test, 20);
	printf(" After: ");
	for (i = 0; i < 20; i++) {
		printf("%.2f ", test[i]);
	}
	printf("\n");
	return 0;
}
