Data Structures and Algorithms Ā· Unit 6 Ā· 8 hrs
Sorting
Exam-focused notes for Sorting (Data Structures and Algorithms, CSC211): what the TU syllabus asks and how it has actually been tested, with 10 solved past questions from this unit.
Sorting in short
This chapter studies how to arrange a list of elements into sorted order: internal and external sorting, the simple $O(n^2)$ comparison sorts (bubble, selection, insertion, shell), the faster divide and conquer sorts (merge, quick, heap), and how their efficiencies compare.
What this unit covers
- Introduction and Types of sorting: Internal and External sort
- Comparison Sorting Algorithms: Bubble, Selection and Insertion Sort, Shell Sort
- Divide and Conquer Sorting: Merge, Quick and Heap Sort
- Efficiency of Sorting Algorithms
Comparison Sorting Algorithms
2081 Ā· 5 marks
Sort the number {82, 73, 12, 39, 26, 88, 2, 9, 60, 41} using shell sort. [5]
Show solved answerHide answer
Shell Sort: {82, 73, 12, 39, 26, 88, 2, 9, 60, 41}
Given
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | 82 | 73 | 12 | 39 | 26 | 88 | 2 | 9 | 60 | 41 |
- $n = 10$
- Increment sequence chosen: $k = 5, 2, 1$ (standard $n/2$ halving)
Solution
Pass 1: Increment $k = 5$
Sub-files (elements 5 positions apart):
| Sub-file | Indices | Values | Sorted |
|---|---|---|---|
| 1 | 0, 5 | {82, 88} | {82, 88} |
| 2 | 1, 6 | {73, 2} | {2, 73} |
| 3 | 2, 7 | {12, 9} | {9, 12} |
| 4 | 3, 8 | {39, 60} | {39, 60} |
| 5 | 4, 9 | {26, 41} | {26, 41} |
Array after Pass 1:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | 82 | 2 | 9 | 39 | 26 | 88 | 73 | 12 | 60 | 41 |
Pass 2: Increment $k = 2$
Sub-files (elements 2 positions apart):
- Even indices ${0,2,4,6,8}$: {82, 9, 26, 73, 60} $\rightarrow$ {9, 26, 60, 73, 82}
- Odd indices ${1,3,5,7,9}$: {2, 39, 88, 12, 41} $\rightarrow$ {2, 12, 39, 41, 88}
Interleave back:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | 9 | 2 | 26 | 12 | 60 | 39 | 73 | 41 | 82 | 88 |
Pass 3: Increment $k = 1$ (standard insertion sort)
Start: {9, 2, 26, 12, 60, 39, 73, 41, 82, 88}
- Insert 2: {2, 9, 26, 12, 60, 39, 73, 41, 82, 88}
- Insert 26: no change
- Insert 12: {2, 9, 12, 26, 60, 39, 73, 41, 82, 88}
- Insert 60: no change
- Insert 39: {2, 9, 12, 26, 39, 60, 73, 41, 82, 88}
- Insert 73: no change
- Insert 41: {2, 9, 12, 26, 39, 41, 60, 73, 82, 88}
- Insert 82: no change
- Insert 88: no change
Final Sorted Array
| 2 | 9 | 12 | 26 | 39 | 41 | 60 | 73 | 82 | 88 |
|---|
Result: ${2, 9, 12, 26, 39, 41, 60, 73, 82, 88}$
2080 Ā· 5 marks
Trace selection sort algorithm with array of numbers 2, 81, 6, 45, 11, 21, 23, 41, and 11. [5]
2079 Ā· 5 marks
Sort the numbers 82, 73, 12, 39, 26, 88, 2, 9, 60, 41 using shell sort. [5]
2078 Ā· 5 marks
Hand test selection sort with array of numbers 4, 71, 32, 19, 61, 2, -5 in descending order. [5]
2077 Ā· 5 marks
Hand test bubble sort with array of numbers 53, 42, 78, 3, 5, 2, 15 in ascending order. [5]
2074 Ā· 5 marks
What do you mean by sorting? Explain the Bubble sort with example. [5]
Show solved answerHide answer
Sorting and Bubble Sort
What is Sorting?
Sorting is the process of arranging data elements in a specific order (either ascending or descending) so that the data can be searched, accessed, or processed more efficiently. Sorting organizes a collection of data into a sequence ordered by some criterion.
Example: Arranging [5, 2, 8, 1, 9] into [1, 2, 5, 8, 9] (ascending order).
Bubble Sort
Definition: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. The basic idea is to pass through the array sequentially several times. Each pass consists of comparing each element a[i] with its adjacent element a[i+1] and interchanging them if they are not in proper order.
After each pass, the largest unsorted element "bubbles up" to its correct position at the end of the array.
Algorithm
for i = 0 to n-2:
for j = 0 to n-2-i:
if a[j] > a[j+1]:
swap(a[j], a[j+1])
Time Complexity
| Case | Complexity |
|---|---|
| Best Case | O(n^2) |
| Average Case | O(n^2) |
| Worst Case | O(n^2) |
Worked Example
Array: [5, 3, 8, 1, 4]
Pass 1 (i = 0)
| Step | Comparison | Array State |
|---|---|---|
| j=0 | 5 > 3? Yes, swap | [3, 5, 8, 1, 4] |
| j=1 | 5 > 8? No | [3, 5, 8, 1, 4] |
| j=2 | 8 > 1? Yes, swap | [3, 5, 1, 8, 4] |
| j=3 | 8 > 4? Yes, swap | [3, 5, 1, 4, 8] |
Largest element 8 is now at its correct position.
Pass 2 (i = 1)
| Step | Comparison | Array State |
|---|---|---|
| j=0 | 3 > 5? No | [3, 5, 1, 4, 8] |
| j=1 | 5 > 1? Yes, swap | [3, 1, 5, 4, 8] |
| j=2 | 5 > 4? Yes, swap | [3, 1, 4, 5, 8] |
Second largest element 5 is now at its correct position.
Pass 3 (i = 2)
| Step | Comparison | Array State |
|---|---|---|
| j=0 | 3 > 1? Yes, swap | [1, 3, 4, 5, 8] |
| j=1 | 3 > 4? No | [1, 3, 4, 5, 8] |
Pass 4 (i = 3)
| Step | Comparison | Array State |
|---|---|---|
| j=0 | 1 > 3? No | [1, 3, 4, 5, 8] |
Final Sorted Array
[1, 3, 4, 5, 8]
Summary
- Bubble sort is simple but inefficient for large datasets due to O(n^2) complexity.
- It performs a maximum of n-1 passes for an array of n elements.
- It is an in-place sorting algorithm (no extra memory required).
Divide and Conquer Sorting
2079 Ā· 5 marks
In which case the position of pivot element in quick sort always either in the last or the first position? Create a max heap from the numbers {10,12,53,34,23,77,59,66,5,8}. [5]
2078 Ā· 5 marks
Write short notes on: a. Divide and Conquer sorting b. AVL Tree [5]
2075 Ā· 10 marks
Explain concept of divide and conquer algorithm. Hand test quick sort algorithm with array of numbers (78, 34, 21, 43, 7, 18, 9, 56, 38, 19). What is time complexity of quick sort algorithm?[10]
Make Unit 6 stick
Practice CSC211 with flashcards & quizzes