Quick Sort
Code
C language code:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quick_sort(int arr[], int low, int high) {
int stack[high - low + 1];
int top = -1;
stack[++top] = low;
stack[++top] = high;
while (top >= 0) {
high = stack[top--];
low = stack[top--];
int p = partition(arr, low, high);
if (p - 1 > low) {
stack[++top] = low;
stack[++top] = p - 1;
}
if (p + 1 < high) {
stack[++top] = p + 1;
stack[++top] = high;
}
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
quick_sort(arr, 0, n - 1);
printf("\nSorted array is \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Code Explanation
- Initialization: The array
arris the array to be sorted, andnis the length of the array. - Swap function:
- The
swapfunction is used to swap two integer values.
- The
- Partition function:
- The
partitionfunction selects the last element of the array as the pivot element. iis initialized tolow - 1, used to track the region of elements smaller than the pivot.- Traverses the array, swapping elements smaller than the pivot to the left side.
- Finally, places the pivot element in the correct position and returns its index.
- The
- Quick sort function:
quick_sortuses a stack to simulate the recursive process, implementing non-recursive quick sort.- Initializes the stack, pushing the initial interval
[low, high]onto it. - Enters a loop, popping an interval from the stack each time and performing partitioning.
- Pushes the left and right sub-intervals after partitioning onto the stack, until the stack is empty.
Example Run
Assuming the input array is {12, 11, 13, 5, 6, 7}:
- Initial state:
{12, 11, 13, 5, 6, 7} - First partition:
- Select pivot element
7. - Swap elements smaller than
7to the left:{6, 5, 7, 12, 11, 13} - Place pivot element
7in the correct position:{6, 5, 7, 12, 11, 13}
- Select pivot element
- Continue partitioning the left side
[6, 5]and right side[12, 11, 13]:- Left side
[6, 5]after partition:{5, 6, 7, 12, 11, 13} - Right side
[12, 11, 13]after partition:{5, 6, 7, 11, 12, 13}
- Left side
- Final result:
{5, 6, 7, 11, 12, 13}
Time Complexity Analysis
The time complexity of quick sort mainly depends on the partitioning process and recursion depth.
- Best-case time complexity: , when each partition can evenly split the array into two parts.
- Worst-case time complexity: , when each partition selects the smallest or largest element as the pivot, resulting in extremely uneven partitions.
- Average time complexity: , for most input arrays, quick sort's performance is very stable.
Space Complexity Analysis
Quick sort sorts in place, but recursive calls use stack space, so the space complexity is .
Advantages and Disadvantages
Advantages:
- Average time complexity of ; suitable for large-scale data sorting.
- No additional auxiliary space needed; low space complexity.
- Simple to implement; works well in most cases.
Disadvantages:
- Worst-case time complexity is .
- Unstable sorting algorithm; may change the relative order of equal elements.
Applicable Scenarios
Quick sort is suitable for the following scenarios:
- Sorting large datasets.
- When a highly efficient sorting algorithm with low space complexity is needed.
- Scenarios where stable sorting is not required.
Summary
Quick sort is an efficient sorting algorithm that works by selecting a pivot element to split the array into two parts, sorting them separately, and then combining them. Although its worst-case time complexity is relatively high, it performs excellently in average cases and is a sorting algorithm with low time complexity and low space complexity.