Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to perform Quick sort using Recursion
Quick Sort is a highly efficient sorting algorithm that uses the divide and conquer approach. It selects a pivot element from the array and partitions the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.
The algorithm works by placing the pivot in its correct position and then recursively applying the same process to the left and right sub-arrays until the entire array is sorted.
How Quick Sort Works
Quick Sort Implementation
Complete Example
using System;
namespace QuickSortDemo {
class Example {
static public int Partition(int[] arr, int left, int right) {
int pivot;
pivot = arr[left];
while (true) {
while (arr[left] < pivot) {
left++;
}
while (arr[right] > pivot) {
right--;
}
if (left < right) {
int temp = arr[right];
arr[right] = arr[left];
arr[left] = temp;
} else {
return right;
}
}
}
static public void QuickSort(int[] arr, int left, int right) {
int pivot;
if (left < right) {
pivot = Partition(arr, left, right);
if (pivot > 1) {
QuickSort(arr, left, pivot - 1);
}
if (pivot + 1 < right) {
QuickSort(arr, pivot + 1, right);
}
}
}
static void Main(string[] args) {
int[] arr = {67, 12, 95, 56, 85, 1, 100, 23, 60, 9};
int n = arr.Length, i;
Console.WriteLine("Quick Sort");
Console.Write("Initial array is: ");
for (i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
QuickSort(arr, 0, n - 1);
Console.Write("\nSorted Array is: ");
for (i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Quick Sort Initial array is: 67 12 95 56 85 1 100 23 60 9 Sorted Array is: 1 9 12 23 56 60 67 85 95 100
Understanding the Algorithm
The Main method initializes the array and displays it before and after sorting −
int[] arr = {67, 12, 95, 56, 85, 1, 100, 23, 60, 9};
QuickSort(arr, 0, arr.Length - 1);
The QuickSort method implements the recursive divide and conquer approach −
if (left < right) {
pivot = Partition(arr, left, right);
QuickSort(arr, left, pivot - 1); // Sort left sub-array
QuickSort(arr, pivot + 1, right); // Sort right sub-array
}
The Partition method selects the leftmost element as pivot and rearranges the array so that elements smaller than the pivot come before it, and elements greater come after it −
int pivot = arr[left]; // Move left pointer right until element >= pivot while (arr[left] < pivot) left++; // Move right pointer left until element <= pivot while (arr[right] > pivot) right--; // Swap elements if pointers haven't crossed if (left < right) swap(arr[left], arr[right]);
Time Complexity
| Case | Time Complexity | Description |
|---|---|---|
| Best Case | O(n log n) | Pivot divides array into equal halves |
| Average Case | O(n log n) | Random pivot selection |
| Worst Case | O(n²) | Pivot is always smallest/largest element |
Conclusion
Quick Sort is an efficient in-place sorting algorithm that uses recursion and the divide-and-conquer strategy. With an average time complexity of O(n log n), it performs well on most datasets and is widely used in practice due to its simplicity and effectiveness.
