What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Advertisements

Non-Comparison Based Sorting
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
MS 101: Algorithms Instructor Neelima Gupta
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Lower bound for sorting, radix sort COMP171 Fall 2006.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Data Structure & Algorithm Lecture 7 – Linear Sort JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
CSE373: Data Structures & Algorithms Lecture 20: Beyond Comparison Sorting Dan Grossman Fall 2013.
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.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
Lower Bounds & Sorting in Linear Time
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Sorting Lower Bound 4/25/2018 8:49 PM
MCA 301: Design and Analysis of Algorithms
Introduction to Algorithms
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
Lower Bound Theory.
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time Ming-Te Chi
Lecture 5 Algorithm Analysis
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
Count Sort, Bucket Sort, Radix Sort
Linear Sorting Sorting in O(n) Jeff Chastine.
Data Structures Sorting Haim Kaplan & Uri Zwick December 2014.
Lower Bounds & Sorting in Linear Time
Linear-Time Sorting Algorithms
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Lecture 5 Algorithm Analysis
CS 3343: Analysis of Algorithms
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Lower bound for sorting, radix sort
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
CS 583 Analysis of Algorithms
The Selection Problem.
Sorting We have actually seen already two efficient ways to sort:
Lecture 5 Algorithm Analysis
Presentation transcript:

What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above

Comparison-Based Sorting is a: 1.o(n ) problem 2.  (n 2 ) problem 3.O(log n) problem 4.  (n log n) problem 5.None of the above

What are the limitations of using comparisons? What are the alternatives?

We know sorting is O(n log n) Proved by the existence of Merge Sort How do we how sorting is  (n log n)? Write out every possible algorithm and show that each is  (n log n)? That might take a while Better idea: Find a computation model that we can analyze

Any sorting algorithms is a series of decisions Start with list: i 1,i 2,i 3,…,i n Compare i j and i k –Value of j and k depend on sort –Swap if needed Compare i j’ and i k’ –Value of j’ and k’ depend on sort and previous answer –Swap if needed Continue…

i 1,i 2,i 3,i 4,…,i n i 1 < i 2 ? i 1,i 2,i 3,i 4,…,i n i 2 <i 3 ? Yes i 2,i 1,i 3,i 4,…,i n i 1 <i 3 ? No i 1,i 2,i 3,i 4,…,i n i 3 < i 4 i 1,i 3,i 2,i 4,…,i n i 2 <i 4 ? Yes No i 2,i 1,i 3,i 4,…,i n i 3 <i 4 ? i 2,i 3,i 1,i 4,…,i n i 2 <i 4 ? YesNo

A tree leaf indicates where we stopped asking questions The ordering of the leaf at which we end up must be the sorted order The depth of the leaf is determined by the number of comparisons leading to it

How many leaves? Every possible ordering of i 1,i 2,…,i n is a solution for some possible input

How many leaves must the tree have? 1.  (n 2 ) 2.  (2 n ) 3.  (n!) 4.  (n n ) 5.None of the above

How many leaves? Every possible ordering of i 1,i 2,…,i n is a solution for some possible input There are n! possible orderings Any decision tree has n! leaves

Best we can hope for is: Balanced tree n! leaves What is the height of a balanced tree? log 2 (number of leaves) Easy to prove from previous results

What is the height of the tree? 1.  (log n!) 2.  (log 2 n ) 3.  (log n 2 ) 4.  (log n) 5.None of the above

Our height/ runtime is log 2 n! Not useful -- how bad is this? Stirling’s Approximation: n! ~ (2  ) 1/2 n n+1/2 /e n Follows: log n! ~ c 1 + c 2 n log n - c 3 n =  (n log n)

Basic Idea: Look at the list Count the number of times each element appears in the list Reconstruct list from the count Notice: Only works on integers –Or sets that can be mapped to the integers For example: Letters (A=0, B=1, C=2, …) Never compares two elements -- previous proof is not applicable!!!

List to be sorted Array for all possible values

What is the runtime of Counting Sort? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above

Consider the steps: –Allocate memory for the counting array: O(1) Run through the list: O(n) Anything else? All elements of the array must initially be set to 0: O(M) –Where M is the largest element of the array Finding M: O(n) Transverse array at the end: O(M) Total: O(n+M)

Sorting Social Security Numbers O(n + 999,999,999)=O(n) Sorting by Birth Year O(n ) = O(n) Sorting n random numbers from (1,n 2 ) O(n + n 2 ) = O(n 2 ) Sorting n random numbers O(n +  ) = O(  )

As implemented: We cannot carry any information with the sorted values Input: (name, birth year) pairs Question: How do we sort names by birth year using counting sort?

Abbott, H.1980 Bell, K.1979 Bones, S.1980 Creevy, C.1981 Diggory, C.1978 Fawcett, S.1980 Jordan, L.1978 Patil, P.1980 Wood, O Wood, O.Diggory, C.Abbott, H.Creevy, C.Bell, K.Jordan, L.Bones, S.Fawcett, S.Patil, P.

Abbott, H.1980 Bell, K.1979 Bones, S.1980 Creevy, C.1981 Diggory, C.1978 Fawcett, S.1980 Jordan, L.1978 Patil, P.1980 Wood, O.1976 Notice: This is a stable sort.

Sort by digit Intuitively: Sort from most significant to least significant Then you would need to keep track of the “break” points -- irritating overhead Instead: Start with the least significant digit Sorting technique must be stable… …like counting sort

Radix Sort

Assume base 10 numbers (for now) Each step: Sort n numbers Range from 0 to 9 O(n+10)

How many steps are required for a m digit number? 1.O(log m) 2.O(m) 3.O(m log m) 4.O(m 2 ) 5.None of the above

Assume base 10 numbers (for now) Each step: Sort n numbers Range from 0 to 9 O(n+10) Number of steps: One per digit: log 10 M (M = max number) O((n + 10)log 10 M)=O(n log M)

Sorting binary numbers: O((n + 2)log 2 n) = O(n log M) Sorting hex numbers: O((n + 16)log 16 M) = O(n log M) Sorting English words O((n + 26) log 26 M) = O(n log M)

Sorting binary numbers: O((n + 2)log 2 n) = O(n log M) Sorting hex numbers: O((n + 16)log 16 M) = O(n log M) Sorting English words O((n + 26) log 26 M) = O(n log M) Sorting base b numbers O((n + b) log b M) = O(n log M + b log M)

Use: Numbers evenly distributed over the interval (0,1) Sort by tossing in buckets: Step 1: Create n buckets Step 2: Toss items into buckets Step 3: Sort each bucket with quicksort (or other)

Example [0.31, 0.70, 0.96, 0.23, 0.04, 0.56, 0.92, 0.89, 0.53, 0.32] [ ) [ ) [ ) [ ) [ ) [ ) [ ) [ ) [ ) [ )

Assumption: Numbers uniformly distributed There will not be too many numbers in any one bucket Sorting each bucket will be quick O(n) average runtime

Given a set of points on the plane, which points define the borders of the convex hull? Suppose each point was a pin sticking into the paper Suppose we surrounded them with a rubber band and let it contract Which points would it touch?

Sample Convex Hull Convex: We cannot draw a line that intersects the hull at more than two points.

Input: An (unordered) set of n points on the plane (given as coordinates) Output: A list of the points on the convex hull in counter-clockwise order Algorithms: Jarvis March: O(n 2 ) Graham’s Scan: O(n log n)

We now know the problem is O(n log n) Can we show that is it  (n log n)? –What other lower bounds do we know? –Can we shows that an o(n log n) CH algorithm would lead to an o(n log n) sorting algorithm?

How do we sort with a Convex Hull algorithm? –(Sorting) Input: A sequence of numbers i 1,i 2,…,i n –Translation: Create a CH input: (i 1,i 1 2 ), (i 2,i 2 2 ), …, (i n,i n 2 )

List: 4, 1, 9, 3, 2, 6, 8, 7 CH Input: (4,16), (1,1), (9,81), (3,9), (2,4), (6,36), (8,64), (7,49) Return (1,1) (2,4) (3,9) (4,16) (6,36) (7,49) (8,64) (9,81)

If CH takes f(n) time: –Translates sorting input to CH input O(n) –Run CH algorithm O(f(n)) –Process CH results O(n) –Runtime: O(n + f(n)) If CH = o(n log n) then we can sort in o(n log n) –We can’t sort in o(n log n) –Hence: CH is  (n log n)