Lecture 2 Sorting
Sorting Problem Insertion Sort, Merge Sort e.g.,
Efficiency Running time from receiving the input to producing the output. Insertion Sort Merge Sort Running time
Is array a data structure?
No! A data structure is a standard part in construction of algorithms. What data structures do you know on array?
Is array a data structure? No! A data structure is a standard part in construction of algorithms. What data structures do you know on array? Stack, queue, list, …, heap.
Heapsort Heap, a data structure Max-Heapify procedure Building a heap Heapsort
A Data Structure Heap A heap is an array object that can be viewed as a nearly complete binary tree
Max-Heap
Min-Heap
Max-Heapify Max-Heapify(A,i) is a subroutine. When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the max-heap property. Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-heap by letting A[i] “float down”.
Running time
Building a Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
Proof.
Building a Max-Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
Analysis
Running time
Heapsort
Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7. Build a max-heap 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.
, 14, 10, 8, 7, 9, 3, 2, 4, 16.
, 8, 10, 4, 7, 9, 3, 2, 1, 16.
, 8, 10, 4, 7, 9, 3, 2, 14, 16.
, 8, 9, 4, 7, 1, 3, 2, 14, 16.
, 8, 9, 4, 7, 1, 3, 10, 14, 16.
, 8, 3, 4, 7, 1, 2, 10, 14, 16.
Running Time O(lg n) O(n)
Quicksort Worst-case running time Expected running time The best practical choice (why?)
Divide and Conquer Divide the problem into subproblems. Conquer the subproblems by solving them recursively. Combine the solutions to subproblems into the solution for original problem.
Idea of Quicksort
Example
Quicksort
How to find such a partition? Such a partition may not exist. e.g., 5, 4, 3, 2, 1. Hence, we may need to make such a partition. Take a A[i]. Classify other A[j] by comparing it with A[i].
Expected Partition
Expected Running Time
Randomized Quicksort
What we learnt in this lecture? What is heap, max-heap and min-heap? Heapsort and Quicksort. What is expected running time?