David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.

Slides:



Advertisements
Similar presentations
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Advertisements

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.
Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap.
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.
Heapsort Chapter 6. Heaps A data structure with  Nearly complete binary tree  Heap property: A[parent(i)] ≥ A[i] eg. Parent(i) { return } Left(i) {
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
David Luebke 1 9/8/2015 CS 332: Algorithms Review for Exam 1.
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
A Introduction to Computing II Lecture 10: Heaps Fall Session 2000.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Introduction to Algorithms
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
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.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
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.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Lecture 2 Sorting.
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heapsort.
Introduction to Algorithms
CSC 413/513: Intro to Algorithms
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
"Teachers open the door, but you must enter by yourself. "
HEAPS.
CS 332: Algorithms Quicksort David Luebke /9/2019.
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
Asst. Prof. Dr. İlker Kocabaş
Presentation transcript:

David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort

David Luebke 2 5/20/2015 Administrivia l Head count: how many people can’t make it to the scheduled office hours? l How many people would be interested in organizing tutors for this class? l Reminder: homework due tonight at midnight (drop box)

David Luebke 3 5/20/2015 Review: Heaps l A heap is a “complete” binary tree, usually represented as an array: A =

David Luebke 4 5/20/2015 Review: Heaps To represent a heap as an array: Parent(i) { return  i/2  ; } Left(i) { return 2*i; } right(i) { return 2*i + 1; }

David Luebke 5 5/20/2015 Review: The Heap Property l Heaps also satisfy the heap property: A[Parent(i)]  A[i]for all nodes i > 1 n In other words, the value of a node is at most the value of its parent n The largest value is thus stored at the root (A[1]) l Because the heap is a binary tree, the height of any node is at most  (lg n)

David Luebke 6 5/20/2015 Review: Heapify() Heapify() : maintain the heap property n Given: a node i in the heap with children l and r n Given: two subtrees rooted at l and r, assumed to be heaps n Action: let the value of the parent node “float down” so subtree at i satisfies the heap property u If A[i] < A[l] or A[i] < A[r], swap A[i] with the largest of A[l] and A[r] u Recurse on that subtree n Running time: O(h), h = height of heap = O(lg n)

David Luebke 7 5/20/2015 Review: BuildHeap() BuildHeap() : build heap bottom-up by running Heapify() on successive subarrays Walk backwards through the array from n/2 to 1, calling Heapify() on each node. n Order of processing guarantees that the children of node i are heaps when i is processed l Easy to show that running time is O(n lg n) l Can be shown to be O(n) n Key observation: most subheaps are small

David Luebke 8 5/20/2015 Review: Heapsort() Heapsort() : an in-place sorting algorithm: n Maximum element is at A[1] n Discard by swapping with element at A[n] u Decrement heap_size[A] u A[n] now contains correct value Restore heap property at A[1] by calling Heapify() n Repeat, always swapping A[1] for A[heap_size(A)] l Running time: O(n lg n) BuildHeap : O(n), Heapify : n * O(lg n)

David Luebke 9 5/20/2015 Tying It Into The Real World l And now, a real-world example…

David Luebke 10 5/20/2015 Tying It Into The “Real World” l And now, a real-world example…combat billiards n Sort of like pool... n Except you’re trying to kill the other players… n And the table is the size of a polo field… n And the balls are the size of Suburbans... n And instead of a cue you drive a vehicle with a ram on it l Problem: how do you simulate the physics? Figure 1: boring traditional pool

David Luebke 11 5/20/2015 Combat Billiards: Simulating The Physics l Simplifying assumptions: n G-rated version: No players u Just n balls bouncing around n No spin, no friction u Easy to calculate the positions of the balls at time T n from time T n-1 if there are no collisions in between n Simple elastic collisions

David Luebke 12 5/20/2015 Simulating The Physics l Assume we know how to compute when two moving spheres will intersect n Given the state of the system, we can calculate when the next collision will occur for each ball n At each collision C i : u Advance the system to the time T i of the collision u Recompute the next collision for the ball(s) involved u Find the next overall collision C i+1 and repeat n How should we keep track of all these collisions and when they occur?

David Luebke 13 5/20/2015 Review: Priority Queues l The heap data structure is often used for implementing priority queues n A data structure for maintaining a set S of elements, each with an associated value or key Supports the operations Insert(), Maximum(), and ExtractMax() n Commonly used for scheduling, event simulation

David Luebke 14 5/20/2015 Priority Queue Operations l Insert(S, x) inserts the element x into set S l Maximum(S) returns the element of S with the maximum key l ExtractMax(S) removes and returns the element of S with the maximum key

David Luebke 15 5/20/2015 Implementing Priority Queues HeapInsert(A, key) // what’s running time? { heap_size[A] ++; i = heap_size[A]; while (i > 1 AND A[Parent(i)] < key) { A[i] = A[Parent(i)]; i = Parent(i); } A[i] = key; }

David Luebke 16 5/20/2015 Implementing Priority Queues HeapMaximum(A) { // This one is really tricky: return A[i]; }

David Luebke 17 5/20/2015 Implementing Priority Queues HeapExtractMax(A) { if (heap_size[A] < 1) { error; } max = A[1]; A[1] = A[heap_size[A]] heap_size[A] --; Heapify(A, 1); return max; }

David Luebke 18 5/20/2015 Back To Combat Billiards l Extract the next collision C i from the queue l Advance the system to the time T i of the collision l Recompute the next collision(s) for the ball(s) involved l Insert collision(s) into the queue, using the time of occurrence as the key l Find the next overall collision C i+1 and repeat

David Luebke 19 5/20/2015 Using A Priority Queue For Event Simulation l More natural to use Minimum() and ExtractMin() l What if a player hits a ball? n Need to code up a Delete() operation n How? What will the running time be?

David Luebke 20 5/20/2015 Quicksort l Sorts in place l Sorts O(n lg n) in the average case l Sorts O(n 2 ) in the worst case l So why would people use it instead of merge sort?

David Luebke 21 5/20/2015 Quicksort l Another divide-and-conquer algorithm n The array A[p..r] is partitioned into two non- empty subarrays A[p..q] and A[q+1..r] u Invariant: All elements in A[p..q] are less than all elements in A[q+1..r] n The subarrays are recursively sorted by calls to quicksort n Unlike merge sort, no combining step: two subarrays form an already-sorted array

David Luebke 22 5/20/2015 Quicksort Code Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); }

David Luebke 23 5/20/2015 Partition Clearly, all the action takes place in the partition() function n Rearranges the subarray in place n End result: u Two subarrays u All values in first subarray  all values in second n Returns the index of the “pivot” element separating the two subarrays l How do you suppose we implement this function?

David Luebke 24 5/20/2015 Partition In Words l Partition(A, p, r): n Select an element to act as the “pivot” (which?) n Grow two regions, A[p..i] and A[j..r] u All elements in A[p..i] <= pivot u All elements in A[j..r] >= pivot n Increment i until A[i] >= pivot n Decrement j until A[j] <= pivot n Swap A[i] and A[j] n Repeat until i >= j n Return j

David Luebke 25 5/20/2015 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; Illustrate on A = {5, 3, 2, 6, 4, 1, 3, 7}; What is the running time of partition() ?

David Luebke 26 5/20/2015 Analyzing Quicksort l What will be the worst case for the algorithm? l What will be the best case for the algorithm? l Which is more likely? l Will any particular input elicit the worst case?

David Luebke 27 5/20/2015 Analyzing Quicksort l What will be the worst case for the algorithm? n Partition is always unbalanced u One subarray is size n - 1, the other is size 1 u This happens when q = p l What will be the best case for the algorithm? l Which is more likely? l Will any particular input elicit the worst case?

David Luebke 28 5/20/2015 Analyzing Quicksort l What will be the worst case for the algorithm? n Partition is always unbalanced l What will be the best case for the algorithm? n Partition is perfectly balanced u Both subarrays of size n/2 l Which is more likely? l Will any particular input elicit the worst case?

David Luebke 29 5/20/2015 Analyzing Quicksort l What will be the worst case for the algorithm? n Partition is always unbalanced l What will be the best case for the algorithm? n Partition is perfectly balanced l Which is more likely? n The latter, by far n Except... l Will any particular input elicit the worst case?

David Luebke 30 5/20/2015 Analyzing Quicksort l What will be the worst case for the algorithm? n Partition is always unbalanced l What will be the best case for the algorithm? n Partition is perfectly balanced l Which is more likely? n The latter, by far, except... l Will any particular input elicit the worst case? n Yes: Already-sorted input

David Luebke 31 5/20/2015 Analyzing Quicksort l In the worst case: T(1) =  (1) T(n) = T(n - 1) +  (n) l What does this work out to? T(n) =  (n 2 )

David Luebke 32 5/20/2015 Analyzing Quicksort l In the best case: T(n) = 2T(n/2) +  (n) l What does this work out to? T(n) =  (n lg n) (by the Master Theorem)

David Luebke 33 5/20/2015 Improving Quicksort l The real liability of quicksort is that it runs in O(n 2 ) on already-sorted input n What can we do about this? l Book discusses two solutions: n Randomize the input array n Pick a random pivot element l How will these solve the problem?

David Luebke 34 5/20/2015 Coming Up l Up next: Analyzing randomized quicksort