Data Structure & Algorithm Lecture 6 – QuickSort & Randomization JJCAO Most materials are stolen from Prof. Yoram Moses’s course.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Quicksort Lecture 3 Prof. Dr. Aydın Öztürk. Quicksort.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
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.
Introduction to Algorithms Jiafen Liu Sept
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
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.
Chapter 7: Sorting Algorithms
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Analysis of Algorithms
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
Sorting Algorithms and Average Case Time Complexity
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
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.
Linear-Time Selection Randomized Selection (Algorithm) Design and Analysis of Algorithms I.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Sorting Chapter 10.
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.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
S: Application of quicksort on an array of ints: partitioning.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Data Structure & Algorithm Lecture 7 – Linear Sort JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
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.
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
Introduction to Algorithms Jiafen Liu Sept
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Order Statistics(Selection Problem)
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
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.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
QuickSort Choosing a Good Pivot Design and Analysis of Algorithms I.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
CS2420: Lecture 12 Vladimir Kulyukin Computer Science Department Utah State University.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Sorting Fundamental Data Structures and Algorithms Klaus Sutner February 17, 2004.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Order Statistics(Selection Problem)
ITEC 2620M Introduction to Data Structures
Presentation transcript:

Data Structure & Algorithm Lecture 6 – QuickSort & Randomization JJCAO Most materials are stolen from Prof. Yoram Moses’s course.

Something Else I hear and I forget I see and I memorize I write and I understand 2

ACM ICPC

Content Quicksort: Worst-case: θ(n^2), Expected-case: θ(cnlgn), c is quite small In place Behavior of it is complex, so … Random sampling to prevent worst-case 4

Mergesort(A[1, n]) Merge( MergeSort(A[1,n/2]), MergeSort(A[n/2+1,n]) ) 5

QuickSort 6

Quicksort To sort A[1..n]: Quick-Sort(A,1,n) 7

Partition 8

9

10

11

Quick-Sort: Running Time n = right - left. T(n) depends on the position of p in the range [left,...,right] 12

Worst-Case 13

Worst-Case Running Time 14

Quick-Sort Best-Case Running Time 15 θ(n) 2θ(n/2) n/2θ(2) The average-case running time of quicksort is much closer to the best case than to the worst case.

Average-Case Running Time 16

Average-Case Running Time 17

Average-Case Running Time 18

Average-Case Running Time 19

Average-Case Running Time 20 //Harmonic Series 的和,上界是 O(lgn)

Average-Case Running Time 21

Randomization Randomly permute the input (before sorting) – tree of all possible executions most of them finish fast Choose partitioning element e randomly at each iteration – easier to analyze – same "good" behavior 22

Randomized-Partition 23

Variants of Quick-Sort When we get to small sub-arrays, do not call Quick-Sort recursively – Run Insertion-Sort on the sub-arrays – Go back up recursion and run Insertion-Sort on the nearly sorted array Different ways to pick the pivot element: – Always pick the first element – Pick a random element – Pick the median of some elements 24

Using Medians of 3 25

Randomized Medians of 3 26

Notes on Quick-Sort Quicksort: Fast on average: θ(cnlgn), c is quite small In place Behavior of it is complex Worst-case: θ(n^2), but unlikely with medians-of-3 which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk. embedded systems with real-time constraints or systems concerned with security often use heapsort 27

Homework 4 Hw04-GuiQtScribble Deadline: 22:00, Oct. ?,