Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Similar documents
Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

CSE 326: Data Structures Quicksort Comparison Sorting Bound

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Sorting. Sorting. Why Sort? Consistent Ordering

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

Quicksort. Part 1: Understanding Quicksort

Searching & Sorting. Definitions of Search and Sort. Linear Search in C++ Linear Search. Week 11. index to the item, or -1 if not found.

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

More on Sorting: Quick Sort and Heap Sort

Design and Analysis of Algorithms

Sorting and Algorithm Analysis

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

CE 221 Data Structures and Algorithms

CS1100 Introduction to Programming

Esc101 Lecture 1 st April, 2008 Generating Permutation

Problem Set 3 Solutions

Sorting. Sorted Original. index. index

CS240: Programming in C. Lecture 12: Polymorphic Sorting

CS221: Algorithms and Data Structures. Priority Queues and Heaps. Alan J. Hu (Borrowing slides from Steve Wolfman)

CSE 326: Data Structures Sorting Conclusion

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

ELEC 377 Operating Systems. Week 6 Class 3

Brave New World Pseudocode Reference

Programming in Fortran 90 : 2017/2018

Priority queues and heaps Professors Clark F. Olson and Carol Zander

CHAPTER 10: ALGORITHM DESIGN TECHNIQUES

CSE 373: Data Structures and Algorithms

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Loop Transformations, Dependences, and Parallelization

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

Lecture 5: Multilayer Perceptrons

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

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

An Optimal Algorithm for Prufer Codes *

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Lecture 3: Computer Arithmetic: Multiplication and Division

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

Conditional Speculative Decimal Addition*

Machine Learning. Support Vector Machines. (contains material adapted from talks by Constantin F. Aliferis & Ioannis Tsamardinos, and Martin Law)

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

LOOP ANALYSIS. The second systematic technique to determine all currents and voltages in a circuit

Support Vector Machines

Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

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

Hierarchical clustering for gene expression data analysis

Private Information Retrieval (PIR)

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Mathematics 256 a course in differential equations for engineering students

Dynamic Programming. Example - multi-stage graph. sink. source. Data Structures &Algorithms II

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

Greedy Technique - Definition

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

CSE 373: Data Structures and Algorithms

CS246: Mining Massive Datasets Jure Leskovec, Stanford University

Parallel Numerics. 1 Preconditioning & Iterative Solvers (From 2016)

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

Machine Learning 9. week

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Sorting Algorithms. + Analysis of the Sorting Algorithms

The AVL Balance Condition. CSE 326: Data Structures. AVL Trees. The AVL Tree Data Structure. Is this an AVL Tree? Height of an AVL Tree

Machine Learning. Topic 6: Clustering

Cache Performance 3/28/17. Agenda. Cache Abstraction and Metrics. Direct-Mapped Cache: Placement and Access

LLVM passes and Intro to Loop Transformation Frameworks

A Binarization Algorithm specialized on Document Images and Photos

Cluster Analysis of Electrical Behavior

Smoothing Spline ANOVA for variable screening

Report on On-line Graph Coloring

A Modified Median Filter for the Removal of Impulse Noise Based on the Support Vector Machines

Intro. Iterators. 1. Access

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

Machine Learning: Algorithms and Applications

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Assembler. Building a Modern Computer From First Principles.

CMPS 10 Introduction to Computer Science Lecture Notes

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

Quick Sort. CSE Data Structures May 15, 2002

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

LU Decomposition Method Jamie Trahan, Autar Kaw, Kevin Martin University of South Florida United States of America

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CHAPTER 2 DECOMPOSITION OF GRAPHS

AMath 483/583 Lecture 21 May 13, Notes: Notes: Jacobi iteration. Notes: Jacobi with OpenMP coarse grain

Today Using Fourier-Motzkin elimination for code generation Using Fourier-Motzkin elimination for determining schedule constraints

Design and Analysis of Algorithms

We can use a max-heap to sort data.

Tsinghua University at TAC 2009: Summarizing Multi-documents by Information Distance

CS 534: Computer Vision Model Fitting

4. Sorting and Order-Statistics

Solving two-person zero-sum game by Matlab

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Performance Evaluation of Information Retrieval Systems

Programming II (CS300)

Transcription:

Today s Outlne Sortng Chapter 7 n Wess CSE 26 Data Structures Ruth Anderson Announcements Wrtten Homework #6 due Frday 2/26 at the begnnng of lecture Proect Code due Mon March 1 by 11pm Today s Topcs: Sortng 2/24/2010 1 2/24/2010 2 Why Sort? Sortng: The Bg Pcture Gven n comparable elements n an array, sort them n an ncreasng (or decreasng) order. Smple algorthms: O(n 2 ) Fancer algorthms: O(n log n) Comparson lower bound: Ω(n log n) Specalzed algorthms: O(n) Handlng huge data sets 2/24/2010 Inserton sort Selecton sort Bubble sort Shell sort Heap sort Merge sort Quck sort Bucket sort Radx sort External sortng 2/24/2010 4 Inserton Sort: Idea At the k th step, put the k th nput element n the correct place among the frst k elements Result: After the k th step, the frst k elements are sorted. Runtme: worst case : best case : average case : Selecton Sort: Idea Fnd the smallest element, put t 1 st Fnd the next smallest element, put t 2 nd Fnd the next smallest, put t rd And so on 2/24/2010 5 2/24/2010 6

Mystery(nt array a[]) { for (nt p = 1; p < length; p++) { nt tmp = a[p]; for (nt = p; > 0 && tmp < a[-1]; --) a[] = a[-1]; a[] = tmp; Student Actvty What sort s ths? What s ts runnng tme? Best? Avg? Worst? 2/24/2010 7 Selecton Sort: Code vod SelectonSort (Array a[0..n-1]) { for (=0, <n; ++) { = Fnd ndex of smallest entry n a[..n-1] Swap(a[],a[]) Runtme: worst case : best case : average case : 2/24/2010 8 Dvde and conquer A common and mportant technque n algorthms Dvde problem nto parts Solve parts Merge solutons Dvde and Conquer Sortng MergeSort: Dvde array nto two halves Recursvely sort left and rght halves Merge halves QuckSort: Partton array nto small tems and large tems Recursvely sort the two smaller portons 2/24/2010 12 2/24/2010 1 Merge Sort MergeSort (Array [1..n]) 1. Splt Array n half 2. Recursvely sort each half. Merge two halves together Merge Sort: Complexty Merge (a1[1..n],a2[1..n]) 1=1, 2=1 Whle (1<n, 2<n) { f (a1[1] < a2[2]) { Next s a1[1] 1++ else { Next s a2[2] 2++ 2/24/2010 Now throw n the dregs 15 The 2-ponter method 2/24/2010 17

Auxlary array The mergng requres an auxlary array Qucksort Uses dvde and conquer Doesn t requre O(N) extra space lke MergeSort 2 4 8 1 5 6 Partton nto left and rght Left less than pvot Rght greater than pvot Recursvely sort left and rght Concatenate left and rght 2/24/2010 18 2/24/2010 22 Quck Sort The steps of QuckSort < < < 28 < < < 15 47 1. Pck a pvot 2. Dvde nto less-than & greater-than pvot. Sort each sde recursvely 2/24/2010 2 S 1 1 0 26 81 2 4 65 4 1 65 57 1 57 2/24/2010 24 26 2 75 75 0 81 select pvot value S 1 S 2 partton S S 1 S 2 0 1 26 1 4 57 65 75 81 2 S 26 1 4 1 57 75 0 65 81 2 [Wess] QuckSort(S 1 ) and QuckSort(S 2 ) Presto! S s sorted Ideas? Selectng the pvot QuckSort Example 0 1 2 4 5 6 7 8 8 1 4 0 5 2 7 6 0 1 4 7 5 2 6 8 Choose the pvot as the medan of three. 2/24/2010 25 Place the pvot and the largest at the rght and the smallest at the left 2/24/2010 27

QuckSort Example 0 1 4 7 5 2 6 8 0 1 4 7 5 2 6 8 0 1 4 7 5 2 6 8 0 1 4 2 7 5 6 8 QuckSort Example 0 1 4 2 7 5 6 8 0 1 4 2 7 5 6 8 0 1 4 2 5 7 6 8 0 1 4 2 5 7 6 8 0 1 4 2 5 7 6 8 Move to the rght to be larger than pvot. Move to the left to be smaller than pvot. Swap 2/24/2010 28 0 1 4 2 5 6 7 8 2/24/2010 S 1 < pvot pvot S 2 > pvot 2 Student Actvty Recursve Qucksort Qucksort(A[]: nteger array, left,rght : nteger): { pvotndex : nteger; f left + CUTOFF rght then pvot := medan(a,left,rght); pvotndex := Partton(A,left,rght-1,pvot); Qucksort(A, left, pvotndex 1); Qucksort(A, pvotndex + 1, rght); else Insertonsort(A,left,rght); Recurrence Relatons Wrte the recurrence relaton for QuckSort: Best Case: Worst Case: Don t use qucksort for small arrays. CUTOFF = 10 s reasonable. 2/24/2010 0 2/24/2010 2 QuckSort: Best case complexty QuckSort: Worst case complexty 2/24/2010 2/24/2010 4

QuckSort: Average case complexty Turns out to be O(n log n) See Secton 7.7.5 for an dea of the proof. Don t need to know proof detals for ths course. Qucksort Complexty Worst case: O(n 2 ) Best case: O(n log n) Average Case: O(n log n) 2/24/2010 5 2/24/2010 6 Mergesort and massve data MergeSort s the bass of massve sortng Qucksort and Heapsort both ump all over the array, leadng to expensve random dsk accesses Mergesort scans lnearly through arrays, leadng to (relatvely) effcent sequental dsk access In-memory sortng of reasonable blocks can be combned wth larger mergesorts Mergesort can leverage multple dsks 2/24/2010 7 Features of Sortng Algorthms In-place Sorted tems occupy the same space as the orgnal tems. (No copyng requred, only O(1) extra space f any.) Stable Items n nput wth the same value end up n the same order as when they began. 2/24/2010 8 How fast can we sort? Heapsort, Mergesort, and Qucksort all run n O(N log N) best case runnng tme Can we do any better? No, f the basc acton s a comparson. Sortng Model Recall our basc assumpton: we can only compare two elements at a tme we can only reduce the possble soluton space by half each tme we make a comparson Suppose you are gven N elements Assume no duplcates How many possble orderngs can you get? Example: a, b, c (N = ) 2/24/2010 40 2/24/2010 41

Permutatons How many possble orderngs can you get? Example: a, b, c (N = ) (a b c), (a c b), (b a c), (b c a), (c a b), (c b a) 6 orderngs = 2 1 =! (e, factoral ) All the possble permutatons of a set of elements For N elements N choces for the frst poston, (N-1) choces for the second poston,, (2) choces, 1 choce N(N-1)(N-2)L(2)(1)= N! possble orderngs 2/24/2010 42 b < c a < b < c a < c a < b < c a < c < b a < b < c c < a < b a < c < b b > c a < c < b a > c Decson Tree c < a < b a < b a < b < c, b < c < a, c < a < b, a < c < b, b < a < c, c < b < a a > b b < c < a b < a < c c < b < a b < c < a c < b < a b < a < c c < a c > a b < c < a b < c b > c b < a < c The leaves contan all the possble orderngs of a, b, c 2/24/2010 4 Student Actvty Lower bound on Heght A bnary tree of heght h has at most how many leaves? L A bnary tree wth L leaves has heght at least: h The decson tree has how many leaves: So the decson tree has heght: h select ust the frst N/2 terms each of the selected terms s logn/2 log(n!) s Ω(NlogN) log( N!) = log ( N ( N 1) ( N 2) L(2) (1) ) = log N + log( N 1) + log( N 2) + L+ log 2 + log1 N log N + log( N 1) + log( N 2) + L+ log 2 N N log 2 2 N N N (log N log 2) = log N 2 2 2 = Ω( N log N) 2/24/2010 45 Ω(N log N) Run tme of any comparson-based sortng algorthm s Ω(N log N) Can we do better f we don t use comparsons? 2/24/2010 46 BucketSort (aka BnSort) If all values to be sorted are known to be between 1 and K, create an array count of sze K, ncrement counts whle traversng the nput, and fnally output the result. Example K=5. Input = (5,1,,4,,2,1,1,5,4,5) count array 1 2 4 5 Runnng tme to sort n tems? 2/24/2010 47

BucketSort Complexty: O(n+K) Case 1: K s a constant BnSort s lnear tme Case 2: K s varable Not smply lnear tme Case : K s constant but large (e.g. 2 2 )??? Fxng mpractcalty: RadxSort Radx = The base of a number system We ll use 10 for convenence, but could be anythng Idea: BucketSort on each dgt, least sgnfcant to most sgnfcant (lsd to msd) 2/24/2010 48 2/24/2010 4 Radx Sort Example (1 st pass) Radx Sort Example (2 nd pass) Input data 8 0 1 2 Bucket sort by 1 s dgt 4 5 6 7 8 8 After 1 st pass 8 After 1 st pass 8 0 0 0 1 2 Bucket sort by 10 s dgt 8 4 5 6 7 8 After 2 nd pass 8 Ths example uses B=10 and base 10 dgts for smplcty of demonstraton. Larger bucket counts should be used 2/24/2010 n an actual mplementaton. 50 2/24/2010 51 Radx Sort Example ( rd pass) Student Actvty BucketSort on lsd: RadxSort Input:126, 28, 66, 41, 416, 11, 28 After 2 nd pass 8 0 00 00 08 0 1 Bucket sort by 100 s dgt 2 4 5 6 7 8 After rd pass 8 0 1 2 4 5 6 7 8 BucketSort on next-hgher dgt: 0 1 2 4 5 6 7 8 Invarant: after k passes the low order k dgts are sorted. BucketSort on msd: 2/24/2010 52 0 1 2 4 5 6 7 8 2/24/2010 5

Radxsort: Complexty How many passes? How much work per pass? Total tme? Concluson? In practce RadxSort only good for large number of elements wth relatvely small values 2/24/2010 54 Hard on the cache compared to MergeSort/QuckSort Internal versus External Sortng Need sortng algorthms that mnmze dsk/tape access tme External sortng Basc Idea: Load chunk of data nto RAM, sort, store ths run on dsk/tape Use the Merge routne from Mergesort to merge runs Repeat untl you have only one run (one sorted chunk) Text gves some examples 2/24/2010 55