Data Structures and Algorithms ยท Unit 6
Sorting Algorithms
Exam-focused notes for Sorting Algorithms (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.
Sorting Algorithms in short
Sorting means arranging data in a fixed order, such as smallest to largest. Sorted data is easier to search, compare and report. This chapter covers quick sort and merge sort, the two main fast methods.
What this unit covers
- Sorting problem definition
- Quick sort algorithm and tracing
- Quick sort time complexity analysis
- Pivot selection and limitations
- Merge sort algorithm and tracing
- Merge sort time complexity analysis
- Why sorting is needed
Why sorting is needed
2082 ยท 10 marks
Why do we need sorting?Trace the Quick sort for the input {12, -9, 56, 23, 4, 8, 6, 9, 23, 21}.[2+8]
Merge sort algorithm and tracing
2080 ยท 10 marks
Discuss the limitation of choosing first element as pivot in quick sort. Using merge sort algorithm, sort the numbers 40, 6, 5,21, 3, 100, 90, 7, 8, 12, 30.[10]
2078 ยท 10 marks
Explain merge sort along with its time complexity. Use this algorithm to sort array of numbers given below: 25, 37, 48, 25, 23, 17, 31, 45, 7, 21, 15, 8, 11[10]
Quick sort algorithm and tracing
2079 ยท 10 marks
Explain quick sort algorithm. Use this algorithm to sort the numbers 35, 82, 18, 54, 13, 31, 20, 69, and 19.[10]
0 ยท 10 marks
Define sorting problem. Trace quick sort algorithm for the following given list of data and also discuss about its time complexity: 78 45 23 89 65 12 90 33[10]
Show solved answerHide answer
Sorting Problem and Quick Sort
Definition of Sorting Problem
The sorting problem is stated as:
- Input: A sequence of $n$ numbers $\langle a_1, a_2, \dots, a_n \rangle$.
- Output: A permutation (rearrangement) $\langle a_1', a_2', \dots, a_n' \rangle$ of the input such that $a_1' \le a_2' \le \dots \le a_n'$.
That is, given an unordered list, arrange its elements in non-decreasing (or non-increasing) order.
Given Data
List to sort:
$$78,\ 45,\ 23,\ 89,\ 65,\ 12,\ 90,\ 33$$
Indices $[0..7]$.
Quick Sort Concept
Quick Sort is a divide-and-conquer algorithm:
- Choose a pivot.
- Partition so that smaller elements go left, larger go right; the pivot lands in its final sorted position.
- Recursively sort the left and right subarrays.
I use the first element as pivot with the standard Lomuto/Hoare-style scan.
Tracing Quick Sort
Pass 1: partition(A, 0, 7), pivot = 78
Array: 78 45 23 89 65 12 90 33
- Move
iright whileA[i] <= 78: 45, 23 pass;A[3]=89 > 78โ stop,i = 3. - Move
jleft whileA[j] > 78:A[7]=33not > 78 โ stop,j = 7. i < jโ swapA[3]=89andA[7]=33:
78 45 23 33 65 12 90 89
- Continue
i: 33, 65, 12 pass;A[6]=90 > 78โ stop,i = 6. - Continue
j:A[7]=89 > 78โ j=6;A[6]=90 > 78โ j=5;A[5]=12not > 78 โ stop,j = 5. i (6) < j (5)?No โ stop. Swap pivotA[0]withA[5]:
12 45 23 33 65 78 90 89 โ pivot 78 fixed at index 5.
Left subarray: indices [0..4] = 12 45 23 33 65
Right subarray: indices [6..7] = 90 89
Pass 2a: partition(A, 0, 4), pivot = 12
12 45 23 33 65
iright whileA[i] <= 12:A[0]=12passes;A[1]=45 > 12โ stop,i = 1.jleft whileA[j] > 12: 65, 33, 23, 45 all > 12;A[0]=12not > 12 โ stop,j = 0.i (1) < j (0)?No โ swap pivotA[0]withA[0](itself).
12 45 23 33 65 78 90 89 โ 12 fixed at index 0.
Left subarray [0..-1] empty; right subarray [1..4] = 45 23 33 65.
Pass 2b: partition(A, 1, 4), pivot = 45
45 23 33 65
iright whileA[i] <= 45: 45, 23, 33 pass;A[4]=65 > 45โ stop,i = 4.jleft whileA[j] > 45:A[4]=65 > 45โ j=3;A[3]=33not > 45 โ stop,j = 3.i (4) < j (3)?No โ swap pivotA[1]withA[3]:
Swap A[1]=45 and A[3]=33:
12 33 23 45 65 78 90 89 โ 45 fixed at index 3.
Left subarray [1..2] = 33 23; right subarray [4..4] = 65 (single element, sorted).
Pass 3: partition(A, 1, 2), pivot = 33
33 23
iright whileA[i] <= 33:A[1]=33passes;A[2]=23 <= 33passes โ i = 3 (out of range).jleft whileA[j] > 33:A[2]=23not > 33 โ stop,j = 2.i (3) < j (2)?No โ swap pivotA[1]withA[2]:
Swap A[1]=33 and A[2]=23:
12 23 33 45 65 78 90 89 โ 33 fixed at index 2.
Left [1..1] = 23 sorted; right [3..2] empty.
Pass 4: partition(A, 6, 7), pivot = 90
90 89
iright whileA[i] <= 90:A[6]=90passes,A[7]=89passes โ i = 8 (out of range).jleft whileA[j] > 90:A[7]=89not > 90 โ stop,j = 7.i (8) < j (7)?No โ swap pivotA[6]withA[7]:
Swap A[6]=90 and A[7]=89:
12 23 33 45 65 78 89 90 โ 90 fixed at index 7.
Left [6..6] = 89 sorted; right empty.
Final Sorted List
$$\boxed{12,\ 23,\ 33,\ 45,\ 65,\ 78,\ 89,\ 90}$$
Time Complexity
Let $T(n)$ be the running time. Partitioning an array of size $n$ costs $\Theta(n)$.
Best case: Pivot splits the array into two nearly equal halves.
$$T(n) = 2T(n/2) + \Theta(n) = \Theta(n \log n)$$
Average case: For random inputs the split is balanced enough that
$$T(n) = \Theta(n \log n)$$
Worst case: Pivot is always the smallest/largest element (e.g. already sorted data with first-element pivot), giving splits of size $0$ and $n-1$:
$$T(n) = T(n-1) + \Theta(n) = \Theta(n^2)$$
Space complexity: $O(\log n)$ average recursion stack, $O(n)$ worst case; it is an in-place sort.
| Case | Time |
|---|---|
| Best | $\Theta(n\log n)$ |
| Average | $\Theta(n\log n)$ |
| Worst | $\Theta(n^2)$ |
Make Unit 6 stick
Practice BIT201 with flashcards & quizzes