The algorithm starts with an error when there are a lot of duplicates in the array. You noticed this only with large values, because you feed the algorithm with random values โโthat have a large range (I assume you used rand () with: 0 - RAND_MAX), and this problem only occurs with large arrays.
When you try to sort an array of identical numbers (try sorting 100,000 identical numbers, the program crashes), you will first go through the entire array, changing the elements too much. Then you split the array into two, but the large array is reduced by only 1:
v quicksortit(tab,(f1-1)-tab);
Thus, your algorithm becomes O (n ^ 2), and you also consume a very large amount of stack. Finding the best rod will not help you in this case, rather choose a version of quicksort () that does not exhibit this drawback.
For instance:
function quicksort(array) if length(array) > 1 pivot := select middle, or a median of first, last and middle left := first index of array right := last index of array while left <= right while array[left] < pivot left := left + 1 while array[right] > pivot right := right - 1 if left <= right swap array[left] with array[right] left := left + 1 right := right - 1 quicksort(array from first index to right) quicksort(array from left to last index)
This is a modified version: http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
source share