CSE 373 Data Structures Lecture 15

Slides:



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

Analysis of Algorithms
QuickSort Average Case Analysis An Incompressibility Approach Brendan Lucier August 2, 2005.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
MS 101: Algorithms Instructor Neelima Gupta
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CSCE 3110 Data Structures & Algorithm Analysis
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Tyler Robison Summer
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.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
Lecture 5: Master Theorem and Linear Time Sorting
Divide and Conquer 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
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…
1 CSE 326: Data Structures: Sorting Lecture 16: Friday, Feb 14, 2003.
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
Sorting in Linear Time Lower bound for comparison-based sorting
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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.
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.
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
1 CSE 326: Data Structures Sorting It All Out Henry Kautz Winter Quarter 2002.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
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,
Internal and External Sorting External Searching
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.
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.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
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
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
CPSC 411 Design and Analysis of Algorithms
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
Lecture 5 Algorithm Analysis
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.
CS200: 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.
Lower Bounds & Sorting in Linear Time
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
Lower bound for sorting, radix sort
Lecture 5 Algorithm Analysis
Presentation transcript:

CSE 373 Data Structures Lecture 15 Sorting (Part III) CSE 373 Data Structures Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Reading Reading Sections 7.8-7.9 and radix sort in Section 3.2.6 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 How fast can we sort? Heapsort, Mergesort, and Quicksort all run in O(N log N) best case running time Can we do any better? No, if sorting is comparison-based. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Sorting Model Recall the basic assumption: we can only compare two elements at a time we can only reduce the possible solution space by half each time we make a comparison Suppose you are given N elements Assume no duplicates How many possible orderings can you get? Example: a, b, c (N = 3) 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Permutations How many possible orderings can you get? Example: a, b, c (N = 3) (a b c), (a c b), (b a c), (b c a), (c a b), (c b a) 6 orderings = 3•2•1 = 3! (i.e., “3 factorial”) All the possible permutations of a set of 3 elements For N elements N choices for the first position, (N-1) choices for the second position, …, (2) choices, 1 choice N(N-1)(N-2)(2)(1)= N! possible orderings 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Decision Tree a < b < c, b < c < a, c < a < b, a < c < b, b < a < c, c < b < a a < b a > b a < b < c c < a < b a < c < b b < c < a b < a < c c < b < a a < c a > c b < c b > c a < b < c a < c < b c < a < b b < c < a b < a < c c < b < a c < a c > a b < c b > c a < c < b b < c < a b < a < c a < b < c The leaves contain all the possible orderings of a, b, c 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Decision Trees A Decision Tree is a Binary Tree such that: Each node = a set of orderings i.e., the remaining solution space Each edge = 1 comparison Each leaf = 1 unique ordering How many leaves for N distinct elements? N!, i.e., a leaf for each possible ordering Only 1 leaf has the ordering that is the desired correctly sorted arrangement 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Decision Trees and Sorting Every comparison-based sorting algorithm corresponds to a decision tree Finds correct leaf by choosing edges to follow i.e., by making comparisons Each decision reduces the possible solution space by one half Run time is  maximum no. of comparisons maximum number of comparisons is the length of the longest path in the decision tree, i.e. the height of the tree 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Decision Tree Example a < b < c, b < c < a, c < a < b, a < c < b, b < a < c, c < b < a 3! possible orders a < b a > b a < b < c c < a < b a < c < b b < c < a b < a < c c < b < a a < c a > c b < c b > c a < b < c a < c < b c < a < b b < c < a b < a < c c < b < a c < a c > a b < c b > c actual order a < c < b b < c < a b < a < c a < b < c 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

How many leaves on a tree? Suppose you have a binary tree of height d . How many leaves can the tree have? d = 1  at most 2 leaves, d = 2  at most 4 leaves, etc. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Lower bound on Height A binary tree of height d has at most 2d leaves depth d = 1  2 leaves, d = 2  4 leaves, etc. Can prove by induction Number of leaves, L < 2d Height d > log2 L The decision tree has N! leaves So the decision tree has height d  log2(N!) 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 log(N!) is (NlogN) select just the first N/2 terms each of the selected terms is  logN/2 Sterling’s formula 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 (N log N) Run time of any comparison-based sorting algorithm is (N log N) Can we do better if we don’t use comparisons? 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Radix Sort: Sorting integers Historically goes back to the 1890 census. Radix sort = multi-pass bucket sort of integers in the range 0 to BP-1 Bucket-sort from least significant to most significant “digit” (base B) Requires P(B+N) operations where P is the number of passes (the number of base B digits in the largest possible input number). If P and B are constants then O(N) time to sort! 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Radix Sort Example After 1st pass Input data Bucket sort by 1’s digit 721 3 123 537 67 478 38 9 478 537 9 1 2 3 4 5 6 7 8 9 721 721 3 123 537 67 478 38 9 3 38 123 67 This example uses B=10 and base 10 digits for simplicity of demonstration. Larger bucket counts should be used in an actual implementation. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Radix Sort Example Bucket sort by 10’s digit After 1st pass After 2nd pass 3 9 721 123 537 38 67 478 721 3 123 537 67 478 38 9 1 2 3 4 5 6 7 8 9 03 09 721 123 537 38 67 478 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Radix Sort Example Bucket sort by 100’s digit After 2nd pass After 3rd pass 3 9 721 123 537 38 67 478 3 9 38 67 123 478 537 721 1 2 3 4 5 6 7 8 9 003 009 038 067 123 478 537 721 Invariant: after k passes the low order k digits are sorted. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Implementation Options List List of data, bucket array of lists. Concatenate lists for each pass. Array / List Array of data, bucket array of lists. Array / Array Array of data, array for all buckets. Requires counting. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Array / Array Data Array Count Array Address Array Target Array 1 2 3 4 5 6 7 478 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 721 1 537 1 3 3 9 1 123 721 2 1 537 7 3 3 67 38 3 478 8 123 3 38 9 67 2 3 9 2 5 Bucket i ranges from add[i] to add[i+1]-1 1 7 add[0] := 0 add[i] := add[i-1] + count[i-1], i > 0 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Array / Array Pass 1 (over A) Calculate counts and addresses for 1st “digit” Pass 2 (over T) Move data from A to T Calculate counts and addresses for 2nd “digit” Pass 3 (over A) Move data from T to A Calculate counts and addresses for 3nd “digit” … In the end an additional copy may be needed. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Choosing Parameters for Radix Sort N number of integers – given m bit numbers - given B number of buckets B = 2r : power of 2 so that calculations can be done by shifting. N/B not too small, otherwise too many empty buckets. P = m/r should be small. Example – 1 million 64 bit numbers. Choose B = 216 =65,536. 1 Million / B  15 numbers per bucket. P = 64/16 = 4 passes. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Properties of Radix Sort Not in-place needs lots of auxiliary storage. Stable equal keys always end up in same bucket in the same order. Fast B = 2r buckets on m bit numbers 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Internal versus External Sorting So far assumed that accessing A[i] is fast – Array A is stored in internal memory (RAM) Algorithms so far are good for internal sorting What if A is so large that it doesn’t fit in internal memory? Data on disk or tape Delay in accessing A[i] – e.g. need to spin disk and move head 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Internal versus External Sorting Need sorting algorithms that minimize disk access time External sorting – Basic Idea: Load chunk of data into RAM, sort, store this “run” on disk/tape Use the Merge routine from Mergesort to merge runs Repeat until you have only one run (one sorted chunk) Text gives some examples 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15

Sorting Lower Bound, Radix Sort - Lecture 15 Summary of Sorting Sorting choices: O(N2) – Bubblesort, Insertion Sort O(N log N) average case running time: Heapsort: In-place, not stable. Mergesort: O(N) extra space, stable. Quicksort: claimed fastest in practice but, O(N2) worst case. Needs extra storage for recursion. Not stable. O(N) – Radix Sort: fast and stable. Not comparison based. Not in-place. 2/24/03 Sorting Lower Bound, Radix Sort - Lecture 15