Heapify & Heap sort - Information Retrieval Lab -...

40
Heapify & Heap sort Alfan F. Wicaksono

Transcript of Heapify & Heap sort - Information Retrieval Lab -...

Page 1: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heapify & Heap sort

Alfan F. Wicaksono

Page 2: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heapify

• Proses konversi sebuah array (arbitrary) menjadi sebuah min/max heap.

Page 3: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heapify

• Algoritma yang efisien bekerja dengan prinsip bottom-up, dan melakukan percolate down berkali-kali hingga root.

• Proses dimulai dari node bukan daun yang ada di level paling bawah – paling kanan. Lalu, bergerak secara reversed level order.

• Kompleksitas algoritma ini adalah O(n), dimana nadalah ukuran heap.

Page 4: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heapify

heapify(A):

A.heapSize = A.length

for i = floor(n/2)-1 downto 0:

percolateDown(A, i)

n : banyak elemen di array/heap size

Page 5: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 25 1 5 40 4 2

0 1 2 3 4 5 6 7 8

20

15 10

25 1 5 40

4 2

Page 6: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 25 1 5 40 4 2

0 1 2 3 4 5 6 7 8

20

15 10

25 1 5 40

4 2Dimulai dari floor(9/2) = 3

Page 7: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 25 1 5 40 4 2

0 1 2 3 4 5 6 7 8

20

15 10

25 1 5 40

4 2Percolate down !

Page 8: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 2 1 5 40 4 25

0 1 2 3 4 5 6 7 8

20

15 10

2 1 5 40

4 25Percolate down !

Page 9: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 2 1 5 40 4 25

0 1 2 3 4 5 6 7 8

20

15 10

2 1 5 40

4 25

Page 10: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 2 1 5 40 4 25

0 1 2 3 4 5 6 7 8

20

15 10

2 1 5 40

4 25Percolate down !

Page 11: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 5 2 1 10 40 4 25

0 1 2 3 4 5 6 7 8

20

15 5

2 1 10 40

4 25Percolate down !

Page 12: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 5 2 1 10 40 4 25

0 1 2 3 4 5 6 7 8

20

15 5

2 1 10 40

4 25

Page 13: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 5 2 1 10 40 4 25

0 1 2 3 4 5 6 7 8

20

15 5

2 1 10 40

4 25Percolate down !

Page 14: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 1 5 2 15 10 40 4 25

0 1 2 3 4 5 6 7 8

20

1 5

2 15 10 40

4 25Percolate down !

Page 15: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 1 5 2 15 10 40 4 25

0 1 2 3 4 5 6 7 8

20

1 5

2 15 10 40

4 25

Page 16: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 1 5 2 15 10 40 4 25

0 1 2 3 4 5 6 7 8

20

1 5

2 15 10 40

4 25Percolate down !

Page 17: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

1 2 5 4 15 10 40 20 25

0 1 2 3 4 5 6 7 8

1

2 5

4 15 10 40

20 25Percolate down !

Page 18: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

1 2 5 4 15 10 40 20 25

0 1 2 3 4 5 6 7 8

1

2 5

4 15 10 40

20 25Min-Heap terbentuk !

Page 19: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heap Sort

Input: arbitrary array

• Bangun heap dengan array tersebut (heapify)

• Lakukan removeMin/Max terus-menerushingga array terurut

• Kompleksitas Waktu O(n log n)

Page 20: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heap Sort

• Gunakan Min-Heap untuk Descending Sorting

• Gunakan Max-Heap untuk Ascending Sorting

Page 21: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

Heap Sort

• Urutkan array berikut (descending) !

20 15 10 25 1 5 40 4 2

0 1 2 3 4 5 6 7 8

*Gunakan Min-Heap*Array yang sama dengan simulasi heapify di slide sebelumnya !

Page 22: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

• #1 lakukan heapify !

20 15 10 25 1 5 40 4 2

0 1 2 3 4 5 6 7 8

*Gunakan Min-Heap*Array yang sama dengan simulasi heapify di slide sebelumnya !

Page 23: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

20 15 10 25 1 5 40 4 2

0 1 2 3 4 5 6 7 8

20

15 10

25 1 5 40

4 2

Kondisi Sebelum heapify !

Page 24: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

1 2 5 4 15 10 40 20 25

0 1 2 3 4 5 6 7 8

1

2 5

4 15 10 40

20 25

Setelah dilakukan Heapify !Langkah-langkah sama dengan yang sudahdisampaikan di slide-slide sebelumnya !

Page 25: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

2 5 4 15 10 40 20 25

0 1 2 3 4 5 6 7 8

2 5

4 15 10 40

20 25

• deleteMin() atau top element 1• Simpan 1 di temporary space• Sebuah “lubang” terbentuk di posisi top

1

Page 26: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

2 5 4 15 10 40 20 25

0 1 2 3 4 5 6 7 8

2 5

4 15 10 40

20 25

• Tukar 1 dengan elemen terakhir padaMin-Heap

• Bagian akhir array sudah BUKAN bagianHEAP, tetapi sudah menjadi bagianSORTED ARRAY !

1

Page 27: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

25 2 5 4 15 10 40 20 1

0 1 2 3 4 5 6 7 8

25

2 5

4 15 10 40

20

• Tukar 1 dengan elemen terakhir padaMin-Heap

• Bagian akhir array sudah BUKAN bagianHEAP, tetapi sudah menjadi bagianSORTED ARRAY !

Page 28: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

25 2 5 4 15 10 40 20 1

0 1 2 3 4 5 6 7 8

25

2 5

4 15 10 40

20

• Percolate down dari root

Page 29: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

2 4 5 20 15 10 40 25 1

0 1 2 3 4 5 6 7 8

2

4 5

20 15 10 40

25

• Percolate down dari root

Page 30: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

2 4 5 20 15 10 40 25 1

0 1 2 3 4 5 6 7 8

2

4 5

20 15 10 40

25

• Percolate down dari root

Page 31: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

4 5 20 15 10 40 25 1

0 1 2 3 4 5 6 7 8

4 5

20 15 10 40

25

• deleteMin() atau top element 2• Simpan 2 di temporary space• Sebuah “lubang” terbentuk di posisi top

2

Page 32: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

4 5 20 15 10 40 25 1

0 1 2 3 4 5 6 7 8

4 5

20 15 10 40

25

2

• Tukar 2 dengan elemen terakhir padaMin-Heap

• Bagian akhir array sudah BUKAN bagianHEAP, tetapi sudah menjadi bagianSORTED ARRAY !

Page 33: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

25 4 5 20 15 10 40 2 1

0 1 2 3 4 5 6 7 8

25

4 5

20 15 10 40

• Tukar 2 dengan elemen terakhir padaMin-Heap

• Bagian akhir array sudah BUKAN bagianHEAP, tetapi sudah menjadi bagianSORTED ARRAY !

Page 34: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

25 4 5 20 15 10 40 2 1

0 1 2 3 4 5 6 7 8

25

4 5

20 15 10 40

• Percolate down dari root

Page 35: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

4 15 5 20 25 10 40 2 1

0 1 2 3 4 5 6 7 8

4

15 5

20 25 10 40

• Percolate down dari root

Page 36: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

4 15 5 20 25 10 40 2 1

0 1 2 3 4 5 6 7 8

4

15 5

20 25 10 40

• Percolate down dari root

Page 37: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

15 5 20 25 10 40 2 1

0 1 2 3 4 5 6 7 8

15 5

20 25 10 40

• deleteMin() atau top element 4• Simpan 4 di temporary space• Sebuah “lubang” terbentuk di posisi top

4

Page 38: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

15 5 20 25 10 40 2 1

0 1 2 3 4 5 6 7 8

15 5

20 25 10 40

4

• Tukar 4 dengan elemen terakhir padaMin-Heap

• Bagian akhir array sudah BUKAN bagianHEAP, tetapi sudah menjadi bagianSORTED ARRAY !

Page 39: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

40 15 5 20 25 10 4 2 1

0 1 2 3 4 5 6 7 8

15 5

20 25 10

• Tukar 4 dengan elemen terakhir padaMin-Heap

• Bagian akhir array sudah BUKAN bagianHEAP, tetapi sudah menjadi bagianSORTED ARRAY !

Page 40: Heapify & Heap sort - Information Retrieval Lab - CSUIir.cs.ui.ac.id/alfan/sda/Heap/heapify-heapsort.pdf · Heapify •Algoritma yang efisien bekerja dengan prinsip bottom-up, dan

• Lakukan hal yang sama hingga semua elemenpada Heap menjadi terurut