Quick Sort Algorithm in DSA C# - Time & Space Complexity
We want to understand how the time to sort a list using Quick Sort changes as the list gets bigger.
Specifically, how many steps does Quick Sort take when sorting more items?
Analyze the time complexity of the following code snippet.
public void QuickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = Partition(arr, low, high);
QuickSort(arr, low, pivotIndex - 1);
QuickSort(arr, pivotIndex + 1, high);
}
}
private int Partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int temp1 = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp1;
return i + 1;
}
This code sorts an array by picking a pivot, dividing the array around it, and sorting the parts recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside Partition that compares and swaps elements.
- How many times: This loop runs for the current sub-array size, and QuickSort calls itself recursively on smaller parts.
When the array is split evenly, each step divides the problem in half, doing work proportional to the size of the current part.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 50 to 60 comparisons and swaps |
| 100 | About 700 to 800 operations |
| 1000 | About 10,000 operations |
Pattern observation: The operations grow faster than just doubling but slower than squaring the input size.
Time Complexity: O(n log n)
This means the time to sort grows a bit more than linearly, multiplying the input size by the number of times it can be halved.
[X] Wrong: "Quick Sort always sorts in linear time because it just splits the array."
[OK] Correct: Splitting helps, but the work inside each split depends on the size of the parts, so it takes more than just linear time.
Understanding Quick Sort's time complexity helps you explain why it is fast on average and what happens in worst cases, showing your grasp of efficient sorting.
"What if we always picked the first element as pivot instead of the last? How would the time complexity change?"
Practice
Quick Sort algorithm?Solution
Step 1: Understand Quick Sort's partitioning
Quick Sort picks a pivot and divides the array into parts smaller and larger than the pivot.Step 2: Apply recursive sorting
It then sorts these parts recursively to get the full sorted array.Final Answer:
Divide the array into smaller parts using a pivot and sort them recursively -> Option CQuick Check:
Quick Sort = Divide and conquer [OK]
- Confusing Quick Sort with Bubble Sort
- Thinking Quick Sort merges arrays like Merge Sort
- Ignoring the pivot concept
Solution
Step 1: Understand partition function return type
The partition function returns the pivot index as an integer after rearranging elements.Step 2: Check C# syntax for arrays and access modifiers
In C#, arrays are declared asint[] arrand public methods specify return type before method name.Final Answer:
public int Partition(int[] arr, int low, int high) -> Option AQuick Check:
Partition returns int index [OK]
- Using void instead of int as return type
- Incorrect array syntax like int arr[] in C#
- Missing access modifier or wrong order
arr = [8, 3, 7, 6, 2] with pivot as last element (2)?Solution
Step 1: Choose pivot and start partitioning
Pivot is 2 (last element). Elements less than 2 go left, others right.Step 2: Rearrange elements around pivot
Only 2 is less or equal to pivot, so it swaps with first element 8. Result: [2, 3, 7, 6, 8]Final Answer:
[2, 3, 7, 6, 8] -> Option BQuick Check:
Pivot 2 placed at start [OK]
- Not swapping pivot correctly
- Assuming full sort after one partition
- Mixing pivot with first element
int Partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
Swap(arr, i, j);
i++;
}
}
Swap(arr, i, high);
return i;
}Solution
Step 1: Understand partition index initialization
Variable i tracks the position for swapping smaller elements. It should start at low - 1 to correctly place elements.Step 2: Check loop and swap logic
Loop runs from low to high-1, swapping when arr[j] < pivot. Starting i at low causes incorrect swaps.Final Answer:
The initial value of i should be low - 1 -> Option DQuick Check:
i starts at low-1 for correct partition [OK]
- Starting i at low causes wrong swaps
- Changing loop bounds incorrectly
- Using wrong comparison operator
[10, 7, 8, 9, 1, 5], what is the array state after the first full Quick Sort partition step choosing the last element as pivot?Solution
Step 1: Identify pivot and partition process
Pivot is 5 (last element). Elements less than 5 move left, others right.Step 2: Rearrange elements around pivot
Elements less than 5 are 1 only (index 4). Swap it with index 0: [1, 7, 8, 9, 10, 5]. Then swap index 1 (7) with high (5): [1, 5, 8, 9, 7, 10].Final Answer:
[1, 5, 8, 9, 7, 10] -> Option AQuick Check:
Pivot 5 placed correctly after smaller elements [OK]
- Assuming full sort after one partition
- Swapping elements incorrectly
- Misplacing pivot index
Ready to prove it?
Pass the test and this pattern moves unknown
