Download presentation
Presentation is loading. Please wait.
1
Heaps and the Heap Sort Algorithm 1)Definition – A heap is a binary tree which a) Is an almost complete binary tree b) The key in the root is ≥ the keys of its children c) The left and right subtrees are also heaps recursion
2
2) Array Representation: 11 1286 15 17 Rules: a) Children of Node at index i are found at indices 2*i and 2*i + 1 b) Parent of a node at index k is found at k / 2 Index123456 Key1711158612
3
LOOP Heap Sort Algorithm For Array Structure – Range Initialized to array size. 1)Interchange front with rear 2)Trickle down – if child > parent then interchange current node with the largest child Continue trickle down until node ≥ both children or have exceeded the current range 3) Reduce Range by 1 (Stop when range = 1)
4
Index 1 2 3 4 5 6 7 8 9 Value y r p d f b k a c Heap Sort on a Tree-like Structure K R BDF P Y CA
5
Analysis of Heap Sort: Note: A heap is an almost complete binary tree, so … N elements in heap ≈ LOG(N) Height of Tree Worst case analysis: At each pass through loop, N items must be compared with at most LOG(N-1) other items Note: The tree does shrink O(N * LOG N)
6
Heaps – Applications Priority Queue – A data structure with 2 operations 1) Insert an item 2) Remove the item with the largest key 4 2 4 5 5 E.g. Tasks with assigned priorities coming into a system – awaiting processing. If 2 jobs having same priority, ‘older’ job done first
7
// Program: Heap.c++ // Purpose: Perform heap sort method to arrange and modify //an array of data elements in an ascending order. Use //an array-based max heap object, and delete it at the end // #include //For ‘cin’ and ‘cout’ typedef int DATA_TYPE; // also the ‘key’ class Heap { DATA_TYPE * heap; int heap_size; void init_Heap(DATA_TYPE A [ ]); public: Heap(int n); //constructor ~Heap() { delete [ ] heap; } //Destructor void ReAdjust_Heap (int Root, int Max_Elem); void Build_Heap(void); void Debug_Print (int pass, int reduced_heap_size); void Heap_Sort(DATA_TYPE A [ ] ); void Print_Heap (void); };
8
// Heap(): Constructor for an object Heap::Heap(int n) { heap = new DATA_TYPE[n + 1}; heap_size = n; } // Init_Heap(): Initialize the heap from the array A void Heap::Init_Heap(DATA_TYPE A [ ] ) { for (int i = 1; I <= heap_size; i++) heap[i] = A[i-1]; }
9
// ReAdjust_Heap(): // Restore the max heap properties, where the parent with index ‘Root=i’ has // left and right children at ‘2*i’ and ‘2*i + 1 Void Heap:: ReAdjust_Heap(int root, int Max_Elem) { enum BOOLEAN {FALSE,TRUE}; DATA_TYPE x = heap[Root]; int j = 2 * Root; // Obtain Child information while (( j = heap[j]) Finished = TRUE; else { heap[j/2] = heap[j]; j = 2 * j; } } heap[j/2] = x; }
10
// Debug_Print(): // Print each element of the heap. A vertical bar marks the end of the heap, // sorted elements follow void Heap::Debug_Print(int pass, int reduced_heap_size) { cout << “Pass #” << pass << “:”; for (int i = 1; i <= reduced_heap_size; i++) cout << heap[i] << “ “; cout << “|”; for(; i <= heap_size; i++) cout << heap[j] << “ “; cout << “\n”; }
11
// Build_Heap() // Build a max heap from the given array ‘A’ by inserting the elements into the // max heap using ReAdjust_Heap(). void Heap::Build_Heap(void) { for(int i = heap_size/2; i > 0; i--) { Readjust_Heap (i, heap_size); } }
12
// Heap_Sort(): // Perform heap sort for ‘A’ and update ‘A’. (Iterative version) void Heap:: Heap_Sort (DATA_TYPE A [ ]) { //Initialize the heap with element of the Array ‘A’ Init_Heap(A); Build_Heap(): //Build a max heap // Sort the max heap in ascending order for (int i = (heap_size – 1); i > 0; i--) { int tmp = heap[i + 1]; //swap heap[i + 1] = heap[1]; heap[1] = tmp; // put root of heap in sorted position A[i] A[i] = heap[i+1]; ReAdjust_Heap(1,i); //Rebuild max heap #ifdef DEBUG Debug_Print((heap_size – i), i); #endif } A[0] = heap[1]; //Put last element of heap in A }
13
// Print_Heap() : Print the result void Heap::Print_Heap (void) { cout << “\n**Sorted in ascending order using heap sort **\n”; for(int i=1; i<=heap_size; i++) cout << “ “ << heap[i]; cout << “\n”; }
14
// main(): Test driver for OOP Implementation of a heap and the heap sort void main(void) { int n; cout > n; // Declare and initialize an object of “Heap” class Heap heap_obj(n); // Array of data elements to be sorted static DATA_TYPE A[ ] = {33,60,5,15,25,12,45,70,35,7}; cout << “Unsorted array is : \n”; for (int i=0; i<n; i++) cout << A[i] << “ “; cout << “\n\n”; heap_obj.Heap_Sort (A); heap_obj.Print_Heap(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.