Algorithms Sorting. Sorting Definition It is a process for arranging a collection of items in an order. Sorting can arrange both numeric and alphabetic.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Analysis of Algorithms
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Sorting Algorithms and Average Case Time Complexity
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Heapsort.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Simple Sorting Algorithms
Analysis of Algorithms CS 477/677
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
By: Vishal Kumar Arora AP,CSE Department, Shaheed Bhagat Singh State Technical Campus, Ferozepur. Different types of Sorting Techniques used in Data Structures.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Binary Heap.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Prof. U V THETE Dept. of Computer Science YMA
Heaps, Heapsort, and Priority Queues
Simple Sorting Algorithms
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
Dr. David Matuszek Heapsort Dr. David Matuszek
Heaps, Heapsort, and Priority Queues
Selection sort Given an array of length n,
Heapsort.
Sub-Quadratic Sorting Algorithms
Heapsort.
Simple Sorting Algorithms
Heapsort.
Simple Sorting Algorithms
Heapsort.
Simple Sorting Algorithms
Heaps & Multi-way Search Trees
Heapsort.
CO 303 Algorithm Analysis and Design
Presentation transcript:

Algorithms Sorting

Sorting Definition It is a process for arranging a collection of items in an order. Sorting can arrange both numeric and alphabetic data in increasing or decreasing order Unsorted array sorted array

3 1. Bubble Sort it is one of the oldest sorts known. The bubble sort got its name because of the way the biggest elements "bubble" to the top. It based on the property of a sorted list that any two adjacent elements are in sorted order.

Bubble sort Compare each element (except the last one) with its neighbor to the right – If they are out of order, swap them – This puts the largest element at the very end – The last element is now in the correct and final place Compare each element (except the last two) with its neighbor to the right – If they are out of order, swap them – This puts the second largest element next to last – The last two elements are now in their correct and final places Compare each element (except the last three) with its neighbor to the right – Continue as above until you have no unsorted elements on the left 4

5 Example Consider a set of data: Bubble sort first compares the first two elements, the 5 and the 9. Because they are already in sorted order, nothing happens. The next pair of numbers, the 9 and the 2 are compared. Because they are not in sorted order, they are swapped and the data becomes:

6 To better understand the "bubbling" nature of the sort, watch how the largest number, 9, "bubbles" to the top in the first iteration of the sort. First iteration (5 9) > compare 5 and 9, no swap 5 (9 2) > compare 9 and 2, swap 5 2 (9 8) > compare 9 and 8, swap (9 4) > compare 9 and 4, swap (9 6) 3 --> compare 9 and 6, swap (9 3) --> compare 9 and 3, swap > first iteration complete Example (cont)

7 Notice that in the example, the largest element, 9, got swapped all the way into its correct position at the end of the list. This happens because in each comparison, the larger element is always pushed towards its place at the end of the list. Example (cont)

8 Second iteration In the second iteration, the second-largest element will be bubbled up to its correct place in the same manner: (5 2) > compare 5 and 2, swap 2 (5 8) > compare 5 and 8, no swap 2 5 (8 4) > compare 8 and 4, swap (8 6) > compare 8 and 6, swap (8 3) 9 --> compare 8 and 3, swap > no need to compare last two because the last element is known to be the largest. Example (cont)

Third iteration (2 5) >compare 2 and 5 no swap, 2 (5 4) >compare 5 and 4 swap, 2 4 (5 6) >compare 5 and 6 no swap, (6 3) >compare 6 and 3 swap, >no need to compare the last three elements 9

10 Fourth iteration (2 4) > compare 2 and 4, no swap 2 (4 5) > compare 4 and 5, no swap 2 4 (5 3) > compare 5 and 3, swap > no need to compare the last four elements. Fifth iteration (2 4) > compare 2 and 4, no swap 2 (4 3) > compare 4 and 3, swap > compare 4 and 5, no swap > no need to compare the last five elements. Sixth iteration (2 3) > compare 2 and 3, no swap. no need to compare the last six elements. Example (cont)

Example of bubble sort (done) 11

Bubble Sorting Algorithm Algorithm: Input array of size N for(i=n-1;i>=1;i--) { for(y=1;y<=i; y++) { if(x[y-1]>x[y]) { temp=x[y-1]; x[y-1]=x[y]; x[y]=temp; } Output sorted array of Size N

Code for bubble sort public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) { // counting down for (inner = 0; inner a[inner + 1]) { // if out of order... int temp = a[inner]; //...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } } 13

Selection sort Given an array of length n, – Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 – Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 – Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 – Search elements 3 through n-1 and select the smallest Swap it with the element in location 3 – Continue in this fashion until there’s nothing left to search

15 Selection Sort int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted

Selection Sort outmin = 1 3 out 16

Example of selection sort The selection sort might swap an array element with itself--this is harmless, and not worth checking for

Chapter 10: Sorting18 Selection Sort Example scan 0-4, smallest 20 swap 35 and scan 1-4, smallest 30 swap 65 and scan 2-4, smallest 35 swap 65 and scan 3-4, smallest 60 swap 60 and done

Chapter 10: Sorting19 Selection Sort Algorithm 1.for fill = 0 to n-1 do 2.set posMin to fill for next = fill+1 to n-1 do 3. if item at next < item at posMin 4. set posMin to next 5. Exchange item at posMin with one at fill

Code for selection sort public static void selectionSort(int[] a) { int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++) { min = outer; for (inner = outer + 1; inner < a.length; inner++) { if (a[inner] < a[min]) { min = inner; } // Invariant: for all i, if outer <= i <= inner, then a[min] <= a[i] } // a[min] is least among a[outer]..a[a.length - 1] int temp = a[outer]; a[outer] = a[min]; a[min] = temp; // Invariant: for all i <= outer, if i < j then a[i] <= a[j] } } 20

I NSERTION S ORT One of the simplest methods to sort an array is an insertion sort, If the first few objects are already sorted, an unsorted object can be inserted in the sorted set in proper place. An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence.

Chapter 10: Sorting22 Insertion Sort Based on technique of card players to arrange a hand – Player keeps cards picked up so far in sorted order – When the player picks up a new card Makes room for the new card Then inserts it in its proper place

Insertion sort Example int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted.

24 Insertion sort Example int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted

25 Starting near the top of the array in the Figure (a) we extract the 3. Then the above elements are shifted down until we find the correct place to insert the 3. This process repeats in Figure (b) with the next number. Finally, in Figure (c), we complete the sort by inserting 2 in the correct place.

for i = 2 to N do newElement = list[ i ] location = i - 1 while (location ≥ 1) and (list[ location ] > newElement) do list[ location + 1 ] = list[ location ] location = location - 1 end while list[ location + 1 ] = newElement end for I NSERTION S ORT A LGORITHM 26

Conclusion and Summary Bubble sort is useful for small amounts of data. Selection sort can be used when amount of data is small and swapping is time- consuming. Insertion sort is most versatile and the best when the list is almost sorted. 27

Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer prototype Select a pivot and split the data into two groups: ( pivot): Recursively apply Quicksort to the subgroups

29 Quick-Sort Quick-sort is a randomized sorting algorithm based on the divide-and-conquer prototype: Divide: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Recur: sort L and G Conquer: join L, E and G x x LG E x

30 Quicksort – Basic Algorithm Sorting an array S consists of 4 easy steps: quicksort(S) { 1. If the number of elements in S is 0 or 1, then return S 2. Pick any element v in S. This is called the pivot. 3. Partition S-{v} into 2 disjoint groups: S 1 ={xєS-{v}| x =v} 4. return {quicksort(S 1 ) {v}, quicksort(S 2 )} } Partition step ambiguous for elements equal to the pivot.

31 Picking the Pivot

Quicksort Start Unsorted Array Start with all data in an array, and consider it unsorted

Quicksort Step 1 26 Step 1, select a pivot (it is arbitrary) We will select the first element, as presented in the original algorithm pivot 1222

Quicksort Step 2 26 Step 2, start process of dividing data into LEFT and RIGHT groups: The LEFT group will have elements less than the pivot. The RIGHT group will have elements greater that the pivot. Use markers left and right left pivot 1222 right

Quicksort Step 3 26 Step 3, If left element belongs to LEFT group, then increment left index. If right index element belongs to RIGHT, then decrement right. Exchange when you find elements that belong to the other group left pivot 1222 right

Quicksort Step 4 26 Step 4: Element 33 belongs to RIGHT group. Element 22 belongs to LEFT group. Exchange the two elements left pivot 1222 right left pivot 1233 right

Quicksort Step 5 26 Step 5: After the exchange, increment left marker, decrement right marker left pivot 1233 right

Quicksort Step 6 26 Step 6: Element 35 belongs to RIGHT group. Element 12 belongs to LEFT group. Exchange, increment left, and decrement right left pivot 1233 right left pivot 3533 right

Quicksort Step 7 26 Step 7: Element 29 belongs to RIGHT. Element 19 belongs to LEFT. Exchange, increment left, decrement right left pivot 3533 right left pivot 3533 right

Quicksort Step 8 26 Step 8: When the left and right markers pass each other, we are done with the partition task. Swap the right with pivot left pivot 3533 right pivot 3533 LEFTRIGHT

Quicksort Step 9 Step 9: Apply Quicksort to the LEFT and RIGHT groups, recursively. Assemble parts when done pivot previous pivot 3533 Quicksort pivot

Example Recursive implementation with the left most array entry selected as the pivot element.

Quicksort code p: first element r: last element Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r) Quicksort(A, p, q-1) Quicksort(A, q+1, r) } Initial call is Quicksort(A, 1, n), where n in the length of A 43

Partition Clearly, all the action takes place in the partition() function – Rearranges the subarray in place – End result: Two subarrays All values in first subarray  all values in second – Returns the index of the “pivot” element separating the two subarrays 44

Partition Code Partition(A, p, r) { x = A[r]// x is pivot i = p - 1 for j = p to r – 1 { do if A[j] <= x then { i = i + 1 exchange A[i]  A[j] } exchange A[i+1]  A[r] return i+1 } 45

Heap A heap 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. H EAP S ORT 46

Binary Tree a binary tree is a tree data structure in which each node has at most two children (referred to as the left child and the right child).treedata structurechildren In a binary tree, the degree of each node can be at most two. Binary trees are used to implement binary search and heap sortbinary search heapsort Parent node Left child node Right child node

Binary Tree Each node of the binary tree corresponds to an element of the array. The array is completely filled on all levels except possibly lowest PARENT (i) return floor(i/2)-1 LEFT (i) return 2i+1 RIGHT (i) return 2i Array A

Binary Tree I array size = 12 Level=0 Level=1 Level=2 Level=3 Binary tree of Size 12 and depth 3

Binary tree A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible(left justified). This type of tree is used as a specialized data structure called a heap.heap Compete or not??

What is a Binary Heap? A binary heap is a complete binary tree with one (or both) of the following heap order properties: MinHeap property: Each node must have a key that is less or equal to the key of each of its children. MaxHeap property: Each node must have a key that is greater or equal to the key of each of its children. A binary heap satisfying the MinHeap property is called a MinHeap. A binary heap satisfying the MaxHeap property is called a MaxHeap. Recall: A complete binary tree may have missing nodes only on the right side of the lowest level.

Max Heap Example Array A

Min heap example Array A

siftUp Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child This is sometimes called sifting up Notice that the child may have lost the heap property Blue node has Maxheap property Blue node does not have Maxheap property 54

Plan of attack First, we will learn how to turn a binary tree into a heap Next, we will learn how to turn a binary tree back into a heap after it has been changed in a certain way Finally (this is the cool part) we will see how to use these ideas to sort an array 55

Constructing a heap I A tree consisting of a single node is automatically a heap We construct a heap by adding nodes one at a time: – Add the node just to the right of the rightmost node in the deepest level – If the deepest level is full, start a new level Examples: Add a new node here 56

Constructing a heap II Each time we add a node, we may destroy the heap property of its parent node To fix this, we sift up But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent node We repeat the sifting up process, moving up in the tree, until either – We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or – We reach the root 57

Constructing a heap III

Other children are not affected The node containing 8 is not affected because its parent gets larger, not smaller The node containing 5 is not affected because its parent gets larger, not smaller The node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally

A sample heap Here’s a sample binary tree after it has been heapified Notice that heapified does not mean sorted Heapifying does not change the shape of the binary tree;

Removing the root Notice that the largest number is now in the root Suppose we discard the root: How can we fix the binary tree so it is once again ? Solution: remove the rightmost leaf at the deepest level and use it for the new root

The reHeap method I Our binary tree is no longer have the Max heap property However, only the root lacks the Maxheap property We can siftUp() the root After doing this, one and only one of its children may have lost the Maxheap property

The reHeap method II Now the left child of the root (still the number 11 ) lacks the Maxheap property We can siftUp() this node After doing this, one and only one of its children may have lost the Maxheap property

The reHeap method III Now the right child of the left child of the root (still the number 11 ) lacks the Maxheap property: We can siftUp() this node After doing this, one and only one of its children may have lost the Maxheap property —but it doesn’t, because it’s a leaf

The reHeap method IV Our tree is once again a heap, because every node in it has the Maxheap property Once again, the largest (or a largest) value is in the root We can repeat this process until the tree becomes empty This produces a sequence of values in order largest to smallest

Sorting What do heaps have to do with sorting an array? Here’s the neat part: – Because the compete binary tree can be represented as an array – All our operations on the complete binary trees can be represented as operations on arrays – To sort: heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; } 66

Mapping into an array Notice: – The left child of index i is at index 2*i+1 – The right child of index i is at index 2*i+2 – Example: the children of node 3 (19) are 7 (18) and 8 (14)

Removing and replacing the root The “root” is the first element in the array The “rightmost node at the deepest level” is the last element Swap them......And pretend that the last element in the array no longer exists—that is, the “last index” is 11 (9)

Array Representation of a Binary Heap A heap is a dynamic data structure that is represented and manipulated more efficiently using an array. Since a heap is a complete binary tree, its node values can be stored in an array, without any gaps, in a breadth-first order, where: Value(node i+1 ) array[ i ], for i > The root is array[0] The parent of array[i] is array[(i – 1)/2], where i > 0 The left child, if any, of array[i] is array[2i+1]. The right child, if any, of array[i] is array[2i+2].

Array Representation of a Binary Heap (contd.) We shall use an implementation in which the heap elements are stored in an array starting at index 1. Value(node i )array[i], for i > The root is array[1]. The parent of array[i] is array[i/2], where i > 1 The left child, if any, of array[i] is array[2i]. The right child, if any, of array[i] is array[2i+1].

Insertion Algorithm 1.Add the new element to the next available position at the lowest level 2.Restore the max-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is smaller than the element, then interchange the parent and child. OR Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent and child. 71

Insert 17 swap Percolate up to maintain the heap property 72

Deletion Delete max – Copy the last number to the root ( overwrite the maximum element stored there ). – Restore the max heap property by percolate down. Delete min – Copy the last number to the root ( overwrite the minimum element stored there ). – Restore the min heap property by percolate down. 73

Heap Sort A sorting algorithm that works by first organizing the data to be sorted into a special type of binary tree called a heap Heapify Build Heap Heap Sort P ROCEDURES ON H EAP 74

Heapify Heapify picks the largest child key and compare it to the parent key. If parent key is larger than heapify quits, otherwise it swaps the parent key with the largest child key. So that the parent is now becomes larger than its children. Heapify(A, i) { l  left(i) (using 2i+1 r  right(i) (using 2i+1 if l A[i] then largest  l else largest  i if r A[largest] then largest  r if largest != i then swap A[i]  A[largest] Heapify(A, largest) } 75

BUILD HEAP We can use the procedure 'Heapify' in a bottom-up fashion to convert an array A[1.. n] into a heap. Since the elements in the subarray A[n/ n] are all leaves, the procedure BUILD_HEAP goes through the remaining nodes of the tree and runs 'Heapify' on each one. The bottom-up order of processing node guarantees that the subtree rooted at children are heap before 'Heapify' is run at their parent. Buildheap(A) { heapsize[A]  length[A] for i   length[A]/2  down to 1 do Heapify(A, i) } 76

Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1.. n]. Since the maximum element of the array stored at the root A[1], it can be put into its correct final position by exchanging it with A[n] (the last element in A). If we now discard node n from the heap than the remaining elements can be made into heap. Note that the new element at the root may violate the heap property. All that is needed to restore the heap property. Heapsort(A) { Buildheap(A) for i  length[A] down to 2 do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) } 77

Example: Convert the following array to a heap Picture the array as a complete binary tree:

swap 79

Heap Sort The heapsort algorithm consists of two phases: - build a heap from an arbitrary array - use the heap to sort the data To sort the elements in the decreasing order, use a min heap To sort the elements in the increasing order, use a max heap

Example of Heap Sort Array A Sorted: Take out biggest Move the last element to the root 81

Array A Sorted: HEAPIFY() swap 82

Array A Sorted: 83

Array A Sorted: Take out biggest Move the last element to the root 84

Array A Sorted: 85

Array A Sorted: HEAPIFY() swap 86

Array A Sorted: 87

Array A Sorted: Take out biggest Move the last element to the root 88

Array A Sorted: swap 89

Array A Sorted: 90

Array A Sorted: Move the last element to the root Take out biggest 91

Array A Sorted: HEAPIFY() swap 92

Array A Sorted: Move the last element to the root Take out biggest 93

Array A Sorted: Take out biggest 94