Heapsort example Andrej Ivaskovic University of Cambridge February 1, 2015 Andrej Ivaskovic (University of Cambridge) Heapsort example February 1, 2015 1 / 25 Input Initial state of the heap We are given an array that needs to be sorted: QWERTYUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 2 / 25 Making the array satisfy the max-heap property heapify(6) QWERTY[U]IOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 3 / 25 Making the array satisfy the max-heap property heapify(6) QWERTY[U]IOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 3 / 25 Making the array satisfy the max-heap property heapify(5) QWERT[Y]UIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 4 / 25 Making the array satisfy the max-heap property heapify(5) QWERT[Y]UIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 4 / 25 Making the array satisfy the max-heap property heapify(4) QWER[T]YUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 5 / 25 Making the array satisfy the max-heap property heapify(4) QWER[T]YUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 5 / 25 Making the array satisfy the max-heap property heapify(3) QWE[R]TYUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 6 / 25 Making the array satisfy the max-heap property heapify(3) QWE[R]TYUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 6 / 25 Making the array satisfy the max-heap property heapify(2) QW[E]RTYUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 7 / 25 Making the array satisfy the max-heap property heapify(2) QW[E]RTYUIOPASDFG Q W E R I T O Andrej Ivaskovic (University of Cambridge) P Y A S Heapsort example U D F February 1, 2015 G 7 / 25 Making the array satisfy the max-heap property heapify(2) QW[Y]RTEUIOPASDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P E A S Heapsort example U D F February 1, 2015 G 7 / 25 Making the array satisfy the max-heap property heapify(2) QW[Y]RTEUIOPASDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P E A S Heapsort example U D F February 1, 2015 G 7 / 25 Making the array satisfy the max-heap property heapify(2) QW[Y]RTSUIOPAEDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 7 / 25 Making the array satisfy the max-heap property heapify(2) QW[Y]RTSUIOPAEDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 7 / 25 Making the array satisfy the max-heap property heapify(1) Q[W]YRTSUIOPAEDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 8 / 25 Making the array satisfy the max-heap property heapify(1) Q[W]YRTSUIOPAEDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 8 / 25 Making the array satisfy the max-heap property heapify(0) [Q]WYRTSUIOPAEDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 9 / 25 Making the array satisfy the max-heap property heapify(0) [Q]WYRTSUIOPAEDFG Q W Y R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 9 / 25 Making the array satisfy the max-heap property heapify(0) [Y]WQRTSUIOPAEDFG Y Q W R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 9 / 25 Making the array satisfy the max-heap property heapify(0) [Y]WQRTSUIOPAEDFG Y Q W R I T O Andrej Ivaskovic (University of Cambridge) P S A E Heapsort example U D F February 1, 2015 G 9 / 25 Making the array satisfy the max-heap property heapify(0) [Y]WURTSQIOPAEDFG Y W U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 G 9 / 25 Making the array satisfy the max-heap property heapify(0) [Y]WURTSQIOPAEDFG Y W U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 G 9 / 25 End of step 1 We have a heap! Now the array satisfies the binary max-heap property: YWURTSQIOPAEDFG Y W U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 G 10 / 25 Removal of elements from the heap We have the 14th element! [G]WURTSQIOPAEDFY G W U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! GWURTSQIOPAEDFY G W U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! WGURTSQIOPAEDFY W G U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! WGURTSQIOPAEDFY W G U R I T O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! WTURGSQIOPAEDFY W T U R I G O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! WTURGSQIOPAEDFY W T U R I G O Andrej Ivaskovic (University of Cambridge) P Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! WTURPSQIOGAEDFY W T U R I P O Andrej Ivaskovic (University of Cambridge) G Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 14th element! WTURPSQIOGAEDFY W T U R I P O Andrej Ivaskovic (University of Cambridge) G Q S A E Heapsort example D F February 1, 2015 Y 11 / 25 Removal of elements from the heap We have the 13th element! [F]TURPSQIOGAEDWY F T U R I P O Andrej Ivaskovic (University of Cambridge) G Q S A E Heapsort example D W February 1, 2015 Y 12 / 25 Removal of elements from the heap We have the 13th element! FTURPSQIOGAEDWY F T U R I P O Andrej Ivaskovic (University of Cambridge) G Q S A E Heapsort example D W February 1, 2015 Y 12 / 25 Removal of elements from the heap We have the 13th element! UTFRPSQIOGAEDWY U T F R I P O Andrej Ivaskovic (University of Cambridge) G Q S A E Heapsort example D W February 1, 2015 Y 12 / 25 Removal of elements from the heap We have the 13th element! UTFRPSQIOGAEDWY U T F R I P O Andrej Ivaskovic (University of Cambridge) G Q S A E Heapsort example D W February 1, 2015 Y 12 / 25 Removal of elements from the heap We have the 13th element! UTSRPFQIOGAEDWY U T S R I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example D W February 1, 2015 Y 12 / 25 Removal of elements from the heap We have the 13th element! UTSRPFQIOGAEDWY U T S R I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example D W February 1, 2015 Y 12 / 25 Removal of elements from the heap We have the 12th element! [D]TSRPFQIOGAEUWY D T S R I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! DTSRPFQIOGAEUWY D T S R I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! TDSRPFQIOGAEUWY T D S R I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! TDSRPFQIOGAEUWY T D S R I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! TRSDPFQIOGAEUWY T R S D I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! TRSDPFQIOGAEUWY T R S D I P O Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! TRSOPFQIDGAEUWY T R S O I P D Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 12th element! TRSOPFQIDGAEUWY T R S O I P D Andrej Ivaskovic (University of Cambridge) G Q F A E Heapsort example U W February 1, 2015 Y 13 / 25 Removal of elements from the heap We have the 11th element! [E]RSOPFQIDGATUWY E R S O I P D Andrej Ivaskovic (University of Cambridge) G Q F A T Heapsort example U W February 1, 2015 Y 14 / 25 Removal of elements from the heap We have the 11th element! ERSOPFQIDGATUWY E R S O I P D Andrej Ivaskovic (University of Cambridge) G Q F A T Heapsort example U W February 1, 2015 Y 14 / 25 Removal of elements from the heap We have the 11th element! SREOPFQIDGATUWY S R E O I P D Andrej Ivaskovic (University of Cambridge) G Q F A T Heapsort example U W February 1, 2015 Y 14 / 25 Removal of elements from the heap We have the 11th element! SREOPFQIDGATUWY S R E O I P D Andrej Ivaskovic (University of Cambridge) G Q F A T Heapsort example U W February 1, 2015 Y 14 / 25 Removal of elements from the heap We have the 11th element! SRQOPFEIDGATUWY S Q R O I P D Andrej Ivaskovic (University of Cambridge) G F A T Heapsort example E U W February 1, 2015 Y 14 / 25 Removal of elements from the heap We have the 11th element! SRQOPFEIDGATUWY S Q R O I P D Andrej Ivaskovic (University of Cambridge) G F A T Heapsort example E U W February 1, 2015 Y 14 / 25 Removal of elements from the heap We have the 10th element! [A]RQOPFEIDGSTUWY A Q R O I P D Andrej Ivaskovic (University of Cambridge) G F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! ARQOPFEIDGSTUWY A Q R O I P D Andrej Ivaskovic (University of Cambridge) G F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! RAQOPFEIDGSTUWY R Q A O I P D Andrej Ivaskovic (University of Cambridge) G F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! RAQOPFEIDGSTUWY R Q A O I P D Andrej Ivaskovic (University of Cambridge) G F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! RPQOAFEIDGSTUWY R Q P O I A D Andrej Ivaskovic (University of Cambridge) G F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! RPQOAFEIDGSTUWY R Q P O I A D Andrej Ivaskovic (University of Cambridge) G F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! RPQOGFEIDASTUWY R Q P O I G D Andrej Ivaskovic (University of Cambridge) A F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 10th element! RPQOGFEIDASTUWY R Q P O I G D Andrej Ivaskovic (University of Cambridge) A F S T Heapsort example E U W February 1, 2015 Y 15 / 25 Removal of elements from the heap We have the 9th element! [A]PQOGFEIDRSTUWY A Q P O I G D Andrej Ivaskovic (University of Cambridge) R F S T Heapsort example E U W February 1, 2015 Y 16 / 25 Removal of elements from the heap We have the 9th element! APQOGFEIDRSTUWY A Q P O I G D Andrej Ivaskovic (University of Cambridge) R F S T Heapsort example E U W February 1, 2015 Y 16 / 25 Removal of elements from the heap We have the 9th element! QPAOGFEIDRSTUWY Q P A O I G D Andrej Ivaskovic (University of Cambridge) R F S T Heapsort example E U W February 1, 2015 Y 16 / 25 Removal of elements from the heap We have the 9th element! QPAOGFEIDRSTUWY Q P A O I G D Andrej Ivaskovic (University of Cambridge) R F S T Heapsort example E U W February 1, 2015 Y 16 / 25 Removal of elements from the heap We have the 9th element! QPFOGAEIDRSTUWY Q P F O I G D Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 16 / 25 Removal of elements from the heap We have the 9th element! QPFOGAEIDRSTUWY Q P F O I G D Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 16 / 25 Removal of elements from the heap We have the 8th element! [D]PFOGAEIQRSTUWY D P F O I G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! DPFOGAEIQRSTUWY D P F O I G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! PDFOGAEIQRSTUWY P D F O I G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! PDFOGAEIQRSTUWY P D F O I G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! POFDGAEIQRSTUWY P O F D I G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! POFDGAEIQRSTUWY P O F D I G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! POFIGAEDQRSTUWY P O F I D G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 8th element! POFIGAEDQRSTUWY P O F I D G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 17 / 25 Removal of elements from the heap We have the 7th element! [D]OFIGAEPQRSTUWY D O F I P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 18 / 25 Removal of elements from the heap We have the 7th element! DOFIGAEPQRSTUWY D O F I P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 18 / 25 Removal of elements from the heap We have the 7th element! ODFIGAEPQRSTUWY O D F I P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 18 / 25 Removal of elements from the heap We have the 7th element! ODFIGAEPQRSTUWY O D F I P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 18 / 25 Removal of elements from the heap We have the 7th element! OIFDGAEPQRSTUWY O I F D P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 18 / 25 Removal of elements from the heap We have the 7th element! OIFDGAEPQRSTUWY O I F D P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example E U W February 1, 2015 Y 18 / 25 Removal of elements from the heap We have the 6th element! [E]IFDGAOPQRSTUWY E I F D P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example O U W February 1, 2015 Y 19 / 25 Removal of elements from the heap We have the 6th element! EIFDGAOPQRSTUWY E I F D P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example O U W February 1, 2015 Y 19 / 25 Removal of elements from the heap We have the 6th element! IEFDGAOPQRSTUWY I E F D P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example O U W February 1, 2015 Y 19 / 25 Removal of elements from the heap We have the 6th element! IEFDGAOPQRSTUWY I E F D P G Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example O U W February 1, 2015 Y 19 / 25 Removal of elements from the heap We have the 6th element! IGFDEAOPQRSTUWY I G F D P E Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example O U W February 1, 2015 Y 19 / 25 Removal of elements from the heap We have the 6th element! IGFDEAOPQRSTUWY I G F D P E Q Andrej Ivaskovic (University of Cambridge) R A S T Heapsort example O U W February 1, 2015 Y 19 / 25 Removal of elements from the heap We have the 5th element! [A]GFDEIOPQRSTUWY A G F D P E Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 20 / 25 Removal of elements from the heap We have the 5th element! AGFDEIOPQRSTUWY A G F D P E Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 20 / 25 Removal of elements from the heap We have the 5th element! GAFDEIOPQRSTUWY G A F D P E Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 20 / 25 Removal of elements from the heap We have the 5th element! GAFDEIOPQRSTUWY G A F D P E Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 20 / 25 Removal of elements from the heap We have the 5th element! GEFDAIOPQRSTUWY G E F D P A Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 20 / 25 Removal of elements from the heap We have the 5th element! GEFDAIOPQRSTUWY G E F D P A Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 20 / 25 Removal of elements from the heap We have the 4th element! [A]EFDGIOPQRSTUWY A E F D P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 21 / 25 Removal of elements from the heap We have the 4th element! AEFDGIOPQRSTUWY A E F D P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 21 / 25 Removal of elements from the heap We have the 4th element! FEADGIOPQRSTUWY F E A D P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 21 / 25 Removal of elements from the heap We have the 4th element! FEADGIOPQRSTUWY F E A D P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 21 / 25 Removal of elements from the heap We have the 3rd element! [D]EAFGIOPQRSTUWY D E A F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 22 / 25 Removal of elements from the heap We have the 3rd element! DEAFGIOPQRSTUWY D E A F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 22 / 25 Removal of elements from the heap We have the 3rd element! EDAFGIOPQRSTUWY E D A F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 22 / 25 Removal of elements from the heap We have the 3rd element! EDAFGIOPQRSTUWY E D A F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 22 / 25 Removal of elements from the heap We have the 2nd element! [A]DEFGIOPQRSTUWY A D E F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 23 / 25 Removal of elements from the heap We have the 2nd element! ADEFGIOPQRSTUWY A D E F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 23 / 25 Removal of elements from the heap We have the 2nd element! DAEFGIOPQRSTUWY D A E F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 23 / 25 Removal of elements from the heap We have the 2nd element! DAEFGIOPQRSTUWY D A E F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 23 / 25 Removal of elements from the heap We have the 1st element! [A]DEFGIOPQRSTUWY A D E F P G Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 24 / 25 Removal of elements from the heap We have the 1st element! ADEFGIOPQRSTUWY A D E G F P Q Andrej Ivaskovic (University of Cambridge) R I S T Heapsort example O U W February 1, 2015 Y 24 / 25 We are done! What we get in the end is sorted: ADEFGIOPQRSTUWY Andrej Ivaskovic (University of Cambridge) Heapsort example February 1, 2015 25 / 25
© Copyright 2026 Paperzz