16
9/23/08 COT 5407 1 Page 146, CLRS QuickSort

QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 1

Page 146, CLRS

QuickSort

Page 2: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 2

Analysis of QuickSort

• Average case

– T(n) <= 2T(n/2) + O(n)

– T(n) = O(n log n)

• Worst case

– T(n) = T(n-1) + O(n)

– T(n) = O(n2)

• “IN PLACE” sorting algorithm

– Which sorting algorithm is not an “IN PLACE” sorting algorithm?

Page 3: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 3

Page 4: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 4

Sorting Algorithms

• SelectionSort

• InsertionSort

• BubbleSort

• ShakerSort

• QuickSort

• MergeSort

• HeapSort

• Bucket & Radix Sort

• Counting Sort

Page 5: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 5

HeapSort

• First convert array into a heap (BUILD-MAX-HEAP, p133)

• Then convert heap into sorted array (HEAPSORT, p136)

Page 6: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 6

Storing binary trees as arrays

20 7 38 4 16 37 43

Page 7: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 7

Heaps (Max-Heap)

43 16 38 4 7 37 20

20 2 3 6 143 16 38 4 7 37 30

HEAP represents a binary tree stored as an array

such that:

• Tree is filled on all levels except last

• Last level is filled from left to right

• Left & right child of i are in locations 2i and 2i+1

• HEAP PROPERTY:

Parent value is at least as large as child’s value

Page 8: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 8

HeapSort: Part 1

p130

O(height of

node in

location i) =

O(log(size of

subtree))

Page 9: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 9

HeapSort: Part 2

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

Page 10: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 10

HeapSort: Part 2

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

Page 11: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 11

Build-Max-HeapAnalysis

Page 12: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 12

Visualizing Algorithms 1

A

B

Position

Value

Unsorted Sorted

What algorithms are A and B?

Page 13: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 13

Visualizing Algorithms 2Position

Value

Unsorted Sorted

Page 14: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 14

Visualizing Comparisons 3

Page 15: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 15

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 16: QuickSort - School of Computing and Information Sciencesusers.cis.fiu.edu/~giri/teach/5407/F08/Lec7.pdf · 9/23/08 COT 5407 4 Sorting Algorithms • SelectionSort • InsertionSort

9/23/08 COT 5407 16

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?