SlideServe
Menu
Préférences
© SlideServe · v2026

Heap And Heap Sort

thisbe avatar
30 diapositives · 654 Views · Jul 14, 2014

Heap And Heap Sort. Ethan Coulter 4/22/2004 CS 146 – Dr. Lee. A heap data structure is a data structure that stores a collection of objects (with keys), and has the following properties: – Complete Binary tree – Heap Order

Heap And Heap Sort - slide 1
01 / 30Heap And Heap Sort
1 / 17

Télécharger la présentation

Heap And Heap Sort
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Heap And Heap Sort Ethan Coulter 4/22/2004 CS 146 – Dr. Lee

  2. A heap data structure is a data structure that stores a collection of objects (with keys), and has the following properties: – Complete Binary tree – Heap Order It is implemented as an array where each node in the tree corresponds to an element of the array. What is a heap?

  3. A Complete Binary Tree A Complete Binary Tree is a binary tree that is completely filled on all levels with a possible exception where the lowest level is filled from left to right.

  4. Heap Order Property For every node v, other than the root, the key stored in v is greater or equal (smaller or equal for max heap) than the key stored in the parent of v. In this case the maximum value is stored in the root

  5. Max Heap Example

  6. Properties Of The Heap The parent of node v, stored in A[i], is stored in A[i/2] • The left child of node v, stored in A[i], is stored in A[2i] • The right child of node v, stored in A[i], is stored in A[2i+1]

  7. Maintaining The Heap Order In order to maintain the heap order the value of all children of A[i] must be less than the value of A[i]. • A[i].child.value <= A[i].value To keep this property we use a function called Heapify()

  8. Heapify() – Input: an array A and an index i. – Assumption: • the binary trees rooted at left and right children of A[i] are heaps • A[i] may violate the heap order property – Purpose: Push the value at A[i] down the heap until the tree rooted at A[i] is a heap.

  9. The Algorithm Heapify(A, i) l  Left_child(i) r  Right_child(i) if (l ≤ heap-size[A] and A [l] > A [i] then Largest l else Largest i if (r ≤ heap-size[A] and A[r] > A [largest] then Largestr if (largest = i) then swap(A [i], A [largest]) A [i] A [largest] Heapify(A, largest)

  10. 4 < 14 so we need to swap

  11. Now Check 4’s New Children and Swap

  12. The Heap Property Is Now Maintained

  13. Heap Sort 1. Build_heap(A) 2. For i  length[A] down to 2 do 3. Swap(A[1], A[i]) 4. heap-size[A]  heap-size[A]-1 5. Heapify(A,1)

  14. Time Cost Analysis Line 1 takes O(n) time There are n-1 calls to Heapify each call requires O(log n) time. Total cost O(n log n).

  15. Example 19 12 16 1 4 7 Array A 4 1 16 19 12 7

  16. Example Take out biggest 19 12 16 Move the last element to the root 1 4 7 Sorted: Array A 4 1 16 12 7 19

  17. Example 7 swap HEAPIFY() 12 16 1 4 Sorted: Array A 7 4 1 16 12 19

  18. Example 16 12 7 1 4 Sorted: Array A 4 1 16 12 7 19

  19. Example Take out biggest 16 Move the last element to the root 12 7 1 4 Sorted: Array A 4 1 12 7 16 19

  20. Example 4 12 7 1 Sorted: Array A 4 1 12 7 16 19

  21. Example 4 swap HEAPIFY() 12 7 1 Sorted: Array A 4 1 12 7 16 19

  22. Example 12 7 4 1 Sorted: Array A 12 7 4 1 16 19

  23. Example Take out biggest 12 Move the last element to the root 7 4 1 Sorted: Array A 7 4 1 16 19 12

  24. Example 1 swap HEAPIFY() 7 4 Sorted: Array A 1 7 4 16 19 12

  25. Example 7 4 1 Sorted: Array A 4 1 7 16 19 12

  26. Example Take out biggest 7 Move the last element to the root 4 1 Sorted: Array A 4 1 16 19 7 12

  27. Example 1 swap HEAPIFY() 4 Sorted: Array A 4 1 16 19 7 12

  28. Example Take out biggest 4 Move the last element to the root 1 Sorted: Array A 1 4 16 19 7 12

  29. Example Take out biggest 1 Sorted: Array A 4 1 16 19 7 12

  30. Example Sorted: 16 7 12 4 1 19