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