Lect03 Ds Sorting
Lect03 Ds Sorting
2024
Contents
1. Selection Sort
2. Insertion Sort
3. Bubble Sort
4. Shell Sort
5. Heap Sort
6. Merge Sort
7. Quick Sort
8. Key-indexed Counting
9. Radix Sort
10. Workshop
Introduction
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Workshop
• The basic problem in sorting is as follows: Given an array that contains n
elements (keys), our task is to sort the elements in increasing order. For
example, the array
1 3 8 2 9 2 5 6
3
Introduction (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Key-indexed
Counting
• For instance, an array {E, X, A, M, P, L, E} has 11 inversions:
Radix Sort E-A, X-A, X-M, X-P, X-L, X-E, M-L, M-E, P-L, P-E, and L-E.
Workshop
Concept 3
If the number of inversions in an array is less than a constant multiple of the array
size, we say that the array is partially sorted.
Mng có n ã sp
4
Introduction (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
Concept 4
Merge Sort A sorting method is stable if it preserves the relative order of equal keys in the
Quick Sort array
Key-indexed
Counting
Radix Sort
Workshop
5
Introduction (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Workshop Concept 6
A non-adaptive sorting algorithm is a type of sorting algorithm that does not
take advantage of any existing order or structure in the input data.
6
Sorting Algorithms
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
O(n2 ) algorithms
Heap Sort
Merge Sort
• Selection Sort
Quick Sort • Insertion Sort
Key-indexed • Bubble Sort
Counting
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
8
Selection Sort
Selection Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
10
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
11
Illustration
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Trace (right before the call to swap())
Merge Sort a[]
Quick Sort
i min 0 1 2 3 4 5 6 7 8 9 10
Key-indexed
Counting S O R T E X A M P L E
Radix Sort 0 6 S O R T E X A M P L E
1 4 A O R T E X S M P L E
Workshop
2 10 A E R T O X S M P L E
3 9 A E E T O X S M P L R
4 7 A E E L O X S M P T R
5 7 A E E L M X S O P T R
6 8 A E E L M O S X P T R
7 10 A E E L M O P X S T R
8 8 A E E L M O P R S T X
9 9 A E E L M O P R S T X
10 10 A E E L M O P R S T X
A E E L M O P R S T X
12
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Analysis of selection sort for the input size of N (the number of keys)
Counting
Radix Sort
Workshop
• Time complexity:
best case ?
average case ?
worst case ?
Insertion Sort
Bubble Sort
Shell Sort
15
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Algorithm 1
Merge Sort
template <class Item >
Quick Sort
void insertion (Item a[], int l, int r) {
Key-indexed
Counting Item v;
Radix Sort
for (int i = l + 1; i <= r; i++) {
Workshop
v = a[i];
for (int j = i; j > l && v < a[j - 1]; j--)
a[j] = a[j - 1];
a[j] = v;
}
}
16
Implementation (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Algorithm 2 (using sentinel technique)
Merge Sort
template <class Item >
Quick Sort
void insertion (Item a[], int l, int r) {
Key-indexed
Counting int i;
Radix Sort
for (i = r; i > l; i--) compare_swap (a[i - 1], a[i]);
Workshop
for (i = l + 2; i <= r; i++) {
v = a[i];
for (int j = i; v < a[j - 1]; j--)
a[j] = a[j - 1];
a[j] = v;
}
}
17
Illustration
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Trace (right after the inner loop is exhausted)
Merge Sort a[]
Quick Sort
i j 0 1 2 3 4 5 6 7 8 9 10
Key-indexed
Counting S O R T E X A M P L E
Radix Sort 1 0 O S R T E X A M P L E
2 1 O R S T E X A M P L E
Workshop
3 3 O R S T E X A M P L E
4 0 E O R S T X A M P L E
5 5 E O R S T X A M P L E
6 0 A E O R S T X M P L E
7 2 A E M O R S T X P L E
8 4 A E M O P R S T X L E
9 2 A E L M O P R S T X E
10 2 A E E L M O P R S T X
A E E L M O P R S T X
18
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
19
Analysis (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Time complexity:
Merge Sort
best case ?
Quick Sort
Key-indexed
average case ?
Counting worst case ?
Radix Sort
20
Bubble Sort
Bubble Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
• Keep passing through the array, exchanging adjacent elements that are out of
Heap Sort
Merge Sort
order, continuing until the array is sorted.
Quick Sort For example, in the array
Key-indexed 1 3 8 2 9 2 5 6
Counting
Radix Sort the first round of bubble sort swaps elements as follows:
Workshop 1 3 2 8 9 2 5 6
1 3 2 8 2 9 5 6
1 3 2 8 2 5 9 6
1 3 2 8 2 5 6 9
Insertion Sort
Bubble Sort
Shell Sort
23
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Time complexity:
Merge Sort
best case ?
Quick Sort
Key-indexed
average case ?
Counting worst case ?
Radix Sort
24
Shell Sort
Shell Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
• Insertion sort is slow because the only exchanges it does involve adjacent
Heap Sort
Merge Sort
items, so items can move through the array only one place at a time.
Quick Sort
• Shellsort is a simple extension of insertion sort that gains speed by allowing
Key-indexed exchanges of elements that are far apart.
Counting
• The running time is better than O(n2 )
Radix Sort
Workshop Concept 7
An h-sorted array is h independent sorted subsequences, interleaved together.
26
Shell Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
27
Increment/Decrement Sequence
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
• Shell proposed
Heap Sort
N
h1 =
Merge Sort
Quick Sort 2
Key-indexed hi
Counting hi+1 = i >1 (1)
Radix Sort
2
Workshop
• Hibbard proposed
hi = 2 i − 1 (2)
• Knuth proposed
h1 = 1
hi+1 = 3hi + 1 i >1 (3)
• Pratt proposed
Successive numbers of the form 2p 3q , p, q ∈ N (4)
28
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
29
Illustration
Selection Sort
Insertion Sort
Bubble Sort
result A E E E H L L L M O P R S S T X 30
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Radix Sort
Theorem 5
Workshop Shellsort does less than N(h − 1)(k − 1)/g comparisons to g-sort an array that is
h- and k-ordered, provided that h and k are relatively prime
31
Analysis (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Time complexity:
Merge Sort
best case ?
Quick Sort
Key-indexed
average case ?
Counting worst case ?
Radix Sort
32
Heap Sort
Heap Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Quick Sort • Max heap: A tree is heap-ordered if the key in each node is larger than or
Key-indexed equal to the keys in all of that node’s children (if any)
Counting
Radix Sort
• Min heap: A tree is heap-ordered if the key in each node is smaller than or
Workshop equal to the keys in all of that node’s children (if any)
Theorem 6
• Max heap: No node in a heap-ordered tree has a key larger than the key at
the root
• Min heap: No node in a heap-ordered tree has a key smaller than the key at
the root
34
Heap Representation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Array representation of a heap-ordered complete binary tree
Merge Sort
Quick Sort
a0
Key-indexed
Counting
a1 a2
Radix Sort
Workshop
a3 a4 a5 a6
a7 a8
Figure 1: Array {a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 } and complete binary tree
35
Heap Representation (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Note
Heap Sort
Array vs. Complete binary tree
Merge Sort
36
Top-down heapify
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
At the given node ai
Heap Sort
Merge Sort
• Exchange the key in the given node ai with the largest key among that
Quick Sort node’s children a2i+1 and a2i+2
Key-indexed • Move down to that child, and continuing down the tree until we reach the
Counting
Radix Sort
bottom or a point where no child has a larger key.
Workshop
37
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
38
Heap Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Build max-heap array: use heapify operation to convert an array a to a
Merge Sort max-heap array
• All elements in the range a n , ..., an−1 are leaf nodes.
n o
Quick Sort
2
Key-indexed
• Apply heapify operation for these elements a n −1 , ..., a0
n o
Counting
2
Radix Sort
Workshop
• Sort a max-heap array a
1. Swap the first and the last element
2. Remove the last element
If |a| > 1 then apply heapify operation for a0 and repeat actions 1-2
39
Heap Sort (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
40
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Key-indexed
Counting
• Time complexity:
best case ?
Radix Sort
Workshop
average case ?
worst case ?
• Space complexity:
• Stability:
41
Merge Sort
Top-Down Merge Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
43
Illustration
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Sorting the following array:
Heap Sort
Merge Sort
1 3 6 2 8 2 5 9
Quick Sort
Key-indexed
Counting
• The array will be divided into two subarrays as follows:
Radix Sort
Workshop 1 3 6 2 8 2 5 9
• Then, the subarrays will be sorted recursively as follows:
1 2 3 6 2 5 8 9
• Finally, the algorithm merges the sorted subarrays and creates the final sorted
array:
1 2 2 3 5 6 8 9
44
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
45
Implementation (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
46
Bottom-up Merge Sort
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
47
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
48
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Key-indexed
Counting
• Time complexity:
Radix Sort
best case ?
Workshop
average case ?
worst case ?
• Space complexity:
• Stability:
49
Quick Sort
Quick Sort
Selection Sort
Insertion Sort
Bubble Sort
1. chn 1 phn t pivot
Shell Sort
2. phân hoch a ra thành 2 phn a left và a right
Heap Sort Idea
Merge Sort Quicksort invented by C. A. R. Hoare in 1960 is a divide-and-conquer method for
Quick Sort sorting
Key-indexed
Counting 1. Partition an array a into two parts a left and a right such that ∀x ∈ a left and
Radix Sort ∀y ∈ a right then x ≤ y
Workshop
2. Sort the parts a left and a right independently
3. Join a left and a right to a = a left a right
51
Implementation
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Chia
Nhn xét: khi mng có kích thcvà
nh tr
thut toán insertion chy hiu qu hn
(vì k có quy)
52
Implementation (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
53
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
54
Analysis (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Time complexity:
Merge Sort
best case ?
Quick Sort
Key-indexed
average case ?
Counting worst case ?
Radix Sort
55
A lower bound for the worst case
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
Theorem 11
Merge Sort No compare-based sorting algorithm can guarantee to sort N items with fewer
Quick Sort than log2 (N!) ∼ N log2 N compares.
Key-indexed
Counting
Proof
Radix Sort
Comparison sorts can be viewed abstractly in terms of decision trees.
Workshop
0:1
1:2 0:2
56
Comparing Sorting Algorithms
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Performance characteristics of sorting algorithms
Merge Sort
algorithm stable? in-place? running time space
Quick Sort
Key-indexed
selection no yes N2 1
Counting insertion yes yes between N and N 2 1
Radix Sort shell no yes N log N?, N 6/5 ? 1
Workshop
merge yes no N log N N
quick no yes N log N log N
3-way quick no yes between N and N log N log N
heap no yes N log N 1
57
Key-indexed Counting
Assumptions about keys
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Assumption. Keys are integers between 0 and R − 1.
Heap Sort
Merge Sort
Implication. Can use key as an array index.
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
59
Key-indexed Counting
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
Problem. Sort an array a of N items whose keys are integers between 0 and
Merge Sort R − 1.
Quick Sort
// Compute frequency counts .
Key-indexed
Counting for (int i = 0; i < N; i++)
Radix Sort count[a[i]. key () + 1]++;
Workshop // Transform counts to indices .
for (int r = 0; r < R; r++)
count[r + 1] += count[r];
// Distribute the items .
for (int i = 0; i < N; i++)
aux[count[a[i]. key () ]++] = a[i];
// Copy back.
for (int i = 0; i < N; i++)
a[i] = aux[i];
60
Illustration
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Input an array a of 12 letters
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
61
Illustration (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Compute frequency counts
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
62
Illustration (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Transform counts to indices.
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
63
Illustration (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Distribute the data
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
64
Illustration (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Copy back
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
65
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Radix Sort
• Time complexity:
Workshop
best case ?
average case ?
worst case ?
• Space complexity:
• Stability:
66
Radix Sort
Radix Sort
Selection Sort
Insertion Sort
Bubble Sort
s nh phân 2
Shell Sort s thp phân 10
Heap Sort Concept 9 s thp lc phân 16
Merge Sort
68
Radix Sort (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Workshop Concept 10
A key is a radix-R number, with digits numbered from the right (starting at 0)
69
Radix Sort (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
There are two basic approaches to radix sorting:
Heap Sort
Merge Sort
• The first class of methods: They examine the digits in the keys in a
Quick Sort left-to-right order, working with the most significant digits first. These
Key-indexed methods are generally referred to as most-significant-digit (MSD) radix
Counting
sorts or top-down methods.
Radix Sort
Workshop
• The second class of methods: They examine the digits in the keys in a
right-to-left order, working with the least significant digits first. These
methods are generally referred to as least-significant-digit (LSD) radix sorts
or bottom-up methods.
70
LSD Implementation
Selection Sort
sp xp các ch s t hàng n v tr i
Insertion Sort
Bubble Sort
Shell Sort
71
Example 1
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
Problem Sort an array of integer numbers {170, 45, 75, 90, 802, 2,
Merge Sort 24, 66}
Quick Sort
• Rewrite the numbers in 3-digit format {170, 045, 075, 090, 802, 002,
Key-indexed
Counting 024, 066}
Radix Sort
Workshop
72
Example 1 (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
• Distribute the data into 10 bins on digit d = 0
Heap Sort
Merge Sort {170, 045, 075, 090, 802, 002, 024, 066}
Quick Sort bin 0: 170 090
Key-indexed
Counting
bin 1:
Radix Sort
bin 2: 802 002
Workshop bin 3:
bin 4: 024
bin 5: 045 075
bin 6: 066
bin 7:
bin 8:
bin 9:
• Join the bins
{170, 090, 802, 002, 024, 045, 075, 066}
73
Example 1 (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
• Distribute the data into 10 bins on digit d = 1
Heap Sort
Merge Sort {170, 090, 802, 002, 024, 045, 075, 066}
Quick Sort bin 0: 802, 002
Key-indexed
Counting
bin 1:
Radix Sort
bin 2: 024
Workshop bin 3:
bin 4: 045
bin 5:
bin 6: 066
bin 7: 170, 075
bin 8:
bin 9: 090
• Join the bins
{802, 002, 024, 045, 066, 170, 075, 090}
74
Example 1 (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
• Distribute the data into 10 bins on digit d = 2
Heap Sort
Merge Sort {802, 002, 024, 045, 066, 170, 075, 090}
Quick Sort bin 0: 002, 024, 045, 066, 075, 090
Key-indexed
Counting
bin 1: 170
Radix Sort
bin 2:
Workshop bin 3:
bin 4:
bin 5:
bin 6:
bin 7:
bin 8: 802
bin 9:
• Join the bins
{002, 024, 045, 066, 075, 090, 170, 802}
75
Example 1 (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
76
Example 2
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
Problem Sort 17 integer numbers
Merge Sort
{6, 7, 1, 3, 5, 2, 0, 4, 2, 1, 7, 2, 1, 3, 5, 2, 7}
Quick Sort
Key-indexed
Counting • Rewrite the numbers in 3-digit binary numbers
Radix Sort
110 111 001 011 101 010 000 100 010 001 111 010 001 011 101 010 111
Workshop
77
Example 2 (cont.)
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort 110 111 001 011 101 010 000 100 010 001 111 010 001 011 101 010 111
Merge Sort
Quick Sort ↓
Key-indexed
Counting 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
Radix Sort
Workshop
↓
000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
78
Analysis
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Time complexity:
Merge Sort
best case ?
Quick Sort
Key-indexed
average case ?
Counting worst case ?
Radix Sort
79
Workshop
� Quiz
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
1. What is a sorting operation?
Merge Sort .........................................................................
Quick Sort .........................................................................
Key-indexed .........................................................................
Counting
Radix Sort
2. What is an inversion?
Workshop .........................................................................
.........................................................................
.........................................................................
3. How selection sort sorts the sample array E A S Y Q U E S T I O N?
.........................................................................
.........................................................................
.........................................................................
81
Ï Exercises
Selection Sort
Insertion Sort
Bubble Sort
Shell Sort
Heap Sort
• Programming exercises in [Cormen, 2009, Sedgewick, 2002]
Merge Sort
Quick Sort
Key-indexed
Counting
Radix Sort
Workshop
82
References
Cormen, T. H. (2009).
Introduction to algorithms.
MIT press.
Sedgewick, R. (2002).
Algorithms in Java, Parts 1-4, volume 1.
Addison-Wesley Professional.
Walls and Mirrors (2014).
Data Abstraction And Problem Solving with C++.
Pearson.