CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.

Slides:



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

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:
CSE 3101: Introduction to the Design and Analysis of Algorithms
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
Lower bound for sorting, radix sort COMP171 Fall 2005.
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.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 5: Master Theorem and Linear Time Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Analysis of Algorithms CS 477/677
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
1 Sorting in O(N) time CS302 Data Structures Section 10.4.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Introduction to Algorithms Jiafen Liu Sept
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
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.
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.
CSCE 411H Design and Analysis of Algorithms Set 10: Lower Bounds Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 10 1 * Slides adapted.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
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.
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.
1 Chapter 8-1: Lower Bound of Comparison Sorts. 2 About this lecture Lower bound of any comparison sorting algorithm – applies to insertion sort, selection.
Lower Bounds & Sorting in Linear Time
David Kauchak cs062 Spring 2010
Sorting Lower Bound 4/25/2018 8:49 PM
CPSC 411 Design and Analysis of Algorithms
Introduction to Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
Linear Sorting Sections 10.4
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
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.
CS200: Algorithm Analysis
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.
Lower Bounds & Sorting in Linear Time
Linear Sorting Section 10.4
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Topic 5: Heap data structure heap sort Priority queue
Lower bound for sorting, radix sort
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
David Kauchak cs302 Spring 2012
CS 583 Analysis of Algorithms
Lecture 5 Algorithm Analysis
Presentation transcript:

CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis

2 How Fast Can We Sort?

Insertion sort: O(n 2 ) Bubble Sort, Selection Sort:  (n 2 ) Merge sort:  (nlgn) Quicksort:  (nlgn) - average What is common to all these algorithms? ◦ They all sort by making comparisons between the input elements

Comparison Sorts Comparison sorts use comparisons between elements to gain information about an input sequence  a 1, a 2, …, a n  Perform tests: a i a j to determine the relative order of a i and a j For simplicity, assume that all the elements are distinct

5 Lower-Bound for Sorting Theorem: To sort n elements, comparison sorts must make  (nlgn) comparisons in the worst case.

Decision Tree Model Represents the comparisons made by a sorting algorithm on an input of a given size. ◦ Models all possible execution traces ◦ Control, data movement, other operations are ignored ◦ Count only the comparisons node leaf:

Worst-case number of comparisons? Worst-case number of comparisons depends on: ◦ the length of the longest path from the root to a leaf (i.e., the height of the decision tree)

Lemma Any binary tree of height h has at most 2 h leaves Proof: by induction on h Basis: h = 0  tree has one node, which is a leaf # of Leaves = 1 ≤ 2 0 (TRUE) Inductive step: assume true for h-1 (i.e. #Leaves ≤ 2 h-1 ) ◦ Extend the height of the tree with one more level ◦ Each leaf becomes parent to two new leaves No. of leaves at level h = 2  (no. of leaves at level h-1 ) ≤ 2  2 h-1 ≤ 2 h h-1 h

What is the least number of leaves in a Decision Tree Model? All permutations on n elements must appear as one of the leaves in the decision tree: n! permutations At least n! leaves

Theorem: Any comparison sort algorithm requires  (nlgn) comparisons in the worst case. Proof: How many leaves does the tree have? At least n! (each of the n! permutations must appear as a leaf) There are at most 2 h leaves (by the previous Lemma)  n! ≤ 2 h  h ≥ lg(n!) =  (nlgn) (see next slide) Lower Bound for Comparison Sorts Exercise 8.1-1: What is the smallest possible depth of a leaf in a decision tree for a comparison sort? h leaves

1. n! ≤ n n  lg(n!) ≤ nlgn  lg(n!) = O(nlgn) 2. n! ≥ 2 n  lg(n!) ≥ nlg2=n  lg(n!) = Ω(n)  n ≤ lg(n!) ≤ nlgn We need a tighter lower bound! Use Stirling’s approximation (3.18): lg(n!) =  (nlgn)

Counting Sort Assumptions: ◦ Sort n integers which are in the range [0... r] ◦ r is in the order of n, that is, r=O(n) Idea: ◦ For each element x, find the number of elements ≤ x ◦ Place x into its correct position in the output array output array ( )

Step 1 (i.e., frequencies/histogram) Allocate C[1..r] (histogram) For 1 ≤ i ≤ n, ++C[A[i]} Find the number of times A[i] appears in A

Step 2 Find the number of elements ≤ A[i] (i.e. cumulative sums)

15 Algorithm Start from the last element of A Place A[i] at its correct place in the output array Decrease C[A[i]] by one

Example A C C B C B C B C B C 8 0 (frequencies) (cumulative sums)

17 Example (cont.) A B C B C B C B

Alg.: COUNTING-SORT(A, B, n, r) 1.for i ← 0 to r 2. do C[ i ] ← 0 3.for j ← 1 to n 4. do C[A[ j ]] ← C[A[ j ]] + 1 % C[i] contains the number of elements = i ; frequencies 5.for i ← 1 to r 6. do C[ i ] ← C[ i ] + C[i -1] % C[i] contains the number of elements ≤ i ; cumulative sum 7.for j ← n downto 1 8. do B[C[A[ j ]]] ← A[ j ] % B[.] contains sorted array 9. C[A[ j ]] ← C[A[ j ]] – n 0r A C 1n B j

Alg.: COUNTING-SORT(A, B, n, k) 1.for i ← 0 to r 2. do C[ i ] ← 0 3.for j ← 1 to n 4. do C[A[ j ]] ← C[A[ j ]] for i ← 1 to r 6. do C[ i ] ← C[ i ] + C[i -1] 7.for j ← n downto 1 8. do B[C[A[ j ]]] ← A[ j ] 9. C[A[ j ]] ← C[A[ j ]] – 1 Analysis of Counting Sort  (r)  (n)  (r)  (n) Overall time:  (n + r)

Analysis of Counting Sort Overall time:  (n + r) In practice we use COUNTING sort when r = O(n)  running time is  (n) Counting sort is stable Counting sort is not in place sort

21 Radix Sort Represents keys as d-digit numbers in some base-k e.g. key = x 1 x 2...x d where 0 ≤ x i ≤ k-1 Example: key=15 key 10 = 15, d=2, k=10 where 0 ≤ x i ≤ 9 key 2 = 1111, d=4, k=2 where 0 ≤ x i ≤ 1

Assumptions: d=Θ(1) and k =O(n) Sorting looks at one column at a time ◦ For a d digit number, sort the least significant digit first ◦ Continue sorting on the next least significant digit, until all digits have been sorted ◦ Requires only d passes through the list Radix Sort

Alg.: RADIX-SORT(A, d) for i ← 1 to d do use a stable sort to sort array A on digit i 1 is the lowest order digit, d is the highest-order digit Radix Sort How do things go wrong if an unstable sorting alg. is used?

Analysis of Radix Sort Given n numbers of d digits each, where each digit may take up to k possible values, RADIX-SORT correctly sorts the numbers in  (d(n+k)) ◦ One pass of sorting per digit takes  (n+k) assuming that we use counting sort ◦ There are d passes (for each digit)   (d(n+k)) Since d=Θ(1) and k =O(n) Therefore, Radix Sort runs in  (n) time

Conclusions In the worst case, any comparison sort will take at least nlgn to sort an array of n numbers We can achieve a O(n) running time for sorting if we can make certain assumptions on the input data: ◦ Counting sort: each of the n input elements is an integer in the range [ 0... r] and r=O(n) ◦ Radix sort: the elements in the input are integers represented with d digits in base-k, where d= Θ (1) and k =O(n)

Problem You are given 5 distinct numbers to sort. Describe an algorithm which sorts them using at most 6 comparisons, or argue that no such algorithm exists. Solution: Total # of leaves in the comparison tree = 5! If the height of the tree is h, then (total # of leaves ≤ 2 h )  2 h ≥ 5! h ≥ log 2 (5!) ≥ log h > 6  There is at least one input permutation which will require at least 7 comparisons to sort. Therefore, no such algorithm exists.