CSE 3101: Introduction to the Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Sorting in linear time (for students to read) Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They.
Advertisements

1 More Sorting; Searching Dan Barrish-Flood. 2 Bucket Sort Put keys into n buckets, then sort each bucket, then concatenate. If keys are uniformly distributed.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Sorting in Linear Time Comp 550, Spring Linear-time Sorting Depends on a key assumption: numbers to be sorted are integers in {0, 1, 2, …, k}. Input:
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
Sorting in linear time Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They are possible in linear.
Counting Sort Non-comparison sort. Precondition: n numbers in the range 1..k. Key ideas: For each x count the number C(x) of elements ≤ x Insert x at output.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Lower bound for sorting, radix sort COMP171 Fall 2005.
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:
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.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
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.
Quicksort.
Divide and Conquer Sorting
Analysis of Algorithms CS 477/677
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
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.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
1 Sorting in O(N) time CS302 Data Structures Section 10.4.
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.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
Analysis of Algorithms CS 477/677
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
Sorting CS 110: Data Structures and Algorithms First Semester,
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.
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 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
19 March More on Sorting CSE 2011 Winter 2011.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Introduction to Algorithms Prof. Charles E. Leiserson
Algorithms and Data Structures Lecture VI
Insertion Sort
Linear Sorting Sections 10.4
Quick Sort (11.2) CSE 2011 Winter November 2018.
Sorting in linear time (for students to read)
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Linear Sorting Sorting in O(n) Jeff Chastine.
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
CS 583 Analysis of Algorithms
Linear Sorting Section 10.4
Presentation transcript:

CSE 3101: Introduction to the Design and Analysis of Algorithms Suprakash Datta datta[at]cse.yorku.ca 4/13/2017 CSE 3101 1

Characteristics Quick Sort sorts ”almost” in place, i.e., does not require an additional array, like insertion sort Divide-and-conquer, like merge sort very practical, average sort performance O(n log n) (with small constant factors), but worst case O(n2) [CAVEAT: this is true for the CLRS version]

Quick Sort – the main idea To understand quick-sort, let’s look at a high-level description of the algorithm A divide-and-conquer algorithm Divide: partition array into 2 subarrays such that elements in the lower part <= elements in the higher part Conquer: recursively sort the 2 subarrays Combine: trivial since sorting is done in place

Linear time partitioning procedure j Partition(A,p,r) 01   x¬A[r] 02   i¬p-1 03   j¬r+1 04   while TRUE 05   repeat j¬j-1 06   until A[j] £x 07   repeat i¬i+1 08   until A[i] ³x 09   if i<j 10   then exchange A[i]«A[j] 11   else return j 17 12 6 19 23 8 5 10 £ X=10 £ i j 10 12 6 19 23 8 5 17 i j 10 5 6 19 23 8 12 17 j i 10 5 6 8 23 19 12 17 10 5 6 8 23 19 12 17

Initial call Quicksort(A, 1, length[A]) Quick Sort Algorithm Initial call Quicksort(A, 1, length[A]) Quicksort(A,p,r) 01   if p<r 02   then q¬Partition(A,p,r) 03     Quicksort(A,p,q) 04     Quicksort(A,q+1,r)

Assume that all input elements are distinct Analysis of Quicksort Assume that all input elements are distinct The running time depends on the distribution of splits

Best Case If we are lucky, Partition splits the array evenly

Using the median as a pivot The recurrence in the previous slide works out, BUT…… Q: Can we find the median in linear-time? A: Yes! Chapter 9 of the text Note : Most implementations do not use the median as pivot.

One side of the parition has only one element Worst Case What is the worst case? One side of the parition has only one element

Worst Case (2)

When does the worst case appear? input is sorted input reverse sorted Same recurrence for the worst case of insertion sort However, sorted input yields the best case for insertion sort!

Suppose the split is 1/10 : 9/10 Analysis of Quicksort Suppose the split is 1/10 : 9/10

An Average Case Scenario Suppose, we alternate lucky and unlucky cases to get an average behavior n n larger constant in the O notation n-1 1 (n-1)/2 (n-1)/2 (n-1)/2+1 (n-1)/2

An Average Case Scenario (2) How can we make sure that we are usually lucky? Partition around the ”middle” (n/2th) element? Partition around a random element (works well in practice) Randomized algorithm running time is independent of the input ordering no specific input triggers worst-case behavior the worst-case is only determined by the output of the random-number generator

Assume all elements are distinct Partition around a random element Randomized Quicksort Assume all elements are distinct Partition around a random element Randomization is often used to design algorithms with good average-case complexity (the worst-case complexity may not be as good)

The optimality question Q: Can we do better that worst case Q(n log n) time for sorting? A: In general no, but in some special cases yes! Q: Why not? A: The well-known (n log n) lower bound.

“the best any algorithm can do” for a problem On Lower Bounds “the best any algorithm can do” for a problem The proof must be algorithm independent In general, lower bound proofs are difficult Must make some assumptions – the sorting lower bound assumes that sorting is comparison based. This will be covered later today, or by Prof. Ruppert next week If we relax the “comparison-based” assumption, we can sort in linear time!

Q: How we beat the (n log n) lower bound for sorting? Next: Linear sorting Q: How we beat the (n log n) lower bound for sorting? A: By making extra assumptions about the input

Non-Comparison Sort – Bucket Sort Assumption: uniform distribution Input numbers are uniformly distributed in [0,1). Suppose input size is n. Idea: Divide [0,1) into n equal-sized subintervals (buckets). Distribute n numbers into buckets Expect that each bucket contains few numbers. Sort numbers in each bucket (insertion sort as default). Then go through buckets in order, listing elements Can be shown to run in linear-time on average

Example of BUCKET-SORT

Generalizing Bucket Sort Q: What if the input numbers are NOT uniformly distributed in [0,1)? A: Can be generalized in different ways, e.g. if the distribution is known we can design (unequal sized) bins that will have roughly equal number of numbers on average.

Non-Comparison Sort – Counting Sort Assumption: n input numbers are integers in the range [0,k], k=O(n). Idea: Determine the number of elements less than x, for each input x. Place x directly in its position.

Counting Sort - pseudocode Counting-Sort(A,B,k) for i0 to k do C[i] 0 for j 1 to length[A] do C[A[j]] C[A[j]]+1 // C[i] contains number of elements equal to i. for i 1 to k do C[i]=C[i]+C[i-1] // C[i] contains number of elements  i. for j length[A] downto 1 do B[C[A[j]]] A[j] C[A[j]] C[A[j]]-1

Counting Sort - example

Counting Sort - analysis for i0 to k (k) do C[i] 0 (1) for j 1 to length[A] (n) do C[A[j]] C[A[j]]+1 (1) ((1) (n)= (n)) // C[i] contains number of elements equal to i. (0) for i 1 to k (k) do C[i]=C[i]+C[i-1] (1) ((1) (n)= (n)) // C[i] contains number of elements  i. (0) for j length[A] downto 1 (n) do B[C[A[j]]] A[j] (1) ((1) (n)= (n)) C[A[j]] C[A[j]]-1 (1) ((1) (n)= (n)) Total cost is (k+n), suppose k=O(n), then total cost is (n). So, it beats the (n log n) lower bound!

Preserves order of elements with the same key. Stable sort Preserves order of elements with the same key. Counting sort is stable. Crucial question: can counting sort be used to sort large integers efficiently?

do use a stable sort to sort A on digit i Analysis: Radix sort Radix-Sort(A,d) for i1 to d do use a stable sort to sort A on digit i Analysis: Given n d-digit numbers where each digit takes on up to k values, Radix-Sort sorts these numbers correctly in (d(n+k)) time.

Radix sort - example 1019 2225 2231 3075 1019 3075 2225 2231 2231 3075 2225 1019 1019 2225 2231 3075 1019 3075 2225 2231 Sorted! 1019 3075 2231 2225 1019 2231 2225 3075 Not sorted!