10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February 2000 1946: The Metropolis.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
CSE 3101: Introduction to the Design and Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Using Divide and Conquer for Sorting
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
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
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
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.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
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.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
Quicksort!. A practical sorting algorithm Quicksort  Very efficient Tight code Good cache performance Sort in place  Easy to implement  Used in older.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
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.
Analysis of Algorithms CS 477/677
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Deterministic and Randomized Quicksort Andreas Klappenecker.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Sorting Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
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.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Analysis of Algorithms CS 477/677
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
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
Ch 7: Quicksort Ming-Te Chi
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Order Statistics Comp 550, Spring 2015.
CS 583 Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Algorithms: Design and Analysis
Order Statistics Comp 122, Spring 2004.
The Selection Problem.
Design and Analysis of Algorithms
Presentation transcript:

10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis Algorithm for Monte Carlo 1947: Simplex Method for Linear Programming // Com S 477/577, 418/ : Krylov Subspace Iteration Method 1951: The Decompositional Approach to Matrix Computations 1957: The Fortran Optimizing Compiler 1959: QR Algorithm for Computing Eigenvalues 1962: Quicksort Algorithms for Sorting 1965: Fast Fourier Transform // Com S 477/ : Integer Relation Detection 1987: Fast Multipole Method With the greatest influence on the development and practice of science and engineering …

Quicksort C. A. R. Hoare 1962 Best practical sorting algorithm! p r pq prqq+1r A divide conquer After partition, A[i]  A[j], p  i  q and q+1  j  r. No work is needed on combining A[p..q] and A[q+1..r]!

How to Partition? pivot x i = p–1p j = r+1r j i  x x  x x ij

Partitioning ij  x x  x x ji jiq = A[p..q] A[q+1..r]

The Procedure Partition(A, p, r) x  A[p] i  p – 1 j  r + 1 while true do repeat j  j – 1 until A[j]  x repeat i  i + 1 until A[i]  x if i < j then A[i]  A[j] else return j // different from the procedure in the textbook

The Quicksort Algorithm Quicksort(A, p, r) if p < r then q  Partition(A, p, r) Quicksort(A, p, q) Quicksort(A, q+1, r)

Performance of Quicksort Depends on how balanced the partitioning is. Worst Case: p = q < r Let n = r – p + 1 n 2 … n – n – 2 1  (n ) time 2

A Worst-Case Example The input array is already sorted. 2, 4, 5, 9, , 5, 9, , 9, ,

Best Case q – p + 1 = (r – p + 1) / 2 recursion tree n n/2 n/4 n/4 cost cn lg n  (n lg n)

Balanced Partitioning Suppose always a 9-to-1 proportional split. T(n) = T(9n/10) + T(n/10) + n n n/10 9n/10 n/100 9n/100 9n/100 81n/100 81n/ n/ cn  cn log n 10 log n 10/9 T(n) =  (n lg n)

Average Case Imagine a bad split followed by a good split. n 1 n – 1 (n – 1) / 2 Cost of two-step partitioning: n + n – 1 =  (n) n (n – 1) / (n – 1) / 2 similar to Cost of one-step partitioning: n =  (n) T(n) =  (n lg n)

Comparison of Sorting Methods Method Space Average Max n=16 n=10000 Insertion sort n(1+  ) 1.25n n 2.5n Merge sort n(1+  ) 14.43n lnn n 14.4n lnn Heapsort n 23.08n lnn n 24.5n lnn Quicksort n + 2  lgn 11.67n lnn – 1.74n  2n Median-of-3 n + 2  lgn 10.63n lnn – 2.12n  n Quicksort (Use the median of three randomly picked elements as the pivot.) D. E. Knuth, “The Art of Computer Programming”, Vol 3, 2 nd ed., p.382, Implemented on the MIX Computer.

Issue with Quicksort Quicksort has the best average-case behavior. All permutations of the input sequence are assumed to be equally likely. Not necessarily true in practice! Make its behavior independent of the input ordering!

Randomization An algorithm is randomized if its behavior depends on  the input  random numbers No particular input causes its worst-case behavior. Choose the pivot at random! Randomized-Partition(A, p, r) i  Random(p, r) A[p]  A[i] return Partition(A, p, r)

An Example p r Execution 1: Random(p, r) = p iijjjjjj j

Cont’d p r Execution 2: Random(p, r) = p iijjjjiiii ijj

Randomized Quicksort Randomized-Quicksort(A, p, r) if p < r then q  Randomized-Partition(A, p, r) Randomized-Quicksort(A, p, q) Randomized-Quicksort(A, q+1, r) A random sequence of good and bad choices can yield an efficient algorithm. Height of recursion tree:  (lg n).

Analysis of Partitioning Assumption for simplification: All elements are distinct. x…… prq A:A: After random partitioning A[k]  x p  k  q A[k]  x q+1  k  r rank(x) is # elements  x

Ranks and Probabilities  rank(x) = 1 x p = q  rank(x)  2 …x…… p q p – q + 1 = rank(x) – 1 Probability P{rank(x) = i } = 1/n, 1  i  n = r – p + 1  P{q = p} = 2/n, if rank(x) = 1, 2  P{q – p + 1 = i} = 1/n, if rank(x) = i+1, i = 2, …, n–1

Average-case Running Time of Randomized-Quicksort T(n) = (1/n) ( T(1) + T(n – 1) +  ( T(q) + T(n – q) ) ) +  (n) rank(x) = 1 rank(x) = q + 1 T(n) = O(n lg n) But T(n) cannot better the best case running time  (n lg n) of quicksort. (see a separate.pdf handout) Running time is  (n lg n)! q = 1 n – 1 So T(n) =  (n lg n).