Chapter 1.4_ Analysis of Sorting Algorithms-All
Chapter 1.4_ Analysis of Sorting Algorithms-All
Introduction to Algorithms
Part IV
● Algorithms (Part I)
● Mathematical preliminaries
○ Order of growth (Part I)
Contents ○
○
Summation (Part II)
Recurrence (Part II)
● Proof of correctness (Part III)
● Analysis of sorting algorithms
● Sorting in Linear time
2
Analysis of Sorting Algorithms
3
Sorting
One of the most common data-processing applications
One of these fields is designated as the "sort key" in which the records are ordered
4
Sorting
Input Output
Example: 2 5 6 1 12 10 Example: 1 2 5 6 10 12
5
Sorting
Types
1. Internal sort
○ All of the data are held in primary memory during the sorting process
○ Examples: Insertion, selection, heap, bubble, quick, shell sort
2. External sort
○ Uses primary memory for the data currently being sorted and secondary
storage for any data that does not fit in primary memory
○ Examples: merge sort
6
Sorting
Sort stability
Indicates that data with equal keys maintain their relative input order in the output
365 blue 119 purple 119 purple
● Insertion Sort ✔
Sorting ● Merge Sort ✔
● Heap Sort
8
Sorting algorithms
9
Quick sort vs merge sort
In merge sort, the divide step does hardly anything, and all the real work happens
in the combine step whereas in quick sort, the real work happens in the divide step.
10
Heap sort
● Is among the fastest sorting algorithms
11
Heap
● A nearly complete binary tree in which the root contains the largest (or
smallest) element in the tree.
● Is completely filled on all levels except possibly the lowest, which is filled from
the left up to a point.
12
Heap property
There are two kinds of binary heaps: max-heaps and min-heaps.
In a max-heap, the max-heap property is that for every node other than the root,
the value of a node is at most the value of its parent. Thus, the largest element in
a max-heap is stored at the root.
13
Heap
78
56 32
45 8 23
78 56 32 45 8 23
14
Heap sort
To implement the heap sort using a max-heap, we need two basic algorithms:
1. Max-heapify
Maintains the max-heap property by pushing the root down the tree until it is in
its correct position in the heap.
2. Build-max-heap
Produces a max-heap from an unordered input array.
15
Heap sort
16
Heap sort
Steps:
17
Heap sort
Example:
8 32 45 78 23 56
18
Heap sort
Convert the array into a max heap
8
8 32 45 78 23 56
[1]
32 45
[2] [3]
78 23 56
19
Heap sort
Convert the array into a max heap
8
8 32 45 78 23 56
[1]
Starting from the index, i, of the node just
32 45 above the leaf level, check if the tree starting
at i is a max-heap. If not, fix it (by reheap down)
[2] [3]
78 23 56
20
Heap sort
Convert the array into a max heap
8
8 32 45 78 23 56
[1]
32 45
78 23 56
21
Heap sort
Convert the array into a max heap
8
8 32 56 78 23 45
[1]
32 56
78 23 45
22
Heap sort
Convert the array into a max heap
8
8 32 56 78 23 45
[1]
32 56
[2] [3]
78 23 45
23
Heap sort
Convert the array into a max heap
8
8 32 56 78 23 45
[1]
32 56
[2] [3]
78 23 45
24
Heap sort
Convert the array into a max heap
8
8 32 56 78 23 45
[1]
32 56
78 23 45
25
Heap sort
Convert the array into a max heap
8
8 78 56 32 23 45
[1]
78 56
32 23 45
26
Heap sort
Convert the array into a max heap
8
8 78 56 32 23 45
[1]
78 56
[2] [3]
32 23 45
27
Heap sort
Convert the array into a max heap
8
8 78 56 32 23 45
[1]
78 56
32 23 45
28
Heap sort
Convert the array into a max heap
78
78 8 56 32 23 45
[1]
8 56
32 23 45
29
Heap sort
Convert the array into a max heap
78
78 8 56 32 23 45
[1]
8 56
32 23 45
30
Heap sort
Convert the array into a max heap
78
78 32 56 8 23 45
[1]
32 56
8 23 45
31
Heap sort
Convert the array into a max heap
78
78 32 56 8 23 45
[1]
32 56
[2] [3]
8 23 45
32
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
78
[1]
32 56
[2] [3]
8 23 45
33
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
78
[1]
32 56
[2] [3]
8 23 45
34
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
45
[1]
32 56
[2] [3]
8 23 78
35
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
56
[1]
32 45
[2] [3]
8 23 78
[4] [5]
36
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
56
[1]
32 45
[2] [3]
8 23 78
[4] [5]
37
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
56
[1]
32 45
[2] [3]
8 23 78
[4] [5]
38
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
23
[1]
32 45
[2] [3]
8 56 78
[4] [5]
39
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
45
[1]
32 23
[2] [3]
8 56 78
[4] [5]
40
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
45
[1]
32 23
[2] [3]
8 56 78
[4]
41
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
45
[1]
32 23
[2] [3]
8 56 78
[4]
42
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
[1]
32 23
[2] [3]
45 56 78
[4]
43
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
32
[1]
8 23
[2] [3]
45 56 78
44
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
32
[1]
8 23
[2] [3]
45 56 78
45
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
32
[1]
8 23
[2] [3]
45 56 78
46
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
23
[1]
8 32
[2] [3]
45 56 78
47
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
23
[1]
8 32
[2]
45 56 78
48
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
23
[1]
8 32
[2]
45 56 78
49
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
[1]
23 32
[2]
45 56 78
50
Swap the root of the heap with the
element at the end of the list. Decrement
Heap sort the heap size by 1 and readjust the heap.
[1]
23 32
45 56 78
51
Analysis of Max-Heapify
52
Analysis of Max-Heapify
Running time of lines 1 - 9 =
53
Analysis of Max-Heapify
Running time of lines 1 - 9 =
54
Analysis of Max-Heapify
Max-Heapify may need to be called all
the way down to the bottom level.
55
Recall
What is the maximum number of nodes on level i of a binary tree?
56
Analysis of Max-Heapify
Max-Heapify may need to be called all
the way down to the bottom level.
58
Analysis of Build-Max-Heap
Trivial Analysis: Each call to Max-Heapify
requires log n time, we make n/2 such calls ⇒ O(n
log n).
59
Analysis of Build-Max-Heap
Tighter Bound: When Max-Heapify is called, the
running time depends on how far an element
might shift down before the process terminates.
60
Analysis of build_heap
Tighter bound (contd.)
And so on.
61
Analysis of build_heap
Tighter bound (contd.)
62
Analysis of build_heap
Recall: The infinite geometric series, for any constant x < 1
63
Analysis of build_heap
When x = ½, we get
65
Analysis of heap sort
Running time of line 1 = O(n)
66
Stability
Algorithm Stable ?
Selection sort No
Heap sort No
Quick sort No
67
Sorting in linear time
68
Sorting algorithms ● Counting sort
that run in linear ● Radix sort
time ● Bucket sort
Readings
Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Chapter 8: Sorting in
Linear Time. In Introduction to algorithms. MIT press.
Andersson, A., Hagerup, T., Nilsson, S., & Raman, R. (1998). Sorting in linear time?.
Journal of Computer and System Sciences, 57(1), 74-93.
Comparison sorts
Sorting algorithms studied so far are comparison sorts, i.e. they are based on
comparisons of input elements.
● ai < aj,
● ai ≤ aj, If we assume all input elements are distinct, then
comparisons of the form ai = aj are useless.
● ai = aj,
Also, ai < aj, ai ≤ aj, ai ≥ aj, and ai > a are equivalent as they
● ai ≥ aj, yield identical information about the relative order of ai and aj.
● ai > aj
Since each of the n! permutations of the input appears as some leaf, we have
n! ≤ l
n! ≤ l ≤ 2h
∴ Any comparison sort algorithm requires Ω(n lg n) comparisons in the worst case.
Counting sort
● Assumes that each of the n input elements is an integer in the range from 0 to
k, for some integer k
● When k = O(n), the sort runs in Θ(n) time
● Determines, for each input element x, the number of elements less than x.
○ This is how it positions x in its place in the output array
○ If there are 17 elements smaller than x, then x will be assigned position 18
● To sort an array A[1..n], we need two additional arrays
○ B[1..n] holds the sorted output
○ C[1..k] stores the number of repetitions of number in A
Counting sort
Counting sort
Counting sort
Counting sort
Counting sort
Counting sort
Counting sort
Line 2 - 3 → Θ(k)
Line 4 - 5 → Θ(n)
Line 7 - 8 → Θ(k)
Line 10 - 12 → Θ(n)
Counting sort
● Is not a comparison sort
● Beats the lower bound of Ω(n lg n)
● Is stable
● Is often used as a subroutine in radix sort
Radix sort
● The idea of Radix Sort is to do digit by digit sort starting from least significant
digit to most significant digit.
● In order for radix sort to work correctly, the digit sorts must be stable.
● Radix sort uses counting sort as a subroutine to sort.
Radix sort
Each element in the n-element array A has d digits, where digit 1 is the lowest-order
digit and digit d is the highest-order digit.
Radix sort
When each digit is in the range 0 to k - 1 (so that it can take on k possible values),
and k is not too large, counting sort is the obvious choice.
For d digits Θ(dn+dk) ⇒ The total time for radix sort is Θ(d(n+k))
When d is constant and k = O(n), we can make radix sort run in linear time.
Bucket sort
● Assumes that the input is drawn from a uniform distribution over the interval
[0, 1)
● Divides the interval [0, 1) into n equal-sized subintervals, or buckets, and then
distributes the n input numbers into the buckets
● To produce the output, we sort numbers in each bucket, then go through
buckets in order
Bucket sort
Bucket sort