242-535 ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CSE 3101: Introduction to the Design and Analysis of Algorithms
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Introduction to Algorithms Jiafen Liu Sept
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
CSE 373: Data Structures and Algorithms
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Quicksort.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
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.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Order Statistics(Selection Problem)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Quick Sort Divide: Partition the array into two sub-arrays
Introduction to Algorithms Prof. Charles E. Leiserson
CSC 413/513: Intro to Algorithms
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
slides adapted from Marty Stepp
EE 312 Software Design and Implementation I
CS 3343: Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
Presentation transcript:

ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions Algorithm Design and Analysis (ADA) , Semester Quicksort

ADA: 5. Quicksort2 1.Quicksort 2.Partitioning Function 3.Analysis of Quicksort 4.Quicksort in PracticeOverview

ADA: 5. Quicksort3 Proposed by Tony Hoare in Voted one of top 10 algorithms of 20th century in science and engineering o A divide-and-conquer algorithm. Sorts “ in place ” -- rearranges elements using only the array, as in insertion sort, but unlike merge sort which uses extra storage. Very practical (after some code tuning). 1. Quicksort

ADA: 5. Quicksort4 Quicksort an n-element array: 1. Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray ≤ x ≤ elements in upper subarray. 2. Conquer: Recursively sort the two subarrays. 3. Combine: Nothing to do. Key: implementing a linear-time partitioning function Divide and conquer

ADA: 5. Quicksort5 quicksort(int[] A, int left, int right) if (left < right) // If the array has 2 or more items pivot = partition (A, left, right) // recursively sort elements smaller than the pivot quicksort(A, left, pivot-1) // recursively sort elements bigger than the pivot quicksort(A, pivot+1, right)Pseudocode

ADA: 5. Quicksort6 Quicksort Diagram pivot

ADA: 5. Quicksort7 quicksort will stop when the subarray is 0 or 1 element big. When the subarray gets to a small size, switch over to dedicated sorting code rather than relying on recursion. quicksort is tail-recursive, a recursive behaviour which can be optimized. Fine Tuning the Code

ADA: 5. Quicksort8 Tail-call optimization avoids allocating a new stack frame for a called function. o It isn't necesary because the calling function only returns the value that it gets from the called function. The most common use of this technique is for optimizing tail-recursion o the recursive function can be rewritten to use a constant amount of stack space (instead of linear) Tail-Call Optimization

ADA: 5. Quicksort9 Before applying tail-call optimization: Tail-Call Graphically After applying it:

Pseudocode Before: int foo(int n) { if (n == 0) return A(); else { int x = B(n); return foo (x); } After: int foo(int n) { if (n == 0) return A(); else { int x = B(n); goto start of foo() code with x as argument value }

ADA: 5. Quicksort11 PARTITION(A, p, q) // A[p.. q] x ← A[p] // pivot = A[p] Running time i ← p// index = O(n) for n for j ← p + 1 to q elements. if A[ j] ≤ x then i ← i + 1 // move the i boundary exchange A[i] ↔ A[ j] // switch big and small exchange A[p] ↔ A[i] return i // return index of pivot 2. Partitioning Function

ADA: 5. Quicksort12 Example of partitioning scan right until find something less than the pivot scan right until find something less than the pivot

ADA: 5. Quicksort13 Example of partitioning

ADA: 5. Quicksort14 Example of partitioning

ADA: 5. Quicksort15 Example of partitioning swap 10 and 5

ADA: 5. Quicksort16 Example of partitioning resume scan right until find something less than the pivot

ADA: 5. Quicksort17 Example of partitioning

ADA: 5. Quicksort18 Example of partitioning

ADA: 5. Quicksort19 Example of partitioning swap 13 and 3

ADA: 5. Quicksort20 Example of partitioning swap 10 and 2

ADA: 5. Quicksort21 Example of partitioning

ADA: 5. Quicksort22 Example of partitioning j runs to the end

ADA: 5. Quicksort23 Example of partitioning swap pivot and 2 so in the middle

ADA: 5. Quicksort24 The analysis is quite tricky. Assume all the input elements are distinct o no duplicate values makes this code faster! o there are better partitioning algorithms when duplicate input elements exist (e.g. Hoare's original code) Let T(n) = worst-case running time on an array of n elements. 3. Analysis of Quicksort

ADA: 5. Quicksort25 QUICKSORT runs very slowly when its input array is already sorted (or is reverse sorted). o almost sorted data is quite common in the real-world This is caused by the partition using the min (or max) element which means that one side of the partition will have has no elements. Therefore: T(n) = T(0) +T(n-1) + Θ(n) = Θ(1) +T(n-1) + Θ(n) = T(n-1) + Θ(n) = Θ(n 2 ) (arithmetic series) 3.1. Worst-case of quicksort no elements n-1 elements

ADA: 5. Quicksort26 T(n) = T(0) +T(n-1) + cn Worst-case recursion tree

ADA: 5. Quicksort27 T(n) = T(0) +T(n-1) + cn T(n) Worst-case recursion tree

ADA: 5. Quicksort28 T(n) = T(0) +T(n-1) + cn cn T(0) T(n-1) Worst-case recursion tree

ADA: 5. Quicksort29 T(n) = T(0) +T(n-1) + cn cn T(0) c(n-1) T(0) T(n-2) Worst-case recursion tree

ADA: 5. Quicksort30 T(n) = T(0) +T(n-1) + cn cn T(0) c(n-1) T(0) T(n-2) T(0) Θ(1) Worst-case recursion tree

ADA: 5. Quicksort31 T(n) = T(0) +T(n-1) + cn Worst-case recursion tree

ADA: 5. Quicksort32 In the worst case, quicksort isn't any quicker than insertion sort. So why bother with quicksort? It's average case running time is very good, as we'll see. Quicksort isn't Quick?

ADA: 5. Quicksort33 If we’re lucky, PARTITION splits the array evenly: T(n) = 2T(n/2) + Θ(n) = Θ( n log n ) (same as merge sort) 3.2. Best-case Analysis Case 2 of the Master Method Case 2 of the Master Method

ADA: 5. Quicksort34 What if the split is always 1/10 : 9/10? T(n) = T(1/10n) + T(9/10n) + Θ(n) 3.3. Almost Best-case

ADA: 5. Quicksort35 T(n) Analysis of “ almost-best ” case

ADA: 5. Quicksort36 cn T(1/10n) T(9/10n) Analysis of “ almost-best ” case

ADA: 5. Quicksort37 cn T(1/10n) T(9/10n) T(1/100n ) T(9/100n) T(9/100n) T(81/100n) Analysis of “ almost-best ” case

ADA: 5. Quicksort38 Analysis of “ almost-best ” case

ADA: 5. Quicksort39 Analysis of “ almost-best ” case short path short path long path long path cn * short path cn * long path all leaves

ADA: 5. Quicksort40 Short path node value: n  (1/10)n  (1/10) 2 n ...  1  n(1/10) sp = 1  n = 10 sp // take logs  log 10 n = sp Long path node value: n  (9/10)n  (9/10) 2 n ...  1  n(9/10) lp = 1  n = (10/9) lp // take logs  log 10/9 n = lp Short and Long Path Heights sp steps lp steps

ADA: 5. Quicksort41 Suppose we alternate good, bad, good, bad, good, partitions …. G(n) = 2B(n/2) + Θ(n) good B(n) = L(n – 1) + Θ(n) bad Solving: G(n) = 2( G(n/2 – 1) + Θ(n/2) ) + Θ(n) = 2G(n/2 – 1) + Θ(n) = Θ(n log n) How can we make sure we choose good partitions? 3.4. Good and Bad Good!

ADA: 5. Quicksort42 IDEA : Partition around a random element. Running time is then independent of the input order. No assumptions need to be made about the input distribution. No specific input leads to the worst-case behavior. The worst case is determined only by the output of a random-number generator. Randomized Quicksort

ADA: 5. Quicksort43 Quicksort is a great general-purpose sorting algorithm. o especially with a randomized pivot o Quicksort can benefit substantially from code tuning o Quicksort can be over twice as fast as merge sort Quicksort behaves well even with caching and virtual memory. 4. Quicksort in Practice

ADA: 5. Quicksort44 Running time estimates: Home PC executes 10 8 compares/second. Supercomputer executes compares/second Timing Comparisons Lesson 1. Good algorithms are better than supercomputers. Lesson 2. Great algorithms are better than good ones.