HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4...

17
9/25/08 COT 5407 1 HeapSort: Part 1 p130 O(height of node in location i) = O(log(size of subtree))

Transcript of HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4...

Page 1: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 1

HeapSort: Part 1

p130

O(height of

node in

location i) =

O(log(size of

subtree))

Page 2: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 2

HeapSort: Part 2

O(log n)Total:O(nlog n)

Page 3: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 3

Build-Max-HeapAnalysis

Page 4: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 4

Visualizing Algorithms 1

A

B

Position

Value

Unsorted Sorted

What algorithms are A and B?

Page 5: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 5

Visualizing Algorithms 2Position

Value

Unsorted Sorted

Page 6: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 6

Visualizing Comparisons 3

Page 7: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 7

Animations

• http://cg.scs.carleton.ca/~morin/misc/sortalg/

• http://home.westman.wave.ca/~rhenry/sort/

– time complexities on best, worst and average case

• http://vision.bc.edu/~dmartin/teaching/sorting/anim-

html/quick3.html

– runs on almost sorted, reverse, random, and unique inputs; shows

code with invariants

• http://www.brian-borowski.com/Sorting/

– comparisons, movements & stepwise animations with user data

• http://maven.smith.edu/~thiebaut/java/sort/demo.html

– comparisons & data movements and step by step execution

Page 8: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 8

Problems to think about!

• What is the least number of comparisons you need to sort a

list of 3 elements? 4 elements? 5 elements?

• How to arrange a tennis tournament in order to find the

tournament champion with the least number of matches?

How many tennis matches are needed?

Page 9: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 9

Sorting Algorithms

• SelectionSort

• InsertionSort

• BubbleSort

• ShakerSort

• QuickSort

• MergeSort

• HeapSort

• Bucket & Radix Sort

• Counting Sort

Page 10: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 10

Upper and Lower Bounds• Define an upper bound on the time complexity of a problem.

The upper bound on the time complexity of a problem is T(n)if an algorithm that solves the problem with timecomplexity O(T(n)).

• Clearly upper bound on the time complexity for sorting isO(n log n).

• Define a lower bound on the time complexity of a problem.The lower bound on the time complexity of a problem is T(n)if algorithms that solve the problem, their timecomplexity is (T(n)).

• It can be proved that the upper bound is tight! In otherwords, it can be mathematically proved that the lower boundfor sorting is (n log n).

Page 11: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 11

Bucket Sort• N values in the range [a..a+m-1]

• For e.g., sort a list of 50 scores in the range [0..9].

• Algorithm

– Make m buckets [a..a+m-1]

– As you read elements throw into appropriate bucket

– Output contents of buckets [0..m] in that order

• Time O(N+m)

Page 12: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 12

Stable Sort

• A sort is stable if equal elements appear in the same order

in both the input and the output.

• Which sorts are stable? Homework!

Page 13: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 13

Radix Sort

553

027

633

937

153

753

953

027

937

553

633

153

753

953

Algorithmfor i = 1 to d do

sort array A on digit i using any sorting algorithm

Time Complexity: O((N+m) + (N+m2) + …+ (N+md))

937

027

553

153

753

953

633

938

027

953

753

553

153

633

Space Complexity: O(md)

Page 14: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 14

Radix Sort

553

027

634

938

756

754

923

938

923

756

754

634

553

027

756

754

553

938

634

923

027

938

027

756

754

634

553

923

Algorithm

for i = 1 to d do

sort array A on digit i using a stable sort algorithm

Time Complexity: O((n+m)d)

Page 15: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 15

Counting Sort

87654321

30320352

103202

543210

877422

543210

Initial Array

Counts

Cumulative

Counts

Page 16: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 16

External Sorting Methods• Assumptions:

– data is too large to be held in main memory;

– data is read or written in blocks;

– 1 or more external devices available for sorting

• Sorting in main memory is cheap or free

• Read/write costs are the dominant cost

• Wide variety of storage types and costs

• No single strategy works for all cases

Page 17: HeapSort: Part 1giri/teach/5407/F08/Lec8.pdf · Build-Max-Heap Analysis. 9/25/08 COT 5407 4 Visualizing Algorithms 1 A B ... sort array A on digit i using any sorting algorithm Time

9/25/08 COT 5407 17

External Merge Sort

• Initial distribution pass

• Several multi-way merging passesASORTINGANDMERGINGEXAMPLEWITHFORTYFIVERECORDS.$=========================================================================================================

AOS.DMN.AEX.FHT.ERV.$

IRT.EGR.LMP.ORT.CEO.$

AGN.GIN.EIW.FIY.DRS.$=========================================================================================================

AAGINORST.FFHIORTTY.$

DEGGIMNNR.CDEEORRSV.$

AEEILMPWX.$=========================================================================================================

AAADEEEGGGIIILMMNNNOPRRSTWX.$

CDEEFFHIOORRRSTTVY.$=========================================================================================================

AAACDDEEEEEFFGGGHIIIILMMNNNOOOPRRRRRSSTTTWXY.$=========================================================================================================

With 2P external devices

Space for M records in main memory

Sorting N records needs

1 + logP(N/M) passes