尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
HeapSort

CS 3358 Data Structures




                          1
Heapsort: Basic Idea

Problem: Arrange an array of items into sorted order.

1) Transform the array of items into a heap.
2) Invoke the “retrieve & delete” operation repeatedly, to
   extract the largest item remaining in the heap, until the
   heap is empty. Store each item retrieved from the heap
   into the array from back to front.
Note: We will refer to the version of heapRebuild used by
  Heapsort as rebuildHeap, to distinguish it from the version
  implemented for the class PriorityQ.

                                                               2
Transform an Array Into a Heap: Example
                  6       3   5   7    2    4    10    9
                  0       1   2   3    4    5    6     7
rebuildHeap

                      6
                                       • The items in the array, above,
                                         can be considered to be
                                         stored in the complete binary
        3                     5          tree shown at right.
                                       • Note that leaves 2, 4, 9 & 10
                                         are heaps; nodes 5 & 7 are
    7         2           4       10     roots of semiheaps.
                                       • rebuildHeap is invoked on
9                                        the parent of the last node in
                                         the array (= 9).
                                                                     3
4
5
6
7
8
9
Transform an Array Into a Heap: Example
                  6       3   5   9    2    4   10    7
                  0       1   2   3    4    5    6    7
rebuildHeap
                                       • Note that nodes 2, 4, 7, 9 &
                      6                  10 are roots of heaps; nodes
                                         3 & 5 are roots of semiheaps.
        3                     5        • rebuildHeap is invoked on
                                         the node in the array
                                         preceding node 9.
    9         2           4       10


7

                                                                    10
Transform an Array Into a Heap: Example
                  6       3   10   9   2    4    5     7
                  0       1   2    3   4    5     6    7
rebuildHeap
                                       • Note that nodes 2, 4, 5, 7, 9
                      6                  & 10 are roots of heaps;
                                         node 3 is the root of a
        3                     10         semiheap.
                                       • rebuildHeap is invoked on
                                         the node in the array
    9         2           4        5     preceding node 10.


7

                                                                     11
Transform an Array Into a Heap: Example
                  6       3   10   9   2    4    5     7
                  0       1   2    3   4    5     6    7
rebuildHeap
                                       • Note that nodes 2, 4, 5, 7, 9
                      6                  & 10 are roots of heaps;
                                         node 3 is the root of a
        3                     10         semiheap.
                                       • rebuildHeap is invoked on
                                         the node in the array
    9         2           4        5     preceding node 10.


7

                                                                     12
Transform an Array Into a Heap: Example
                  6       9   10   3   2    4    5    7
                  0       1   2    3   4    5    6    7
rebuildHeap
                                       • Note that nodes 2, 4, 5, 7 &
                      6                  10 are roots of heaps; node 3
                                         is the root of a semiheap.
        9                     10       • rebuildHeap is invoked
                                         recursively on node 3 to
                                         complete the transformation
    3         2           4        5     of the semiheap rooted at 9
                                         into a heap.
7

                                                                    13
Transform an Array Into a Heap: Example
                  6       9   10   7   2    4     5    3
                  0       1   2    3   4    5     6    7
rebuildHeap
                                       • Note that nodes 2, 3, 4, 5, 7,
                      6                  9 & 10 are roots of heaps;
                                         node 6 is the root of a
        9                     10         semiheap.
                                       • The recursive call to
                                         rebuildHeap returns to node
    7         2           4        5     9.
                                       • rebuildHeap is invoked on
                                         the node in the array
3
                                         preceding node 9.
                                                                     14
Transform an Array Into a Heap: Example
                10    9   6   7   2    4    5    3
                0     1   2   3   4    5    6    7


                 10               • Note that node 10 is now the
                                    root of a heap.
                                  • The transformation of the
        9                 6
                                    array into a heap is complete.

    7       2         4       5


3

                                                               15
Transform an Array Into a Heap (Cont’d.)

• Transforming an array into a heap begins by invoking
   rebuildHeap on the parent of the last node in the array.
• Recall that in an array-based representation of a complete binary
  tree, the parent of any node at array position, i, is
                             (i – 1) / 2 
• Since the last node in the array is at position n – 1, it follows
  that transforming an array into a heap begins with the node at
  position
                    (n – 2) / 2  =  n / 2  – 1
  and continues with each preceding node in the array.


                                                                      16
Transform an Array Into a Heap: C++

// transform array a[ ], containing n items, into a heap
for( int root = n/2 – 1; root >= 0; root – – )
{
  // transform a semiheap with the given root into a heap
      rebuildHeap( a, root, n );
}




                                                            17
Rebuild a Heap: C++
// transform a semiheap with the given root into a heap
void rebuildHeap( ItemType a[ ], int root, int n )
{ int child = 2 * root + 1;         // set child to root’s left child, if any
    if( child < n )                 // if root’s left child exists . . .
    { int rightChild = child + 1;
       if( rightChild < n && a[ rightChild ] > a[ child ] )
          child = rightChild;       // child indicates the larger item
       if( a[ root ] < a[ child ] )
       { swap( a[ root ], a[ child ] );
          rebuildHeap( a, child, n );
       }
    }
}
                                                                           18
Transform a Heap Into a Sorted Array

• After transforming the array of items into a heap, the next
  step in Heapsort is to:
   – invoke the “retrieve & delete” operation repeatedly, to
      extract the largest item remaining in the heap, until the
      heap is empty. Store each item retrieved from the heap
      into the array from back to front.
• If we want to perform the preceding step without using
  additional memory, we need to be careful about how we
  delete an item from the heap and how we store it back into
  the array.

                                                              19
Transform a Heap Into a Sorted Array:
                   Basic Idea
Problem: Transform array a[ ] from a heap of n items into a
  sequence of n items in sorted order.
Let last represent the position of the last node in the heap. Initially,
   the heap is in a[ 0 .. last ], where last = n – 1.
1) Move the largest item in the heap to the beginning of an (initially
   empty) sorted region of a[ ] by swapping a[0] with a[ last ].
2) Decrement last. a[0] now represents the root of a semiheap in
     a[ 0 .. last ], and the sorted region is in a[ last + 1 .. n – 1 ].
3) Invoke rebuildHeap on the semiheap rooted at a[0] to transform
   the semiheap into a heap.
4) Repeat steps 1 - 3 until last = -1. When done, the items in array
     a[ ] will be arranged in sorted order.
                                                                       20
Transform a Heap Into a Sorted Array: Example
                0    1   2   3   4     5    6    7
        a[ ]: 10     9   6   7   2     4    5    3
                             Heap

                10               • We start with the heap that
                                   we formed from an unsorted
                                   array.
        9                6
                                 • The heap is in a[0..7] and the
                                   sorted region is empty.
    7       2        4       5   • We move the largest item in
                                   the heap to the beginning of
                                   the sorted region by
3                                  swapping a[0] with a[7].
                                                                 21
Transform a Heap Into a Sorted Array: Example
                  0       1       2   3   4    5     6    7
        a[ ]:     3       9       6   7   2    4     5   10
                                  Semiheap               Sorted
rebuildHeap
                                          • a[0..6] now represents a
                      3                     semiheap.
                                          • a[7] is the sorted region.
       9                          6
                                          • Invoke rebuildHeap on the
                                            semiheap rooted at a[0].

   7          2               4       5


                                                                         22
Transform a Heap Into a Sorted Array: Example
                  0       1         2   3    4      5    6    7
        a[ ]:     9       3         6   7    2      4    5    10
                                  Becoming a Heap             Sorted
rebuildHeap
                                            • rebuildHeap is invoked
                      9                          recursively on a[1] to
                                                 complete the transformation
                                                 of the semiheap rooted at a[0]
       3                            6            into a heap.


   7          2               4         5


                                                                            23
Transform a Heap Into a Sorted Array: Example
              0       1       2    3     4     5    6    7
      a[ ]:   9       7       6    3     2    4     5    10
                                  Heap                   Sorted

                                         • a[0] is now the root of a heap
                  9                        in a[0..6].
                                         • We move the largest item in
                                           the heap to the beginning of
      7                       6            the sorted region by
                                           swapping a[0] with a[6].
  3       2               4        5


                                                                       24
Transform a Heap Into a Sorted Array: Example
                  0       1        2   3     4     5    6     7
        a[ ]:     5       7        6   3     2     4    9    10
                                  Semiheap              Sorted
rebuildHeap
                                             • a[0..5] now represents a
                      5                        semiheap.
                                             • a[6..7] is the sorted region.
       7                           6
                                             • Invoke rebuildHeap on the
                                               semiheap rooted at a[0].

   3          2               4


                                                                               25
Transform a Heap Into a Sorted Array: Example
                  0       1       2   3   4     5    6     7
        a[ ]:     7       5       6   3   2     4    9    10
                                  Heap               Sorted
rebuildHeap
                                          • Since a[1] is the root of a
                      7                     heap, a recursive call to
                                            rebuildHeap does nothing.
                                          • a[0] is now the root of a heap
       5                          6         in a[0..5].
                                          • We move the largest item in
   3          2               4             the heap to the beginning of
                                            the sorted region by
                                            swapping a[0] with a[5].
                                                                          26
Transform a Heap Into a Sorted Array: Example
                  0       1      2   3   4     5     6      7
        a[ ]:     4       5      6   3   2     7     9      10
                              Semiheap             Sorted
rebuildHeap
                                         • a[0..4] now represents a
                      4                    semiheap.
                                         • a[5..7] is the sorted region.
       5                         6
                                         • Invoke rebuildHeap on the
                                           semiheap rooted at a[0].

   3          2


                                                                           27
Transform a Heap Into a Sorted Array: Example
              0       1    2     3   4     5     6      7
      a[ ]:   6       5    4     3   2    7      9      10
                          Heap                 Sorted

                                     • a[0] is now the root of a heap
                  6                    in a[0..4].
                                     • We move the largest item in
                                       the heap to the beginning of
      5                    4           the sorted region by
                                       swapping a[0] with a[4].
  3       2


                                                                   28
Transform a Heap Into a Sorted Array: Example
                0        1   2     3   4     5    6     7
        a[ ]:   2        5   4     3   6     7    9    10
                        Semiheap             Sorted
rebuildHeap
                                       • a[0..3] now represents a
                    2                    semiheap.
                                       • a[4..7] is the sorted region.
       5                      4
                                       • Invoke rebuildHeap on the
                                         semiheap rooted at a[0].

   3


                                                                         29
Transform a Heap Into a Sorted Array: Example
                0       1   2   3   4    5     6    7
        a[ ]:   5       2   4   3   6    7     9   10
                Becoming a Heap          Sorted
rebuildHeap
                                    • rebuildHeap is invoked
                    5                 recursively on a[1] to
                                      complete the transformation
                                      of the semiheap rooted at a[0]
       2                    4         into a heap.


   3


                                                                 30
Transform a Heap Into a Sorted Array: Example
              0       1   2      3   4     5    6    7
      a[ ]:   5       3   4      2   6    7     9    10
                      Heap                 Sorted

                                     • a[0] is now the root of a heap
                  5                    in a[0..3].
                                     • We move the largest item in
                                       the heap to the beginning of
      3                      4         the sorted region by
                                       swapping a[0] with a[3].
  2


                                                                   31
Transform a Heap Into a Sorted Array: Example
                0       1   2      3   4     5      6   7
        a[ ]:   2       3   4      5   6     7      9   10
                    Semiheap               Sorted
rebuildHeap
                                       • a[0..2] now represents a
                    2                    semiheap.
                                       • a[3..7] is the sorted region.
       3                       4
                                       • Invoke rebuildHeap on the
                                         semiheap rooted at a[0].




                                                                         32
Transform a Heap Into a Sorted Array: Example
             0        1     2   3   4     5      6   7
     a[ ]:   4        3     2   5   6     7      9   10
                     Heap               Sorted

                                    • a[0] is now the root of a heap
                 4                    in a[0..2].
                                    • We move the largest item in
                                      the heap to the beginning of
     3                      2         the sorted region by
                                      swapping a[0] with a[2].




                                                                  33
Transform a Heap Into a Sorted Array: Example
                 0       1   2   3   4     5    6     7
        a[ ]:    2       3   4   5   6     7    9    10
                Semiheap             Sorted
rebuildHeap
                                     • a[0..1] now represents a
                     2                 semiheap.
                                     • a[2..7] is the sorted region.
       3
                                     • Invoke rebuildHeap on the
                                       semiheap rooted at a[0].




                                                                       34
Transform a Heap Into a Sorted Array: Example
             0       1   2   3   4     5    6    7
     a[ ]:   3       2   4   5   6    7     9    10
             Heap                Sorted

                                 • a[0] is now the root of a heap
                 3                 in a[0..1].
                                 • We move the largest item in
                                   the heap to the beginning of
     2                             the sorted region by
                                   swapping a[0] with a[1].




                                                               35
Transform a Heap Into a Sorted Array: Example
             0       1   2   3     4      5   6    7
     a[ ]:   2       3   4   5     6      7   9   10
         Heap                    Sorted

                                  • a[1..7] is the sorted region.
                 2                • Since a[0] is a heap, a
                                    recursive call to rebuildHeap
                                    does nothing.
                                  • We move the only item in the
                                    heap to the beginning of the
                                    sorted region.


                                                                    36
Transform a Heap Into a Sorted Array: Example
             0   1   2   3   4       5    6     7
     a[ ]:   2   3   4   5   6       7    9     10
                         Sorted

                             • Since the sorted region
                                  contains all the items in the
                                  array, we are done.




                                                                  37
Heapsort: C++
void heapsort( ItemType a[ ], int n )
{ // transform array a[ ] into a heap
      for( int root = n/2 – 1; root >= 0; root – – )
         rebuildHeap( a, root, n );
    for( int last = n – 1; last > 0; )
    { // move the largest item in the heap, a[ 0 .. last ], to the
       // beginning of the sorted region, a[ last+1 .. n–1 ], and
       // increase the sorted region
          swap( a[0], a[ last ] ); last – – ;
        // transform the semiheap in a[ 0 .. last ] into a heap
            rebuildHeap( a, 0, last );
    }
}
                                                                     38
Heapsort: Efficiency
• rebuildHeap( ) is invoked  n / 2  times to transform an array of
    n items into a heap. rebuildHeap( ) is then called n – 1 more
    times to transform the heap into a sorted array.
•   From our analysis of the heap-based, Priority Queue, we saw
    that rebuilding a heap takes O( log n ) time in the best, average,
    and worst cases.
•   Therefore, Heapsort requires
           O( [  n / 2  + (n – 1) ] * log n ) = O( n log n )
    time in the best, average and worst cases.
•   This is the same growth rate, in all cases, as Mergesort, and the
    same best and average cases as Quicksort.
•   Knuth’s analysis shows that, in the average case, Heapsort
    requires about twice as much time as Quicksort, and 1.5 times as
    much time as Mergesort (without requiring additional storage).
                                                                    39
Growth Rates for Selected Sorting Algorithms
                       Best case      Average case (†)       Worst case
    Selection sort         n2                n2                  n2
    Bubble sort             n                n2                  n2
    Insertion sort          n                n2                  n2
    Mergesort          n * log2 n        n * log2 n          n * log2 n
    Heapsort           n * log2 n        n * log2 n          n * log2 n
    Treesort           n * log2 n        n * log2 n              n2
    Quicksort          n * log2 n        n * log2 n              n2
    Radix sort              n                 n                   n
†
 According to Knuth, the average growth rate of Insertion sort is about
0.9 times that of Selection sort and about 0.4 times that of Bubble Sort.
Also, the average growth rate of Quicksort is about 0.74 times that of
Mergesort and about 0.5 times that of Heapsort.
                                                                            40

More Related Content

What's hot

Heaps
HeapsHeaps
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
Amit Kundu
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Shell sort
Shell sortShell sort
Shell sort
Rajendran
 
Hashing
HashingHashing
Hashing
kurubameena1
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Data structure array
Data structure  arrayData structure  array
Data structure array
MajidHamidAli
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Heap sort
Heap sortHeap sort
Heap sort
Ayesha Tahir
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
stack
stackstack
stack
Raj Sarode
 
Stack
StackStack
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 

What's hot (20)

Heaps
HeapsHeaps
Heaps
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Stacks
StacksStacks
Stacks
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Shell sort
Shell sortShell sort
Shell sort
 
Hashing
HashingHashing
Hashing
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Heap sort
Heap sortHeap sort
Heap sort
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
stack
stackstack
stack
 
Stack
StackStack
Stack
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 

Viewers also liked

Heap sort
Heap sortHeap sort
Heap sort
Mohd Arif
 
Heapsort
HeapsortHeapsort
Heapsort
mmoylan
 
Heap sort
Heap sortHeap sort
Heap sort
kaushiklv
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Algoritmo de ordenamiento: Heap Sort
Algoritmo de ordenamiento: Heap SortAlgoritmo de ordenamiento: Heap Sort
Algoritmo de ordenamiento: Heap Sort
Daniel Gomez Jaramillo
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
Tareq Hasan
 
Data Structure (Heap Sort)
Data Structure (Heap Sort)Data Structure (Heap Sort)
Data Structure (Heap Sort)
Adam Mukharil Bachtiar
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
Sajid Marwat
 
Heaps
HeapsHeaps
Heaps
pratmash
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
Pedro Hugo Valencia Morales
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
Bst Ali
 
Design and analysis of computer algorithms
Design and analysis of computer algorithmsDesign and analysis of computer algorithms
Design and analysis of computer algorithms
Krishna Chaytaniah
 
Heapsort
HeapsortHeapsort
Heapsort
Ssankett Negi
 
Heap - Python
Heap - PythonHeap - Python
Heap - Python
Sérgio Souza Costa
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
heshekik
 
практическая работа построение диаграмм
практическая работа построение диаграммпрактическая работа построение диаграмм
практическая работа построение диаграмм
liza2209
 
Effect of Solar Variability on the Helioshphere and Cosmic Rays
Effect of Solar Variability on the Helioshphere and Cosmic RaysEffect of Solar Variability on the Helioshphere and Cosmic Rays
Effect of Solar Variability on the Helioshphere and Cosmic Rays
ijsrd.com
 

Viewers also liked (17)

Heap sort
Heap sortHeap sort
Heap sort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Algoritmo de ordenamiento: Heap Sort
Algoritmo de ordenamiento: Heap SortAlgoritmo de ordenamiento: Heap Sort
Algoritmo de ordenamiento: Heap Sort
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Data Structure (Heap Sort)
Data Structure (Heap Sort)Data Structure (Heap Sort)
Data Structure (Heap Sort)
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Heaps
HeapsHeaps
Heaps
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Design and analysis of computer algorithms
Design and analysis of computer algorithmsDesign and analysis of computer algorithms
Design and analysis of computer algorithms
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heap - Python
Heap - PythonHeap - Python
Heap - Python
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
 
практическая работа построение диаграмм
практическая работа построение диаграммпрактическая работа построение диаграмм
практическая работа построение диаграмм
 
Effect of Solar Variability on the Helioshphere and Cosmic Rays
Effect of Solar Variability on the Helioshphere and Cosmic RaysEffect of Solar Variability on the Helioshphere and Cosmic Rays
Effect of Solar Variability on the Helioshphere and Cosmic Rays
 

Recently uploaded

Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
Fuxnet [EN] .pdf
Fuxnet [EN]                                   .pdfFuxnet [EN]                                   .pdf
Fuxnet [EN] .pdf
Overkill Security
 
An Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise IntegrationAn Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise Integration
Safe Software
 
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdfLee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
leebarnesutopia
 
An All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS MarketAn All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS Market
ScyllaDB
 
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
anilsa9823
 
MongoDB vs ScyllaDB: Tractian’s Experience with Real-Time ML
MongoDB vs ScyllaDB: Tractian’s Experience with Real-Time MLMongoDB vs ScyllaDB: Tractian’s Experience with Real-Time ML
MongoDB vs ScyllaDB: Tractian’s Experience with Real-Time ML
ScyllaDB
 
DynamoDB to ScyllaDB: Technical Comparison and the Path to Success
DynamoDB to ScyllaDB: Technical Comparison and the Path to SuccessDynamoDB to ScyllaDB: Technical Comparison and the Path to Success
DynamoDB to ScyllaDB: Technical Comparison and the Path to Success
ScyllaDB
 
Guidelines for Effective Data Visualization
Guidelines for Effective Data VisualizationGuidelines for Effective Data Visualization
Guidelines for Effective Data Visualization
UmmeSalmaM1
 
Discover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched ContentDiscover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched Content
ScyllaDB
 
Building a Semantic Layer of your Data Platform
Building a Semantic Layer of your Data PlatformBuilding a Semantic Layer of your Data Platform
Building a Semantic Layer of your Data Platform
Enterprise Knowledge
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDBScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB
 
Radically Outperforming DynamoDB @ Digital Turbine with SADA and Google Cloud
Radically Outperforming DynamoDB @ Digital Turbine with SADA and Google CloudRadically Outperforming DynamoDB @ Digital Turbine with SADA and Google Cloud
Radically Outperforming DynamoDB @ Digital Turbine with SADA and Google Cloud
ScyllaDB
 
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
manji sharman06
 
New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024
ThousandEyes
 
Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!
Ortus Solutions, Corp
 

Recently uploaded (20)

Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
Fuxnet [EN] .pdf
Fuxnet [EN]                                   .pdfFuxnet [EN]                                   .pdf
Fuxnet [EN] .pdf
 
An Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise IntegrationAn Introduction to All Data Enterprise Integration
An Introduction to All Data Enterprise Integration
 
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdfLee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
 
An All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS MarketAn All-Around Benchmark of the DBaaS Market
An All-Around Benchmark of the DBaaS Market
 
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
Call Girls Chennai ☎️ +91-7426014248 😍 Chennai Call Girl Beauty Girls Chennai...
 
MongoDB vs ScyllaDB: Tractian’s Experience with Real-Time ML
MongoDB vs ScyllaDB: Tractian’s Experience with Real-Time MLMongoDB vs ScyllaDB: Tractian’s Experience with Real-Time ML
MongoDB vs ScyllaDB: Tractian’s Experience with Real-Time ML
 
DynamoDB to ScyllaDB: Technical Comparison and the Path to Success
DynamoDB to ScyllaDB: Technical Comparison and the Path to SuccessDynamoDB to ScyllaDB: Technical Comparison and the Path to Success
DynamoDB to ScyllaDB: Technical Comparison and the Path to Success
 
Guidelines for Effective Data Visualization
Guidelines for Effective Data VisualizationGuidelines for Effective Data Visualization
Guidelines for Effective Data Visualization
 
Discover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched ContentDiscover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched Content
 
Building a Semantic Layer of your Data Platform
Building a Semantic Layer of your Data PlatformBuilding a Semantic Layer of your Data Platform
Building a Semantic Layer of your Data Platform
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDBScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
ScyllaDB Leaps Forward with Dor Laor, CEO of ScyllaDB
 
Radically Outperforming DynamoDB @ Digital Turbine with SADA and Google Cloud
Radically Outperforming DynamoDB @ Digital Turbine with SADA and Google CloudRadically Outperforming DynamoDB @ Digital Turbine with SADA and Google Cloud
Radically Outperforming DynamoDB @ Digital Turbine with SADA and Google Cloud
 
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
TrustArc Webinar - Your Guide for Smooth Cross-Border Data Transfers and Glob...
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
 
New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024
 
Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!Introducing BoxLang : A new JVM language for productivity and modularity!
Introducing BoxLang : A new JVM language for productivity and modularity!
 

Heap sort

  • 1. HeapSort CS 3358 Data Structures 1
  • 2. Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into a heap. 2) Invoke the “retrieve & delete” operation repeatedly, to extract the largest item remaining in the heap, until the heap is empty. Store each item retrieved from the heap into the array from back to front. Note: We will refer to the version of heapRebuild used by Heapsort as rebuildHeap, to distinguish it from the version implemented for the class PriorityQ. 2
  • 3. Transform an Array Into a Heap: Example 6 3 5 7 2 4 10 9 0 1 2 3 4 5 6 7 rebuildHeap 6 • The items in the array, above, can be considered to be stored in the complete binary 3 5 tree shown at right. • Note that leaves 2, 4, 9 & 10 are heaps; nodes 5 & 7 are 7 2 4 10 roots of semiheaps. • rebuildHeap is invoked on 9 the parent of the last node in the array (= 9). 3
  • 4. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. 8
  • 9. 9
  • 10. Transform an Array Into a Heap: Example 6 3 5 9 2 4 10 7 0 1 2 3 4 5 6 7 rebuildHeap • Note that nodes 2, 4, 7, 9 & 6 10 are roots of heaps; nodes 3 & 5 are roots of semiheaps. 3 5 • rebuildHeap is invoked on the node in the array preceding node 9. 9 2 4 10 7 10
  • 11. Transform an Array Into a Heap: Example 6 3 10 9 2 4 5 7 0 1 2 3 4 5 6 7 rebuildHeap • Note that nodes 2, 4, 5, 7, 9 6 & 10 are roots of heaps; node 3 is the root of a 3 10 semiheap. • rebuildHeap is invoked on the node in the array 9 2 4 5 preceding node 10. 7 11
  • 12. Transform an Array Into a Heap: Example 6 3 10 9 2 4 5 7 0 1 2 3 4 5 6 7 rebuildHeap • Note that nodes 2, 4, 5, 7, 9 6 & 10 are roots of heaps; node 3 is the root of a 3 10 semiheap. • rebuildHeap is invoked on the node in the array 9 2 4 5 preceding node 10. 7 12
  • 13. Transform an Array Into a Heap: Example 6 9 10 3 2 4 5 7 0 1 2 3 4 5 6 7 rebuildHeap • Note that nodes 2, 4, 5, 7 & 6 10 are roots of heaps; node 3 is the root of a semiheap. 9 10 • rebuildHeap is invoked recursively on node 3 to complete the transformation 3 2 4 5 of the semiheap rooted at 9 into a heap. 7 13
  • 14. Transform an Array Into a Heap: Example 6 9 10 7 2 4 5 3 0 1 2 3 4 5 6 7 rebuildHeap • Note that nodes 2, 3, 4, 5, 7, 6 9 & 10 are roots of heaps; node 6 is the root of a 9 10 semiheap. • The recursive call to rebuildHeap returns to node 7 2 4 5 9. • rebuildHeap is invoked on the node in the array 3 preceding node 9. 14
  • 15. Transform an Array Into a Heap: Example 10 9 6 7 2 4 5 3 0 1 2 3 4 5 6 7 10 • Note that node 10 is now the root of a heap. • The transformation of the 9 6 array into a heap is complete. 7 2 4 5 3 15
  • 16. Transform an Array Into a Heap (Cont’d.) • Transforming an array into a heap begins by invoking rebuildHeap on the parent of the last node in the array. • Recall that in an array-based representation of a complete binary tree, the parent of any node at array position, i, is  (i – 1) / 2  • Since the last node in the array is at position n – 1, it follows that transforming an array into a heap begins with the node at position  (n – 2) / 2  =  n / 2  – 1 and continues with each preceding node in the array. 16
  • 17. Transform an Array Into a Heap: C++ // transform array a[ ], containing n items, into a heap for( int root = n/2 – 1; root >= 0; root – – ) { // transform a semiheap with the given root into a heap rebuildHeap( a, root, n ); } 17
  • 18. Rebuild a Heap: C++ // transform a semiheap with the given root into a heap void rebuildHeap( ItemType a[ ], int root, int n ) { int child = 2 * root + 1; // set child to root’s left child, if any if( child < n ) // if root’s left child exists . . . { int rightChild = child + 1; if( rightChild < n && a[ rightChild ] > a[ child ] ) child = rightChild; // child indicates the larger item if( a[ root ] < a[ child ] ) { swap( a[ root ], a[ child ] ); rebuildHeap( a, child, n ); } } } 18
  • 19. Transform a Heap Into a Sorted Array • After transforming the array of items into a heap, the next step in Heapsort is to: – invoke the “retrieve & delete” operation repeatedly, to extract the largest item remaining in the heap, until the heap is empty. Store each item retrieved from the heap into the array from back to front. • If we want to perform the preceding step without using additional memory, we need to be careful about how we delete an item from the heap and how we store it back into the array. 19
  • 20. Transform a Heap Into a Sorted Array: Basic Idea Problem: Transform array a[ ] from a heap of n items into a sequence of n items in sorted order. Let last represent the position of the last node in the heap. Initially, the heap is in a[ 0 .. last ], where last = n – 1. 1) Move the largest item in the heap to the beginning of an (initially empty) sorted region of a[ ] by swapping a[0] with a[ last ]. 2) Decrement last. a[0] now represents the root of a semiheap in a[ 0 .. last ], and the sorted region is in a[ last + 1 .. n – 1 ]. 3) Invoke rebuildHeap on the semiheap rooted at a[0] to transform the semiheap into a heap. 4) Repeat steps 1 - 3 until last = -1. When done, the items in array a[ ] will be arranged in sorted order. 20
  • 21. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 10 9 6 7 2 4 5 3 Heap 10 • We start with the heap that we formed from an unsorted array. 9 6 • The heap is in a[0..7] and the sorted region is empty. 7 2 4 5 • We move the largest item in the heap to the beginning of the sorted region by 3 swapping a[0] with a[7]. 21
  • 22. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 3 9 6 7 2 4 5 10 Semiheap Sorted rebuildHeap • a[0..6] now represents a 3 semiheap. • a[7] is the sorted region. 9 6 • Invoke rebuildHeap on the semiheap rooted at a[0]. 7 2 4 5 22
  • 23. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 9 3 6 7 2 4 5 10 Becoming a Heap Sorted rebuildHeap • rebuildHeap is invoked 9 recursively on a[1] to complete the transformation of the semiheap rooted at a[0] 3 6 into a heap. 7 2 4 5 23
  • 24. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 9 7 6 3 2 4 5 10 Heap Sorted • a[0] is now the root of a heap 9 in a[0..6]. • We move the largest item in the heap to the beginning of 7 6 the sorted region by swapping a[0] with a[6]. 3 2 4 5 24
  • 25. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 5 7 6 3 2 4 9 10 Semiheap Sorted rebuildHeap • a[0..5] now represents a 5 semiheap. • a[6..7] is the sorted region. 7 6 • Invoke rebuildHeap on the semiheap rooted at a[0]. 3 2 4 25
  • 26. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 7 5 6 3 2 4 9 10 Heap Sorted rebuildHeap • Since a[1] is the root of a 7 heap, a recursive call to rebuildHeap does nothing. • a[0] is now the root of a heap 5 6 in a[0..5]. • We move the largest item in 3 2 4 the heap to the beginning of the sorted region by swapping a[0] with a[5]. 26
  • 27. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 4 5 6 3 2 7 9 10 Semiheap Sorted rebuildHeap • a[0..4] now represents a 4 semiheap. • a[5..7] is the sorted region. 5 6 • Invoke rebuildHeap on the semiheap rooted at a[0]. 3 2 27
  • 28. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 6 5 4 3 2 7 9 10 Heap Sorted • a[0] is now the root of a heap 6 in a[0..4]. • We move the largest item in the heap to the beginning of 5 4 the sorted region by swapping a[0] with a[4]. 3 2 28
  • 29. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 2 5 4 3 6 7 9 10 Semiheap Sorted rebuildHeap • a[0..3] now represents a 2 semiheap. • a[4..7] is the sorted region. 5 4 • Invoke rebuildHeap on the semiheap rooted at a[0]. 3 29
  • 30. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 5 2 4 3 6 7 9 10 Becoming a Heap Sorted rebuildHeap • rebuildHeap is invoked 5 recursively on a[1] to complete the transformation of the semiheap rooted at a[0] 2 4 into a heap. 3 30
  • 31. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 5 3 4 2 6 7 9 10 Heap Sorted • a[0] is now the root of a heap 5 in a[0..3]. • We move the largest item in the heap to the beginning of 3 4 the sorted region by swapping a[0] with a[3]. 2 31
  • 32. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 2 3 4 5 6 7 9 10 Semiheap Sorted rebuildHeap • a[0..2] now represents a 2 semiheap. • a[3..7] is the sorted region. 3 4 • Invoke rebuildHeap on the semiheap rooted at a[0]. 32
  • 33. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 4 3 2 5 6 7 9 10 Heap Sorted • a[0] is now the root of a heap 4 in a[0..2]. • We move the largest item in the heap to the beginning of 3 2 the sorted region by swapping a[0] with a[2]. 33
  • 34. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 2 3 4 5 6 7 9 10 Semiheap Sorted rebuildHeap • a[0..1] now represents a 2 semiheap. • a[2..7] is the sorted region. 3 • Invoke rebuildHeap on the semiheap rooted at a[0]. 34
  • 35. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 3 2 4 5 6 7 9 10 Heap Sorted • a[0] is now the root of a heap 3 in a[0..1]. • We move the largest item in the heap to the beginning of 2 the sorted region by swapping a[0] with a[1]. 35
  • 36. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 2 3 4 5 6 7 9 10 Heap Sorted • a[1..7] is the sorted region. 2 • Since a[0] is a heap, a recursive call to rebuildHeap does nothing. • We move the only item in the heap to the beginning of the sorted region. 36
  • 37. Transform a Heap Into a Sorted Array: Example 0 1 2 3 4 5 6 7 a[ ]: 2 3 4 5 6 7 9 10 Sorted • Since the sorted region contains all the items in the array, we are done. 37
  • 38. Heapsort: C++ void heapsort( ItemType a[ ], int n ) { // transform array a[ ] into a heap for( int root = n/2 – 1; root >= 0; root – – ) rebuildHeap( a, root, n ); for( int last = n – 1; last > 0; ) { // move the largest item in the heap, a[ 0 .. last ], to the // beginning of the sorted region, a[ last+1 .. n–1 ], and // increase the sorted region swap( a[0], a[ last ] ); last – – ; // transform the semiheap in a[ 0 .. last ] into a heap rebuildHeap( a, 0, last ); } } 38
  • 39. Heapsort: Efficiency • rebuildHeap( ) is invoked  n / 2  times to transform an array of n items into a heap. rebuildHeap( ) is then called n – 1 more times to transform the heap into a sorted array. • From our analysis of the heap-based, Priority Queue, we saw that rebuilding a heap takes O( log n ) time in the best, average, and worst cases. • Therefore, Heapsort requires O( [  n / 2  + (n – 1) ] * log n ) = O( n log n ) time in the best, average and worst cases. • This is the same growth rate, in all cases, as Mergesort, and the same best and average cases as Quicksort. • Knuth’s analysis shows that, in the average case, Heapsort requires about twice as much time as Quicksort, and 1.5 times as much time as Mergesort (without requiring additional storage). 39
  • 40. Growth Rates for Selected Sorting Algorithms Best case Average case (†) Worst case Selection sort n2 n2 n2 Bubble sort n n2 n2 Insertion sort n n2 n2 Mergesort n * log2 n n * log2 n n * log2 n Heapsort n * log2 n n * log2 n n * log2 n Treesort n * log2 n n * log2 n n2 Quicksort n * log2 n n * log2 n n2 Radix sort n n n † According to Knuth, the average growth rate of Insertion sort is about 0.9 times that of Selection sort and about 0.4 times that of Bubble Sort. Also, the average growth rate of Quicksort is about 0.74 times that of Mergesort and about 0.5 times that of Heapsort. 40
  翻译: