Visualisierung der Zwischenschritte des Quicksorts.

Code

#include <stdlib.h>
#include <string.h>
#include <limits.h>

void swap(int a[], int first, int second) {
	int temp = a[first];
	a[first] = a[second];
	a[second] = temp;
}

int quicksort_split(int a[], int left, int right) {
	int pivot, i, j;

	pivot = a[right];
	i = left - 1;
	j = right;
	for (;;) {
		while (a[++i] < pivot) {}
		while (j > i && a[--j] > pivot) {}
		if (i >= j)
			break;
		swap(a, i, j);
	}
	a[right] = a[i];
	a[i] = pivot;
	return i;
}

void quicksort_main(int a[], int left, int right) {
	int pivot;

	if (right > left) {
		pivot = quicksort_split(a, left, right);
		quicksort_main(a, left, pivot - 1);
		quicksort_main(a, pivot + 1, right);
	}
}
int main(int argc, char **argv) {
	int i;

	//argc have size of arguments, we need +1 to
	//transport the array size in the last element
	int *a  = (int *) malloc(sizeof(int) * argc-1);
	a[argc-1] = INT_MAX;

	for(i=1; i < argc;i++){
		a[i-1] = (int)strtol ( argv[i], NULL, 10 );
	}

	quicksort_main(a, 0, argc-1);
	free(a);
	return 0;
}

Zwischenschritte der Sortierung

Eingabe