I have Quicksort that always selects the first element of a subsequence as a set:
void qqsort(int array[], int start, int end) { int i = start; // index of left-to-right scan int k = end; // index of right-to-left scan if (end - start >= 1) { // check that there are at least two elements to sort int pivot = array[start]; // set the pivot as the first element in the partition while (k > i) { // while the scan indices from left and right have not met, while (array[i] <= pivot && i <= end && k > i) // from the left, look for the first element greater than the pivot i++; while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first element not greater than the pivot k--; if (k > i) // if the left seekindex is still smaller than the right index, swap the corresponding elements swap(array, i, k); } swap(array, start, k); // after the indices have crossed, swap the last element in the left partition with the pivot qqsort(array, start, k - 1); // quicksort the left partition qqsort(array, k + 1, end); // quicksort the right partition } else { // if there is only one element in the partition, do not do any sorting return; } }
Now, as you can see, this algorithm always takes the first element as a rotation: int pivot = array[start];
I want to change this algorithm so that it always uses the last element instead of the first element of the subsequence, because I want to analyze the physical runtime of both implementations.
I tried changing the line int pivot = array[start]; on int pivot = array[end]; , but then the algorithm deduced an unsorted sequence:
To test another element, I also tried using the central element of the subsequence, but the algorithm still failed:
Can someone please help me understand this algorithm correctly and tell me what changes I need to make in order to successfully implement this implementation, always select the last element of the subsequence as a fulcrum?