Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.

Slides:



Advertisements
Similar presentations
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
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.
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
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.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
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.
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.
Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap.
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.
1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.
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.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
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.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
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:
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.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CSE 5392 Fall 2005 Week 4 Devendra Patel Subhesh Pradhan.
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:
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
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.
Algorithms Sorting. Sorting Definition It is a process for arranging a collection of items in an order. Sorting can arrange both numeric and alphabetic.
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
Dr. David Matuszek Heapsort Dr. David Matuszek
Heaps, Heapsort, and Priority Queues
Heapsort.
"Teachers open the door, but you must enter by yourself. "
Heapsort.
HEAPS.
Heapsort.
Heapsort.
Heaps and priority queues
Heapsort.
CO 303 Algorithm Analysis and Design
Presentation transcript:

Sorting Cont

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

3 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

4 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.

5 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 17

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 18

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 } 19

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 20

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 28

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 29

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 30

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 31

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; } 40

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. 45

Insert 17 swap Percolate up to maintain the heap property 46

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. 47

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 48

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) } 49

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) } 50

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) } 51

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

swap 53

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 55

Array A Sorted: HEAPIFY() swap 56

Array A Sorted: 57

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

Array A Sorted: 59

Array A Sorted: HEAPIFY() swap 60

Array A Sorted: 61

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

Array A Sorted: swap 63

Array A Sorted: 64

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

Array A Sorted: HEAPIFY() swap 66

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

Array A Sorted: Take out biggest 68