Chapter 8: Problem 14
Illustrate the performance of the heap-sort algorithm on the following input sequence: (2,5,16,4,10,23,39,18,26,15)
Short Answer
Expert verified
The sorted sequence is (2, 4, 5, 10, 15, 16, 18, 23, 26, 39).
Step by step solution
01
- Build the Max-Heap
Heapify the array to build a max-heap. The array is (2, 5, 16, 4, 10, 23, 39, 18, 26, 15). Start from the last non-leaf node and move upwards to the root. Transform the array step by step into a max-heap.
02
Step 1.1 - Heapify sub-tree rooted at index 4
For index 4 (left child is 9 and right child is not present), compare value 10 with its child 15, then swap if necessary. Updated array: (2, 5, 16, 4, 15, 23, 39, 18, 26, 10)
03
Step 1.2 - Heapify sub-tree rooted at index 3
For index 3 (left child is 7 and right child is 8), compare value 4 with its children 18 and 26, and then swap with the largest value (26). Continue to heapify the affected sub-tree. Updated array: (2, 5, 16, 26, 15, 23, 39, 18, 4, 10). No further action required on this sub-tree.
04
Step 1.3 - Heapify sub-tree rooted at index 2
For index 2 (left child is 5 and right child is 6), compare value 16 with its children 23 and 39, and then swap with the largest value (39). Continue to heapify the affected sub-tree. Updated array: (2, 5, 39, 26, 15, 23, 16, 18, 4, 10). No further action required on this sub-tree.
05
Step 1.4 - Heapify sub-tree rooted at index 1
For index 1 (left child is 3 and right child is 4), compare value 5 with its children 26 and 15, and then swap with the largest value (26). Continue to heapify the affected sub-tree. Updated array: (2, 26, 39, 18, 15, 23, 16, 5, 4, 10). Heapify affected sub-tree rooted at index 3 (4, 5). Don't require further heapification.
06
Step 1.5 - Heapify sub-tree rooted at index 0
For index 0 (left child is 1 and right child is 2), compare value 2 with its children 26 and 39, and then swap with the largest value (39). Continue to heapify the affected sub-tree. Updated array: (39, 26, 23, 18, 15, 2, 16, 5, 4, 10). Heapify affected sub-tree rooted at index 2 (16, 2). Updated array: (39, 26, 23, 18, 15, 10, 16, 5, 4, 2).
07
- Extract Elements from the Heap
Remove the root element (largest) and reduce the heap size, repeatedly heapify the rest of the array. Repeat until the heap is empty.
08
Step 2.1 - Remove the root and re-heapify
Swap the root with the last element and reduce the heap size. Re-heapify from the root. (2, 26, 23, 18, 15, 10, 16, 5, 4, 39) and re-heapify: (26, 18, 23, 5, 15, 10, 16, 2, 4). Result array after first extraction (26, 18, 23, 5, 15, 10, 16, 2, 4, 39).
09
Step 2.2 - Continuation of root extraction
Swap the root with the last element and re-heapify. (4, 18, 23, 5, 15, 10, 16, 2, 26, 39) and re-heapify: (23, 18, 16, 5, 15, 10, 4, 2). Result array: (23, 18, 16, 5, 15, 10, 4, 2, 26, 39).
10
Step 2.3 - Further extractions
Continue swapping and re-heapifying till the heap is fully sorted. Next steps will be:Initial: (2, 18, 16, 5, 15, 10, 4, 23, 26, 39)Re-heapify: (18, 15, 16, 5, 2, 10, 4)Initial: (4, 15, 16, 5, 2, 10, 18)Re-heapify: (16, 15, 10, 5, 2, 4)Initial: (4, 15, 10, 5, 2, 16)Re-heapify: (15, 5, 10, 4, 2)Initial: (2, 5, 10, 4, 15)Re-heapify: (10, 5, 2, 4)Initial: (4, 5, 2, 10)Re-heapify: (5, 4, 2)Final stages: Swap and heapify till it's fully ordered. Result array becomes: (2, 4, 5, 10, 15, 16, 18, 23, 26, 39)
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
heaps and priority queues
Heaps are a special type of binary tree that satisfy the heap property. In a max-heap, for any given node 'i', the value of 'i' is greater than or equal to the values of its children. This property ensures that the largest element is always at the root of the tree.
Heaps are often used to implement priority queues. A priority queue is a data structure where each element has a priority assigned to it. In a max-priority queue, elements with higher priority are served before elements with lower priority.
The process of inserting an element into a heap involves adding the element at the end of the tree and then 'heapifying' to restore the heap property. Similarly, removing the root element (the max element in a max-heap) involves replacing it with the last element in the tree and then heapifying. These properties and operations make heaps a useful data structure for various algorithms, including heap-sort.
Heaps are often used to implement priority queues. A priority queue is a data structure where each element has a priority assigned to it. In a max-priority queue, elements with higher priority are served before elements with lower priority.
The process of inserting an element into a heap involves adding the element at the end of the tree and then 'heapifying' to restore the heap property. Similarly, removing the root element (the max element in a max-heap) involves replacing it with the last element in the tree and then heapifying. These properties and operations make heaps a useful data structure for various algorithms, including heap-sort.
sorting algorithms
Sorting algorithms are methods for organizing data in a particular order. Common sorting algorithms include bubble sort, quicksort, merge sort, and heap-sort. Each of these has unique characteristics, trade-offs, and use cases.
Heap-sort is a comparison-based sorting algorithm. It leverages the heap data structure to efficiently sort elements. The algorithm involves two main steps:
Heap-sort is a comparison-based sorting algorithm. It leverages the heap data structure to efficiently sort elements. The algorithm involves two main steps:
- Building a max-heap from the input data.
- Repeatedly extracting the maximum element from the heap and adjusting the remaining heap.
time complexity
In computer science, the time complexity of an algorithm indicates the amount of time it takes to run as a function of the size of its input. It's an important factor in determining the efficiency and scalability of an algorithm.
The heap-sort algorithm has a time complexity of O(n log n) in the average, best, and worst cases. This is because:
The heap-sort algorithm has a time complexity of O(n log n) in the average, best, and worst cases. This is because:
- Building the max-heap takes O(n) time, where 'n' is the number of elements.
- Each extraction of the maximum element and the subsequent heapification process takes O(log n) time.
- This heapification process is repeated 'n' times, making the time complexity O(n log n).