/********************* * from wikipedia's quicksort article, * the pseudo code for the "complex version" (in place) is as follows : function partition(array, left, right, pivotIndex) pivotValue := array[pivotIndex] swap array[pivotIndex] and array[right] // Move pivot to end storeIndex := left for i from left to right - 1 // left ≤ i < right if array[i] ≤ pivotValue swap array[i] and array[storeIndex] storeIndex := storeIndex + 1 swap array[storeIndex] and array[right] // Move pivot to its final place return storeIndex procedure quicksort(array, left, right) if right > left select a pivot index //(e.g. pivotIndex := left + (right - left)/2) pivotNewIndex := partition(array, left, right, pivotIndex) quicksort(array, left, pivotNewIndex - 1) quicksort(array, pivotNewIndex + 1, right) * Here's what it might look like in C. * * Jim M | Feb 2011 | GPL *************/ #include // Swap two values in an array. void swap(int array[], int i, int j){ int tmp; tmp = array[i]; array[i] = array[j]; array[j] = tmp; } // See comments above. int partition(int array[], int left, int right, int pivotIndex){ int storeIndex, tmp, i, pivotValue; pivotValue = array[pivotIndex]; swap(array, pivotIndex, right); // move pivot to end storeIndex = left; for (i = left; i < right; i++){ if (array[i] <= pivotValue){ swap(array, i, storeIndex); storeIndex++; } } swap(array, storeIndex, right); // move pivot to final place return storeIndex; } // See comments above. void quicksort(int array[], int left, int right){ int pivotIndex, pivotNewIndex; if (right > left){ // select a pivot index; can be anything in left to right range. pivotIndex = left + (right - left)/2; pivotNewIndex = partition(array, left, right, pivotIndex); quicksort(array, left, pivotNewIndex - 1); quicksort(array, pivotNewIndex + 1, right); } } void print_array(int n, int array[]){ // Print array[0,...(n-1)], followed by a newline. int i; printf("["); for (i=0; i