27
CS6045: Advanced Algorithms Sorting Algorithms

CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Embed Size (px)

DESCRIPTION

Heaps Max-heap property: Min-heap property:

Citation preview

Page 1: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

CS6045: Advanced Algorithms

Sorting Algorithms

Page 2: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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

Page 3: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Heaps

• Max-heap property:

• Min-heap property:

Page 4: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Maintain the Heap Property

• Running Time?

• O(log n)

Page 5: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is
Page 6: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Build a Heap

Page 7: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is
Page 8: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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)

Page 9: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Heapsort

Page 10: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is
Page 11: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is
Page 12: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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)

Page 13: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Min-Heap Operations 2

4

11

12

8

1310

6

97

3

2

4

3

12

8

1310

6

97

11

2

4

1112

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

Page 14: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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

Page 15: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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

Page 16: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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?

Page 17: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Decision Tree Model• Abstraction of any comparison sort• Counting only comparisons• Abstract everything else: such as control and data

movement

Page 18: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

• How many leaves on the decision tree?– >= n!

• What is the length of the longest path from root to leaf?– Depend on the algorithm

Page 19: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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! <= l <= 2h 2h >= n! h >= lg(n!) >= lg (n/e)n //Stirlling’s approximation = nlg(n/e) = nlgn – nlge = (n log n)

Page 20: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Non-comparison Sorts

• Counting Sort

Page 21: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is
Page 22: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Analysis

• O(n + k)

• How big a k is practical?

Page 23: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Radix Sort

Page 24: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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.

Page 25: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

Analysis

• O(n + k) per iteration• d iterations• O(d(n + k)) total

Page 26: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is

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

Page 27: CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is