notes_COSqueues

9 s p a x r l o e e x t e m s x l l r r x s exch1

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int (heap-ordered) sink(2, 11) S M M R L P O E O T sink(1, 11) exch(1, 10) sink(1, 9) S T O R sink(3, 11) M O M A X A S E P E E P E E O starting point (heap-ordered P X L R 30 exch(1, 11) sink(1, 10) O P M E E S L P R sink(4, 11) R T M 11 S exch(1, 11) T sink(1, 10) S T sortdown A 3 5 7 9 6 X 10A 11) P M 2S X E 11 T X sink(a, k, N); T S R E E E L R E L P M starting point (arbitrary order) A L for (int k = N/2; Mk >= O k--) 1; P E T 11 10 3 O E E E O 3 11 9 S P 5 8E Psink(5, O M 7 T M L O T X O exch(1, 2) P A pass. Build heap using bottom-up method. sink(1, 1) X M S 2 T E 5 E X M S 8 A A E A 4 E so heap construction P O R O Heapsort: heap construction X T T S R T A E A E M E 7 X EE S R 6 X 11 L X P O A A sink(5, 11) M Heapsort: constructing (left) and sorting down (right) a heap O P R S T X 5 X exch(1, 7) exch(1,sink(1, 6) 2) A sink(1, 1) M E E O result (heap-ordered) E M S E T P E T R E M 5 S R R 3 E A T Heapsort: constructing (left) and sorting down (right) a heap 1 O A O L S S A A A A A E R P A A X E O L R M S X L R E L L T exch(1, 8) First exch(1,sink(1, 7) 3) E sink(1, 2) O E S E M A A A P X O X X R R E X exch(1, 7) sink(1, 6) T P L L M E A M E L O exch(1, 9) exch(1,sink(1, 8) 4) E sink(1, 3) P X T S E E O result (heap-ordered) R M M S E T O S R E O T A R L R P X E O M P O S L 29 S R exch(1, 8) 11) sink(1, sink(1, 7) A T P M M E E P M array in sorted order A R 11) exch(1, 9) sink(1, 8) T S P L 1 S R S S E X O A 11 L L 8 E A 7 A P 4 X SM TE XE R PR S 1 E S L O X T S P O exch(1, 5) 2 sink(1, O 4) E OE A S E X E E X T S result (sorted) P T O 3R X 6 R O P E E A R T M exch(1, 10) exch(1,sink(1, 9) 5) sink(1, 4) 1S A T E E O starting point (heap-ordered) L E E E E X T starting point (heap-ordered) L R e 6) exch(1, xch(1, 11) sink(1, 10) sink(1, 5) AP A XO R S L M M S array in arbitrary order L A P P O S R T T AP RE S L M L 2 X O XO 7 A S X sink(3, 11) exch(1, 10) sink(1, 9) O Heapsort: constructing (left) and sorting down (right) a heap sink(4, 11) RE 11 sink(4, 11) exch(1, 11) sink(1, 10) sorted result R E (in place) L A P R M 10 T LP M RM O S M O T T L 6 E M E L P X T S starting point (arbitrary order) sortdown M R A E A X E L P MR X T S starting point (arbitrary order) S 5 9 we assume array entries are indexed 1 to N exch(1, 6) sink(1, 5) X E O T 11 exch(1, 11) sink(5, 7) sink(1, 6) X L 5 10 sortdown 3 exch(1, 2) 11) sink(5, sink(1, 1) P S A O O build a 3 R E max-heap O M T 1 Heap construction. Build max heap using bottom-up method. E...
View Full Document

This document was uploaded on 02/20/2014 for the course COS 226 at Princeton.

Ask a homework question - tutors are online