How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Similar documents
Algorithms Chapter 8 Sorting in Linear Time

Lower bound for comparison-based sorting

Design and Analysis of Algorithms

COT 6405 Introduction to Theory of Algorithms

Chapter 8 Sorting in Linear Time

II (Sorting and) Order Statistics

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Chapter 8 Sort in Linear Time

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Lecture: Analysis of Algorithms (CS )

Can we do faster? What is the theoretical best we can do?

Introduction to Algorithms

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Introduction to Algorithms 6.046J/18.401J

ECE608 - Chapter 8 answers

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

8 SortinginLinearTime

Lower Bound on Comparison-based Sorting

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

EECS 2011M: Fundamentals of Data Structures

Module 3: Sorting and Randomized Algorithms

Can we do faster? What is the theoretical best we can do?

Module 3: Sorting and Randomized Algorithms

Introduction to Algorithms

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CS Algorithms and Complexity

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Properties of a heap (represented by an array A)

Count Sort, Bucket Sort, Radix Sort. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

4. Sorting and Order-Statistics

CS240 Fall Mike Lam, Professor. Quick Sort

Algorithms and Data Structures for Mathematicians

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting and Selection

CS 303 Design and Analysis of Algorithms

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

COMP Data Structures

Sorting. CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

CPE702 Sorting Algorithms

Fast Sorting and Selection. A Lower Bound for Worst Case

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

SORTING AND SELECTION

Data Structures and Algorithms Chapter 4

Sample Solutions to Homework #2

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

We assume uniform hashing (UH):

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

Partha Sarathi Manal

Linear Sorts. EECS 2011 Prof. J. Elder - 1 -

Data Structures and Algorithms Week 4

Data Structures. Sorting. Haim Kaplan & Uri Zwick December 2013

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Other techniques for sorting exist, such as Linear Sorting which is not based on comparisons. Linear Sorting techniques include:

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

CS:3330 (22c:31) Algorithms

Lecture 3: Sorting 1

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Lecture 6 Sorting and Searching

The divide and conquer strategy has three basic parts. For a given problem of size n,

CSci 231 Final Review

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

l Find a partner to bounce ideas off of l Assignment 2 l Proofs should be very concrete l Proving big-o, must satisfy definition

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS61BL. Lecture 5: Graphs Sorting

Sorting is ordering a list of objects. Here are some sorting algorithms

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

Priority Queues. T. M. Murali. January 29, 2009

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Practical Session 10 - Huffman code, Sort properties, QuickSort algorithm, Selection

COMP 352 FALL Tutorial 10

CSE 326: Data Structures Sorting Conclusion

Theory and Algorithms Introduction: insertion sort, merge sort

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING ADVANCED DATA STRUCTURES AND ALGORITHMS EXAM EXAMINATION JUNE 2014

CS 5321: Advanced Algorithms Sorting. Acknowledgement. Ali Ebnenasir Department of Computer Science Michigan Technological University

Merge Sort

Lecture 5: Sorting in Linear Time CSCI Algorithms I. Andrew Rosenberg

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

END-TERM EXAMINATION

O(n): printing a list of n items to the screen, looking at each item once.

CS 561, Lecture 1. Jared Saia University of New Mexico

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Module 5: Hashing. CS Data Structures and Data Management. Reza Dorrigiv, Daniel Roche. School of Computer Science, University of Waterloo

Transcription:

Chapter 8. Sorting in Linear Time

Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based Insertion Sort -- comparison-based Heap Sort -- comparison-based Merge Sort -- comparison-based

Lower bounds for sorting Lower bounds Ω(n) to examine all the input. All sorts seen so far are Ω(n lg n). We ll show that Ω(n lg n) is a lower bound for comparison sorts. Decision tree Abstraction of any comparison sort. Represents comparisons made by a specific sorting algorithm on inputs of a given size. Abstracts away everything else: control and data movement. We re counting only comparisons.

Decision Tree How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once. For any comparison sort, 1 tree for each n. View the tree as if the algorithm splits in two at each node, based on the information it has determined up to that point. The tree models all possible execution traces. What is the length of the longest path from root to leaf? Depends on the algorithm Insertion sort: (n²) Merge sort: (n lg n) Worst case Running time

Lemma Lemma Any binary tree of height h has 2 h In other words: l = # of leaves, h = height, Then l 2 h. leaves. Theorem Any decision tree that sorts n elements has height Ω(n lg n).

Proof

Sorting in Linear Time Non-comparison sorts. Counting sort Depends on a key assumption: : numbers to be sorted are integers in {0, 1,,..., k}. Input: A[1.. n], where A[ j ] {0, 1,..., k} for j = 1, 2,,..., n. Array A and values n and k are given as parameters. Output: B[1.. n], sorted. B is assumed to be already allocated and is given as a parameter. Auxiliary storage: C[0.. k]

Analysis of Counting sort Θ(n + k), which is Θ(n) if k = O(n). How big a k is practical? Good for sorting 32-bit values? No. 16-bit? Probably not. 8-bit? Maybe, depending on n. 4-bit? Probably (unless n is really small). Counting sort will be used in radix sort. Stable algorithm: Numbers with the same value appear in the output array in the same order as they do in the input array;.

Radix Sort(1) Key idea: Sort least significant digits first. Why?

Radix Sort(2) Correctness: Induction on number of passes ( i in pseudocode). Assume digits 1, 2,..., i-1 are sorted. Show that a stable sort on digit i leaves digits 1,..., i sorted: If 2 digits in position i are different, ordering by position i is correct, and positions 1,..., i-1 are irrelevant. If 2 digits in position i are equal, numbers are already in the right order (by inductive hypothesis). The stable sort on digit i leaves them in the right order.

Analysis of Radix Sort(1) Lemma 8.3: Given n d-digit digit numbers in which each digit can take on up to k possible values, RADIX-SORT correctly sorts these numbers in Θ (d( n + k)) time. Assume that we use counting sort as the intermediate sort. Θ (n + k) per pass (digits in range 0,,..., k) d passes Θ (d(n + k)) total If k = O(n), time = Θ (d n).

Analysis of Radix Sort(2) Lemma 8.4 Given n b-bit numbers and any positive integer r b, RADIX- SORT correctly sorts these numbers in Θ (b/r (n+ 2 r )) time. How to break each key into digits? n words. b bits/word. Break into r-bit digits. Have d = b/r. Use counting sort, k = 2 r -1. Example: 32-bit words, 8-bit digits. b = 32, r = 8, d = 32/8 = 4, k = 2 8-1 = 255. Time = Θ (b/r (n + 2 r )).

Bucket Sort Assumes the input is generated by a random process that distributes elements uniformly over [0, 1). Idea: Divide [0, 1) into n equal-sized buckets. Distribute the n input values into the buckets. Sort each bucket. Then go through buckets in order, listing elements in each one. Input: A[1.. n], where 0 A[i] < 1 for all i. Auxiliary array: B[0.. n-1] of linked lists, each list initially empty.

Correctness Consider A[i ], A[ j ]: Assume without loss of generality that A[i ] A[ j Then n A[i ] n A[ j ]. So A[i ] is placed into the same bucket as A[ j ] or into a bucket with a lower index. If same bucket, insertion sort fixes up. If earlier bucket, concatenation of lists fixes up.

Analysis Relies on no bucket getting too many values. All lines of algorithm except insertion sorting take Θ(n) altogether. Intuitively, if each bucket gets a constant number of elements, it takes O(1) time to sort each bucket O(n) sort time for all buckets. We expect each bucket to have few elements, since the average is 1 element per bucket.

Detailed Analysis

Conclusion

Homework 8.2-3, 8.2-4, 8.3-2,8.3-4, 8.4-2, 8.4-3

Important Features of Sort Algorithms Sort Algorithm Worst-case T(n) Average-case T(n) Insertion sort Θ(n² ) Θ(n² ) Quick sort Θ (n²) Θ (n lg n) Merge sort Θ (n lg n) Θ (n lg n) Heap sort Θ (n lg n) Θ(n lg n) Counting sort Radix sort Bucket sort Θ (n) Θ (n lg n) Θ(n² ) Θ (n) Θ (n) Θ (n)