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.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Analysis of Algorithms
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
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
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.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Lower bound for sorting, radix sort COMP171 Fall 2006.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
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)
Lecture 5: Master Theorem and Linear Time Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
September 24, 2001 L5.1 Introduction to Algorithms Lecture 6 Prof. Shafi Goldwasser.
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
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Introduction to Algorithms Jiafen Liu Sept
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
September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.1 Introduction to Algorithms 6.046J/18.401J Prof. Erik Demaine LECTURE5.
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.
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
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.
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 3 Sorting and Selection. Comparison Sort.
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
Decision trees Polynomial-Time
Introduction to Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
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.
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-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
David Kauchak cs302 Spring 2012
CS 583 Analysis of Algorithms
Lecture 5 Algorithm Analysis
Presentation transcript:

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 same elements, but in the non-decreasing order

How fast can we sort? Can we do better than O(n log n)? Well… it depends? It depends on the model of computation!

Comparison Sorting Only comparisons are used to determine the order of elements Example –Insertion sort –Merge Sort –Quick Sort –Heapsort

Decision Trees Example: 3 element sorting (a 1, a 2, a 3 ) a 1 ? a 2 “Less than” leads to left branch “Great than” leads to right branch

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1, a 2, a 3

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1 ? a 3 a 1, a 2, a 3

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1 ? a 3 a 1, a 2, a 3 a 3, a 1, a 2 a 1, a 3, a 2

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1 ? a 3 a 1, a 2, a 3 a 3, a 1, a 2 a 1, a 3, a 2

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1 ? a 3 a 1, a 2, a 3 a 3, a 1, a 2 a 1, a 3, a 2 a 2, a 1, a 3

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1 ? a 3 a 2 ? a 3 a 1 ? a 3 a 1, a 2, a 3 a 3, a 1, a 2 a 1, a 3, a 2 a 2, a 1, a 3 a 2, a 3, a 1 a 3, a 2, a 1

Decision Trees Example: 3 element sort (a 1, a 2, a 3 ) a 2 ? a 3 a 1 ? a 2 a 1 ? a 3 a 2 ? a 3 a 1 ? a 3 a 1, a 2, a 3 a 3, a 1, a 2 a 1, a 3, a 2 a 2, a 1, a 3 a 2, a 3, a 1 a 3, a 2, a 1

Decision Trees Each leaf contains a permutation indicating that permutation of order has been established A decision tree can model a comparison sort It is the tree of all possible instruction traces Running time = length of path Worst case running time = length of the longest path (height of the tree)

Lower Bound for Comparison Sorting Theorem: Any decision tree that sorts n elements has height  (n lg n) Proof: There are n! leaves A binary tree of height h has no more than 2 h nodes

Optimal Comparison Sorting Algorithms Merge sort Quick sort Heapsort

Sorting in Linear Time Counting sort –Suppose we only have k keys {1,2,…,k} –  ( n+k ) time Radix sort –Can use counting sort –Place by place (digit by digit ) sorting –IBM Human machine interface –If elements have d places (digits), then we need d calls to counting sort

Counting Sort Counts how many occurrences of each key Allocate proper amount of spaces for each key, from small to large For each element a[j] in the input, count the number of elements that are less than the element A[k] is “Less than” A[j] if –A[k] < A[j] or –A[k] = A[j] and k < j

Example of Counting Sort A = [ ], we have four keys C =[ ] Prefix sum of C –Prefix_sum( C ) = 0 [ ] –First key goes from 1 to 1 –Second key goes from 2 to 1 (empty) –Third key goes from 2 to 3 –Fourth key goes from 4 5 Sorted array is = [ ]

Counting Sort Counting_sort (A, 1, n) –for i = 0 to k do –for j = 1 to length(A) do –for i = 1 to k do –for j = length(A) downto 1 do Zero out the counting array C[j] now contains the number of elements equal to i C[j] now contains the number of elements less than or equal to j

Stable sorting Stable: after sorting elements with the same values appear in the output array in the same order as they do in the input array An important property of counting sort –It is stable

Complexity of Counting Sort  (n+k)

Radix Sort

Radix Sort

Radix Sort

Radix Sort

Correctness Induction on digit position –Assume numbers are sorted by low-order t-1 digits –Sort on digit t Two numbers that differ in digit t are correctly sorted 2 number = in digit t are put in same order as input, implying correct order Spreadsheets use stable sort, so you can try by hand if you have spreadsheets

Time Complexity of Radix sort Sort n words with b bits each –b passes of counting sort on 1-bits digits –Or b/2 passes … 2-bits digits –Or b/r passes … r-bits digits r = log n Keys from {1,…,n d } needs  (dn) time