Heapsort - Analysis of Algorithms - Lecture Slides, Slides of Design and Analysis of Algorithms

Main points of this lecture are: Heapsort, Insertion Sort, Merge Sort, Heap Operations, Priority Queues, Heap Data Structure, Garbage-Collected Storage, Binary Representation, Example of Max Heap, Heap Property, Induction and Transitivity

Typology: Slides

2012/2013

Uploaded on 04/23/2013

asmita
asmita 🇮🇳

4.6

(34)

178 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Heapsort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Heapsort - Analysis of Algorithms - Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Heapsort

Overview

  • Heapsort
    • O( n lg n ) worst case—like merge sort.
    • Sorts in place—like insertion sort.
    • Combines the best of both algorithms.
  • To understand heapsort, we’ll cover heaps and heap operations, and then we’ll take a look at priority queues.

Example of a Max Heap

Heap property

  • For max-heaps (largest element at root), max-heap property: for all nodes i, excluding the root, A [PARENT( i )] ≥ A [ i ].
  • For min-heaps (smallest element at root), min-heap property: for all nodes i, excluding the root, A [PARENT( i )] ≤ A [ i ].
  • By induction and transitivity of ≥, the max-heap property guarantees that the maximum element of a max-heap is at the root. Similar argument for min- heaps.
  • The heapsort algorithm we’ll show uses max-heaps.

Maintaining the heap property

Maintaining the heap property

  • The way MAX-HEAPIFY works:
    • Compare A [ i ], A [LEFT( i )], and A [RIGHT( i )].
    • If necessary, swap A [ i ] with the larger of the two children to preserve heap property.
    • Continue this process of comparing and swapping down the heap, until subtree rooted at i is max- heap. If we hit a leaf, then the subtree rooted at the leaf is trivially a max-heap.

Example

  • Node 2 violates the max-heap property.
  • Compare node 2 with its children, and then swap it with the larger of the two children.
  • Continue down the tree, swapping until the value is properly placed at the root of a subtree that is a max-heap. In this case, the max-heap is a leaf.
  • Time: O(lg n ).
  • Analysis: Heap is almost-complete binary tree, hence must process O(lg n ) levels, with constant work at each level (comparing 3 items and maybe swapping 2).

Building a heap

  • The following procedure, given an unordered array, will produce a max-heap.

Correctness

Analysis

  • Simple bound : O( n ) calls to MAX-HEAPIFY, each of which takes O(lg n ) time ⇒ O( n lg n ).
  • Tighter analysis : Observation: Time to run MAX-HEAPIFY is linear in the height of the node it’s run on, and most nodes have small heights. Have ≤ ceiling( n /2 h +1) nodes of height h (see Exercise 6.3-3), and height of heap is floor(lg n ) (Exercise 6.1-2).

The heapsort algorithm

  • Given an input array, the heapsort algorithm acts as follows: - Builds a max-heap from the array. - Starting with the root (the maximum element), the algorithm places the maximum element into the correct place in the array by swapping it with the element in the last position in the array. - “Discard” this last node (knowing that it is in its correct place) by decreasing the heap size, and calling MAX- HEAPIFY on the new (possibly incorrectly-placed) root. - Repeat this “discarding” process until only one node (the smallest element) remains, and therefore is in the correct place in the array.

The heapsort algorithm

Analysis

  • BUILD-MAX-HEAP: O ( n )
  • for loop: n – 1 times
  • Exchange elements: O (1)
  • MAX-HEAPIFY: O ( n lg n )
  • Total time: O ( n lg n )
  • Though heapsort is a great algorithm, a well- implemented quicksort usually beats it in practice.

Heap implementation of priority

queue

  • Heaps efficiently implement priority queues.
    • We look at maxpriority queues implemented with max-heaps.
    • Min-priority queues are implemented with min- heaps similarly.
  • A heap gives a good compromise between fast insertion but slow extraction and vice versa. - Both operations take O ( n lg n ) time.