Quick Sort Algorithm with Comparison Counting - C++ Implementation
This is an implementation of the Quick Sort algorithm in C++ that takes in an array of integers, a starting index and an ending index, and returns the number of comparisons made during the sorting process.
int quickSortComparisons(vector<int>& arr, int start, int end) {
if (start >= end) {
return 0;
}
int comparisons = 0;
int pivot = arr[start];
int i = start + 1;
int j = end;
while (i <= j) {
while (i <= j && arr[i] <= pivot) {
i++;
comparisons++;
}
while (i <= j && arr[j] > pivot) {
j--;
comparisons++;
}
if (i < j) {
swap(arr[i], arr[j]);
comparisons += 2; // 加上交换的比较次数
}
}
swap(arr[start], arr[j]);
comparisons += 2; // 加上交换的比较次数
comparisons += quickSortComparisons(arr, start, j - 1);
comparisons += quickSortComparisons(arr, j + 1, end);
return comparisons;
}
The algorithm works by selecting a pivot element (in this case, the first element of the array), and partitioning the array into two sub-arrays - one containing elements smaller than the pivot, and the other containing elements greater than the pivot. This process is repeated recursively on each sub-array until the entire array is sorted.
The variable 'comparisons' keeps track of the number of comparisons made during the sorting process. It is initialized to 0 and incremented each time a comparison is made.
The while loop in lines 12-25 is the partitioning process. It starts with the left pointer i at the second element of the array (since the first element is the pivot), and the right pointer j at the last element of the array. It then loops until i and j cross each other. The inner while loop in lines 14-17 moves the left pointer i to the right until it encounters an element greater than the pivot. Similarly, the inner while loop in lines 18-21 moves the right pointer j to the left until it encounters an element smaller than or equal to the pivot. When i and j cross each other, the loop ends.
If i is less than j, it means that there are still elements in the wrong sub-array, so we swap the elements at i and j to put them in the correct sub-array. We also increment the 'comparisons' variable by 2 to account for the two comparisons made during the swap.
After the loop ends, we swap the pivot element with the element at index j to put the pivot in its correct position. We again increment the 'comparisons' variable by 2 to account for the two comparisons made during the swap.
Finally, we recursively call the function on the left sub-array (from start to j-1) and the right sub-array (from j+1 to end), and add the number of comparisons made during those recursive calls to the current value of 'comparisons'. We then return the final value of 'comparisons'.
Overall, this implementation of Quick Sort has a time complexity of O(nlogn) on average, and O(n^2) in the worst case (when the array is already sorted or nearly sorted). The number of comparisons made during the sorting process is a good metric for comparing the efficiency of different sorting algorithms.
原文地址: https://www.cveoy.top/t/topic/omqp 著作权归作者所有。请勿转载和采集!