CSE 326: Data Structures Sorting Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
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
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
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
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.
© 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.
E.G.M. Petrakissorting1 Sorting  Put data in order based on primary key  Many methods  Internal sorting:  data in arrays in main memory  External.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
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
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.
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.
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 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.
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.
Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?
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
Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office hours:
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
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.
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.
Sorting.
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
Lecture 5 Algorithm Analysis
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.
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
CSE 326: Data Structures Sorting
Lecture 5 Algorithm Analysis
Lower bound for sorting, radix sort
The Selection Problem.
Lecture 5 Algorithm Analysis
Presentation transcript:

CSE 326: Data Structures Sorting Ben Lerner Summer 2007

2 Features of Sorting Algorithms In-place –Sorted items occupy the same space as the original items. (No copying required, only O(1) extra space if any.) Stable –Items in input with the same value end up in the same order as when they began.

3 Sort Properties Are the following:stable?in-place? Insertion Sort?NoYesCan Be NoYes Selection Sort?NoYesCan Be NoYes MergeSort?NoYesCan Be NoYes QuickSort?NoYesCan Be NoYes Your Turn

4 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 the basic action is a comparison.

5 Sorting Model Recall our 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)

6 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! (ie, “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

7 Decision Tree a < b < c, b < c < a, c < a < b, a < c < b, b < a < c, c < b < a a < b < c c < a < b a < c < b b < c < a b < a < c c < b < a a < b < c a < c < b c < a < b a < b < c a < c < b b < c < a b < a < c c < b < a b < c < a b < a < c a < b a > b a > ca < c b < c b > c b < c b > c c < a c > a The leaves contain all the possible orderings of a, b, c

8 Decision Trees A Decision Tree is a Binary Tree such that: –Each node = a set of orderings ie, the remaining solution space –Each edge = 1 comparison –Each leaf = 1 unique ordering –How many leaves for N distinct elements? N!, ie, a leaf for each possible ordering Only 1 leaf has the ordering that is the desired correctly sorted arrangement

9 Decision Tree Example a < b < c, b < c < a, c < a < b, a < c < b, b < a < c, c < b < a a < b < c c < a < b a < c < b b < c < a b < a < c c < b < a a < b < c a < c < b c < a < b a < b < c a < c < b b < c < a b < a < c c < b < a b < c < a b < a < c a < b a > b a > ca < c b < c b > c b < c b > c c < a c > a possible orders actual order

10 Decision Trees and Sorting Every sorting algorithm corresponds to a decision tree –Finds correct leaf by choosing edges to follow ie, 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

11 Lower bound on Height A binary tree of height h has at most how many leaves? The decision tree has how many leaves: A binary tree with L leaves has height at least: So the decision tree has height: Your Turn

12 log(N!) is  (NlogN) select just the first N/2 terms each of the selected terms is  logN/2

13  (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?

14 BucketSort (aka BinSort) If all values to be sorted are known to be between 1 and K, create an array count of size K, increment counts while traversing the input, and finally output the result. Example K=5. Input = (5,1,3,4,3,2,1,1,5,4,5) count array Running time to sort n items?

15 BucketSort Complexity: O(n+K) Case 1: K is a constant –BinSort is linear time Case 2: K is variable –Not simply linear time Case 3: K is constant but large (e.g ) –???

16 Fixing impracticality: RadixSort Radix = “The base of a number system” –We’ll use 10 for convenience, but could be anything Idea: BucketSort on each digit, least significant to most significant (lsd to msd)

Bucket sort by 1’s digit Input data This example uses B=10 and base 10 digits for simplicity of demonstration. Larger bucket counts should be used in an actual implementation. Radix Sort Example (1 st pass) After 1 st pass

18 Bucket sort by 10’s digit Radix Sort Example (2 nd pass) After 1 st passAfter 2 nd pass

19 Bucket sort by 100’s digit Radix Sort Example (3 rd pass) After 2 nd pass After 3 rd pass Invariant: after k passes the low order k digits are sorted.

20 RadixSort Input:126, 328, 636, 341, 416, 131, BucketSort on lsd: BucketSort on next-higher digit: BucketSort on msd: Your Turn

21 Radixsort: Complexity How many passes? How much work per pass? Total time? Conclusion? In practice –RadixSort only good for large number of elements with relatively small values –Hard on the cache compared to MergeSort/QuickSort

22 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

23 Internal versus External Sorting Need sorting algorithms that minimize disk/tape 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

24 Summary of sorting Sorting choices: –O(N 2 ) – 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(N 2 ) worst case. Needs extra storage for recursion. Not stable. –O(N) – Radix Sort: fast and stable. Not comparison based. Not in-place.