Heapsort example - University of Cambridge...heapify(6) QWERTY[U]IOPASDFG Q W R I O T P A E Y S D U...

108
Heapsort example Andrej Ivaskovic University of Cambridge February 1, 2015 Andrej Ivaskovic (University of Cambridge) Heapsort example February 1, 2015 1 / 25

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...

Page 1: Heapsort example - University of Cambridge...heapify(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

Heapsort example

Andrej Ivaskovic

University of Cambridge

February 1, 2015

Andrej Ivaskovic (University of Cambridge) Heapsort example February 1, 2015 1 / 25

Page 2: Heapsort example - University of Cambridge...heapify(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

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

Page 3: Heapsort example - University of Cambridge...heapify(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

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

Page 4: Heapsort example - University of Cambridge...heapify(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

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

Page 5: Heapsort example - University of Cambridge...heapify(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

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

Page 6: Heapsort example - University of Cambridge...heapify(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

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

Page 7: Heapsort example - University of Cambridge...heapify(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

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

Page 8: Heapsort example - University of Cambridge...heapify(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

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

Page 9: Heapsort example - University of Cambridge...heapify(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

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

Page 10: Heapsort example - University of Cambridge...heapify(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

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

Page 11: Heapsort example - University of Cambridge...heapify(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

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

Page 12: Heapsort example - University of Cambridge...heapify(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

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

Page 13: Heapsort example - University of Cambridge...heapify(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

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

Page 14: Heapsort example - University of Cambridge...heapify(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

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

Page 15: Heapsort example - University of Cambridge...heapify(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

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

Page 16: Heapsort example - University of Cambridge...heapify(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

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

Page 17: Heapsort example - University of Cambridge...heapify(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

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

Page 18: Heapsort example - University of Cambridge...heapify(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

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

Page 19: Heapsort example - University of Cambridge...heapify(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

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

Page 20: Heapsort example - University of Cambridge...heapify(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

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

Page 21: Heapsort example - University of Cambridge...heapify(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

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

Page 22: Heapsort example - University of Cambridge...heapify(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

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

Page 23: Heapsort example - University of Cambridge...heapify(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

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

Page 24: Heapsort example - University of Cambridge...heapify(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

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

Page 25: Heapsort example - University of Cambridge...heapify(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

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

Page 26: Heapsort example - University of Cambridge...heapify(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

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

Page 27: Heapsort example - University of Cambridge...heapify(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

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

Page 28: Heapsort example - University of Cambridge...heapify(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

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

Page 29: Heapsort example - University of Cambridge...heapify(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

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

Page 30: Heapsort example - University of Cambridge...heapify(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

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

Page 31: Heapsort example - University of Cambridge...heapify(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

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

Page 32: Heapsort example - University of Cambridge...heapify(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

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

Page 33: Heapsort example - University of Cambridge...heapify(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

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

Page 34: Heapsort example - University of Cambridge...heapify(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

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

Page 35: Heapsort example - University of Cambridge...heapify(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

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

Page 36: Heapsort example - University of Cambridge...heapify(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

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

Page 37: Heapsort example - University of Cambridge...heapify(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

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

Page 38: Heapsort example - University of Cambridge...heapify(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

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

Page 39: Heapsort example - University of Cambridge...heapify(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

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

Page 40: Heapsort example - University of Cambridge...heapify(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

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

Page 41: Heapsort example - University of Cambridge...heapify(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

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

Page 42: Heapsort example - University of Cambridge...heapify(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

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

Page 43: Heapsort example - University of Cambridge...heapify(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

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

Page 44: Heapsort example - University of Cambridge...heapify(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

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

Page 45: Heapsort example - University of Cambridge...heapify(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

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

Page 46: Heapsort example - University of Cambridge...heapify(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

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

Page 47: Heapsort example - University of Cambridge...heapify(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

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

Page 48: Heapsort example - University of Cambridge...heapify(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

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

Page 49: Heapsort example - University of Cambridge...heapify(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

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

Page 50: Heapsort example - University of Cambridge...heapify(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

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

Page 51: Heapsort example - University of Cambridge...heapify(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

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

Page 52: Heapsort example - University of Cambridge...heapify(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

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

Page 53: Heapsort example - University of Cambridge...heapify(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

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

Page 54: Heapsort example - University of Cambridge...heapify(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

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

Page 55: Heapsort example - University of Cambridge...heapify(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

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

Page 56: Heapsort example - University of Cambridge...heapify(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

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

Page 57: Heapsort example - University of Cambridge...heapify(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

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

Page 58: Heapsort example - University of Cambridge...heapify(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

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

Page 59: Heapsort example - University of Cambridge...heapify(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

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

Page 60: Heapsort example - University of Cambridge...heapify(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

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

Page 61: Heapsort example - University of Cambridge...heapify(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

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

Page 62: Heapsort example - University of Cambridge...heapify(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

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

Page 63: Heapsort example - University of Cambridge...heapify(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

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

Page 64: Heapsort example - University of Cambridge...heapify(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

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

Page 65: Heapsort example - University of Cambridge...heapify(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

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

Page 66: Heapsort example - University of Cambridge...heapify(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

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

Page 67: Heapsort example - University of Cambridge...heapify(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

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

Page 68: Heapsort example - University of Cambridge...heapify(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

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

Page 69: Heapsort example - University of Cambridge...heapify(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

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

Page 70: Heapsort example - University of Cambridge...heapify(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

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

Page 71: Heapsort example - University of Cambridge...heapify(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

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

Page 72: Heapsort example - University of Cambridge...heapify(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

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

Page 73: Heapsort example - University of Cambridge...heapify(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

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

Page 74: Heapsort example - University of Cambridge...heapify(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

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

Page 75: Heapsort example - University of Cambridge...heapify(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

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

Page 76: Heapsort example - University of Cambridge...heapify(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

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

Page 77: Heapsort example - University of Cambridge...heapify(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

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

Page 78: Heapsort example - University of Cambridge...heapify(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

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

Page 79: Heapsort example - University of Cambridge...heapify(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

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

Page 80: Heapsort example - University of Cambridge...heapify(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

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

Page 81: Heapsort example - University of Cambridge...heapify(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

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

Page 82: Heapsort example - University of Cambridge...heapify(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

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

Page 83: Heapsort example - University of Cambridge...heapify(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

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

Page 84: Heapsort example - University of Cambridge...heapify(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

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

Page 85: Heapsort example - University of Cambridge...heapify(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

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

Page 86: Heapsort example - University of Cambridge...heapify(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

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

Page 87: Heapsort example - University of Cambridge...heapify(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

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

Page 88: Heapsort example - University of Cambridge...heapify(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

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

Page 89: Heapsort example - University of Cambridge...heapify(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

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

Page 90: Heapsort example - University of Cambridge...heapify(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

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

Page 91: Heapsort example - University of Cambridge...heapify(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

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

Page 92: Heapsort example - University of Cambridge...heapify(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

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

Page 93: Heapsort example - University of Cambridge...heapify(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

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

Page 94: Heapsort example - University of Cambridge...heapify(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

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

Page 95: Heapsort example - University of Cambridge...heapify(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

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

Page 96: Heapsort example - University of Cambridge...heapify(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

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

Page 97: Heapsort example - University of Cambridge...heapify(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

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

Page 98: Heapsort example - University of Cambridge...heapify(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

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

Page 99: Heapsort example - University of Cambridge...heapify(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

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

Page 100: Heapsort example - University of Cambridge...heapify(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

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

Page 101: Heapsort example - University of Cambridge...heapify(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

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

Page 102: Heapsort example - University of Cambridge...heapify(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

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

Page 103: Heapsort example - University of Cambridge...heapify(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

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

Page 104: Heapsort example - University of Cambridge...heapify(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

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

Page 105: Heapsort example - University of Cambridge...heapify(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

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

Page 106: Heapsort example - University of Cambridge...heapify(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

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

Page 107: Heapsort example - University of Cambridge...heapify(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

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

Page 108: Heapsort example - University of Cambridge...heapify(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

We are done!

What we get in the end is sorted:

ADEFGIOPQRSTUWY

Andrej Ivaskovic (University of Cambridge) Heapsort example February 1, 2015 25 / 25