Quick Sort is a highly efficient sorting algorithm that uses the divide-and-conquer strategy. It works by selecting a “pivot” element from the array, partitioning the remaining elements into two sub-arrays (elements less than the pivot and elements greater than the pivot), and recursively sorting the sub-arrays.
Key Concepts of Quick Sort:
- Pivot Selection:
- A pivot is an element around which the array is partitioned.
- Common strategies: choose the first element, last element, middle element, or a random element.
- Partitioning:
- Rearrange elements so that:
- Elements smaller than the pivot are on the left.
- Elements larger than the pivot are on the right.
- The pivot is placed in its correct position in the sorted array.
- Rearrange elements so that:
- Recursive Sorting:
- The process is repeated for the left and right sub-arrays.
Characteristics of Quick Sort:
- Time Complexity:
- Best Case: O(nlogn)O(n \log n)O(nlogn)
- Average Case: O(nlogn)O(n \log n)O(nlogn)
- Worst Case: O(n2)O(n^2)O(n2) (occurs when the pivot is the smallest or largest element repeatedly).
- Space Complexity:
- O(logn)O(\log n)O(logn) (recursive stack space).
- In-Place Sorting:
- Quick Sort does not require extra memory, making it efficient for large datasets.
Implementation of Quick Sort in C:
C Code Example:
#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Partition function
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choosing the last element as pivot
int i = low – 1; // Index of smaller element
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) { // If current element is smaller than or equal to pivot
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]); // Place pivot in the correct position
return i + 1; // Return the pivot index
}
// Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high); // Partitioning index
// Recursively sort elements before and after partition
quickSort(arr, low, pi – 1);
quickSort(arr, pi + 1, high);
}
}
// Utility function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
}
// Main function
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf(“Original array: “);
printArray(arr, n);
quickSort(arr, 0, n – 1);
printf(“Sorted array: “);
printArray(arr, n);
return 0;
}
How It Works:
- Initial Array: {10, 7, 8, 9, 1, 5}
- Pivot: 5 (last element).
- After partition: {1, 5, 8, 9, 7, 10}.
- Pivot 5 is placed in its correct position at index 1.
- Left Sub-Array: {1}
- Already sorted.
- Right Sub-Array: {8, 9, 7, 10}
- Pivot: 10.
- After partition: {8, 9, 7, 10}.
- Pivot 10 is placed in its correct position.
- Recursively sort left and right sub-arrays.
Advantages and Disadvantages:
Advantages:
- Efficient: One of the fastest algorithms for large datasets.
- In-Place: No additional memory is needed for sorting.
Disadvantages:
- Worst-Case Performance: O(n2)O(n^2)O(n2), though this can be mitigated by using randomized pivot selection.
- Recursive: May lead to stack overflow for very large datasets if not optimized.
Example Output:
For the input array {10, 7, 8, 9, 1, 5}:
Original array: 10 7 8 9 1 5
Sorted array: 1 5 7 8 9 10
Quick Sort is one of the most widely used sorting algorithms due to its speed and efficiency, especially for large datasets. Proper pivot selection and handling can mitigate its worst-case performance.