Download presentation
Presentation is loading. Please wait.
Published byBrice Potter Modified over 9 years ago
1
CS6045: Advanced Algorithms Sorting Algorithms
2
Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is A[1] –Parent of –Left child of A[i] = A[2i] –Right child of A[i] = A[2i + 1] –Computing is fast with binary representation implementation
3
Heaps Max-heap property: Min-heap property:
4
Maintain the Heap Property Running Time? O(log n)
6
Build a Heap
8
Correctness Loop invariant: At start of every iteration of for loop, each node i+1, i+2, …, n is the root of a max-heap Analysis running time? O(n log n) Tighter bound? O(n)
9
Heapsort
12
Analysis BUILD-MAX-HEAP: O(n) for loop: n – 1 times Exchange elements: O(1) MAX-HEAPIFY: O(log n) Time: –O(n log n)
13
Min-Heap Operations 2 4 11 12 8 1310 6 97 3 2 4 3 12 8 1310 6 97 11 2 4 12 8 1310 6 97 3 Insert(S, x): O(height) = O(log n) Extract-min(S): return head, replace head key with the last, float down, O(log n) 2 6 11 12 8 1310 4 95 12 6 118 1310 4 95 4 6 118 1310 12 95 4 6 118 1310 5 912
14
Priority Queues Priority Queue –Maintains a dynamic set S of elements –Each element has a key --- an associated value Applications –job scheduling on shared computer –Dijkstra’s finding shortest paths in graphs –Prim’s algorithm for minimum spanning tree
15
Priority Queues Operations supported by priority queue –Insert(S, x) - inserts element with the pointer x –Minimum/Maximum(S) - returns element with the minimum key –Extract-Min/Max(S) - removes and returns minimum key –Increase/Decrease-Key(S,x,k) – increases/decreases the value of element x’s key to the new value k
16
Comparison Sorting The only operation that may be used to gain order information about a sequence is comparison of pairs of elements Insertion sort, merge sort, quicksort, heapsort Lower bound for comparison sorting?
17
Decision Tree Model Abstraction of any comparison sort Counting only comparisons Abstract everything else: such as control and data movement
18
How many leaves on the decision tree? –>= n! What is the length of the longest path from root to leaf? –Depend on the algorithm
19
Lower Bound for Comparison Sorting A lower bound on the heights of decision trees in the lower bound on the running time of any comparison sort algorithm (n log n) –n! = n! h >= lg(n!) >= lg (n/e) n //Stirlling’s approximation = nlg(n/e) = nlgn – nlge = (n log n)
20
Non-comparison Sorts Counting Sort
22
Analysis O(n + k) How big a k is practical?
23
Radix Sort
24
Correctness Induction on number of passes (i in pseudocode). Assume digits 1, 2, ……, i -1 are sorted. Show that a stable sort on digit i leaves digits 1, 2, ……, i sorted: –If 2 digits in position i are different, ordering by position i is correct, and positions 1, 2, ……, i - 1 are irrelevant. –If 2 digits in position i are equal, numbers are already in the right order (by inductive hypothesis). The stable sort on digit i leaves them in the right order.
25
Analysis O(n + k) per iteration d iterations O(d(n + k)) total
26
Bucket Sort Idea: –Divide [0,1) into n equal-sized buckets –Distribute the n input values into the buckets –Sort each bucket –Go through buckets in order, listing elements in each on
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.