Lesson 35 of 50
cSorting Algorithms – QuickSort & MergeSort
15 mins
Course progress70%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 35 of 50
c15 mins
Keep these ideas in mind as you work through the lesson.
Both QuickSort and MergeSort achieve O(n log n) average performance.
Both are classic examples of divide‑and‑conquer.
void quicksort(int a[], int lo, int hi) {
if (lo < hi) {
int p = partition(a, lo, hi);
quicksort(a, lo, p - 1);
quicksort(a, p + 1, hi);
}
}
void mergesort(int a[], int left, int right) {
if (left >= right) return;
int mid = left + (right - left) / 2;
mergesort(a, left, mid);
mergesort(a, mid+1, right);
merge(a, left, mid, right);
}