AdvancedSorting

# Last how many comparisons to find out 6 tournament

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: mbers the champion – Who is second, third, or …. last? – How many comparisons to find out? 6 Tournament Sort: Champion P M H P M P CHAMPI O ON 7 Tournament Sort: Second P M H P M P CHAMPI O ON 8 Tournament Sort: Second O M H O M CHAM I O ON 9 Tournament Sort: Third N M H N M I N CHAM 10 Tournament Sort: Fourth M M H I M CHAM 11 Tournament Sort: Complexity Can be a stable sort ! Memory: O(n) complexity due to the complexity tournament tournament tree ! Finding the champion takes O(n) comparisons comparisons ! Finding the order of the rest of the Finding integers integers takes O(n log n) comparisons log ! Overall time complexity: O(n log n) log ! How to reduce memory requirement? 12 ! Heapsort ! ! ! ! ! ! Use Use a descending heap, a tree implemented using array array Also Also called max heap, descending partially ordered ordered tree,… A max heap is an almost complete binary tree max where the value of each child is equal to or less than than the value of its parent Ordering Ordering in a heap is similar in spirit to the ordering ordering in a tournament tree Heap offers efficient insert and remove operations Array implementation of heap allows in-place sort in13 Heapsort ! ! Heaps Heaps as efficient implementation of the ADT priority queue (see Queues, Lists, and Trees – lectures lectures 8, 9, and 10) Selection Selection sort that implements the descending priority priority queue with a descending heap Initialize (PQueue) // descending for for i ← 0 to n-1 // insert r[i] with priority k[i] Insert(PQueue, r[i], k[i]) for for i ← n-1 downto 0 r[i] r[i] ← Remove(PQueue) Selection phase...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online