Lecture 2 Sorting Sorting Problem e g Insertion
Lecture 2 Sorting
Sorting Problem e. g. , Insertion Sort, Merge Sort
Efficiency • Running time from receiving the input to producing the output. Running time Insertion Sort Merge Sort
Heapsort • • Heap, a data structure Max-Heapify procedure Building a max-heap Heapsort
Quiz Sample • Is array a data structure?
Is array a data structure? • No! • A data structure is a data organization associated with a set of operations (standard algorithms) for efficiently using the data. • 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.
A Data Structure Heap • A heap is a nearly complete binary tree which can be easily implemented on an 1 array. 6 2 3 5 3 4 2 5 4 6 1 6 5 3 2 4 1
Nearly complete binary tree • Every level except bottom is complete. • On the bottom, nodes are placed as left as possible. 1 1 6 6 2 3 5 4 2 6 1 No! 4 2 3 5 3 4 2 5 4 Yes! 6 1
Algorithms associated with Heap
Two Special Heaps • Max-Heap • Min-Heap
Max-Heap
Min-Heap
Algorithms associated with Max-Heap • Three algorithms associated with heap. • In addition, it is associated with two more algorithms: Max-Heapify and Build-Max. 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”.
14 4 14 2 4 7 8 1 2 14 8 2 7 4 1 7 8 1
Running time
Building a Heap e. g. , 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
4 1 2 14 3 16 8 Proof. 7 9 10
4 1 2 14 3 9 16 8 7 7 10
4 1 2 14 3 16 8 7 9 10
4 1 2 14 3 16 8 7 9 10
Building a Max-Heap e. g. , 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
4 1 2 14 3 16 8 7 9 10
4 1 2 14 3 16 8 7 9 10
4 1 16 14 2 3 8 7 9 10
4 1 14 2 3 16 8 7 9 10
4 1 14 2 10 16 8 7 9 3
4 1 14 2 10 16 8 7 9 3
4 16 14 2 10 1 8 7 9 3
4 16 14 2 10 7 8 1 9 3
16 4 14 2 10 7 8 1 9 3
16 14 4 2 10 7 8 1 9 3
16 14 7 8 2 10 4 1 9 3
Analysis
16 14 7 8 2 10 4 1 9 3
Running time
Heapsort
Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7. Build a max-heap 16 14 7 8 2 10 4 9 3 1 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.
1 14 7 8 2 10 4 16 9 3
1 14 7 8 2 10 4 9 3 16 1, 14, 10, 8, 7, 9, 3, 2, 4, 16.
14 8 7 4 2 10 1 16 9 3
14 8 7 4 2 10 1 9 3 16 14, 8, 10, 4, 7, 9, 3, 2, 1, 16.
1 8 7 4 2 10 14 16 9 3
1 8 7 4 2 10 14 9 3 16 1, 8, 10, 4, 7, 9, 3, 2, 14, 16.
10 8 7 4 2 9 14 16 1 3
10 8 7 4 2 9 14 1 3 16 10, 8, 9, 4, 7, 1, 3, 2, 14, 16.
2 8 7 4 10 9 14 16 1 3
2 8 7 4 10 9 14 1 3 16 2, 8, 9, 4, 7, 1, 3, 10, 14, 16.
9 8 7 4 10 3 14 16 1 2
9 8 7 4 10 3 14 1 2 16 9, 8, 3, 4, 7, 1, 2, 10, 14, 16.
2 8 7 4 10 3 14 16 1 9
8 7 2 4 10 3 14 16 1 9
1 7 2 4 10 3 14 16 8 9
7 4 2 1 10 3 14 16 8 9
2 4 7 1 10 3 14 16 8 9
4 2 7 1 10 3 14 16 8 9
1 2 4 10 3 7 14 16 8 9
3 2 4 10 1 7 14 16 8 9
1 2 4 10 3 7 14 16 8 9
2 1 4 10 3 7 14 16 8 9
1 2 4 10 3 7 14 16 8 9
Running Time O(n) O(lg 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
Randomized Quicksort
What we learnt in this lecture? • What is heap, max-heap and min-heap? • Heapsort and Quicksort. • What is expected running time?
- Slides: 79