Divide & conquer. Which works better for multi-cores: insertion sort or merge sort? Why?

Similar documents
CS 758/858: Algorithms

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

CS3334 Data Structures Lecture 4: Bubble Sort & Insertion Sort. Chee Wei Tan

Introduction to. Algorithms. Lecture 10. Prof. Piotr Indyk

Chapter 7: Sorting 7.1. Original

A Lower Bound for Comparison Sort

CSc 110, Spring Lecture 40: Sorting Adapted from slides by Marty Stepp and Stuart Reges

Algorithms and Data Structures CS 372. The Sorting Problem. Insertion Sort - Summary. Merge Sort. Input: Output:

Merge Sort. Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

Programming Abstractions

Animation Demos. Shows time complexities on best, worst and average case.

Previous Lecture. How can computation sort data faster for you? Sorting Algorithms: Speed Comparison. Recursive Algorithms 10/31/11

Animation Demos. Shows time complexities on best, worst and average case.

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 6 Lecture - 37 Divide and Conquer: Counting Inversions

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Nicki Dell Spring 2014

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

Hypercube Networks-III

The Theory Behind the z/architecture Sort Assist Instructions

GENERALIZATION: RANK ORDER FILTERS

ECE 242 Data Structures and Algorithms. Simple Sorting II. Lecture 5. Prof.

CSL 356: Analysis and Design of Algorithms. Ragesh Jaiswal CSE, IIT Delhi

DATA STRUCTURES USING C

Collectives Pattern. Parallel Computing CIS 410/510 Department of Computer and Information Science. Lecture 8 Collective Pattern

Sorting. APS105: Computer Fundamentals. Jason Anderson

CSE 1400 Applied Discrete Mathematics Permutations

Sequence Alignment & Computational Thinking

MITOCW R7. Comparison Sort, Counting and Radix Sort

Lecture 20: Combinatorial Search (1997) Steven Skiena. skiena

Randomized Algorithms

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

Lecture5: Lossless Compression Techniques

CSE465, Spring 2009 March 16 1

Permutations. Example 1. Lecture Notes #2 June 28, Will Monroe CS 109 Combinatorics

CSE 373 DECEMBER 4 TH ALGORITHM DESIGN

MA/CSSE 473 Day 14. Permutations wrap-up. Subset generation. (Horner s method) Permutations wrap up Generating subsets of a set

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

Lecture 12: Divide and Conquer Algorithms. Divide and Conquer Algorithms

Below is a implementation in Python of the imprtant algorithms of Chapter 6 of Discrete Mathematics by G. Chartrand and P. Zhang.

Fast Sorting and Pattern-Avoiding Permutations

MITOCW watch?v=cnb2ladk3_s

Collectives Pattern CS 472 Concurrent & Parallel Programming University of Evansville

SOME MORE DECREASE AND CONQUER ALGORITHMS

West Windsor-Plainsboro Regional School District Advanced Topics in Computer Science Grades 9-12

School of Computing and Information Technology. ASSIGNMENT 1 (Individual) CSCI 103 Algorithms and Problem Solving. Session 2, April - June 2017

Sequence Alignment & Computational Thinking

Solving and Analyzing Sudokus with Cultural Algorithms 5/30/2008. Timo Mantere & Janne Koljonen

Selected Game Examples

CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

NOTES ON SEPT 13-18, 2012

Heuristics, and what to do if you don t know what to do. Carl Hultquist

4. Non Adaptive Sorting Batcher s Algorithm

What is a Sorting Function?

Maximum Contiguous Subarray Sum Problems

More Great Ideas in Theoretical Computer Science. Lecture 1: Sorting Pancakes

CSE101: Algorithm Design and Analysis. Ragesh Jaiswal, CSE, UCSD

Past questions from the last 6 years of exams for programming 101 with answers.

1 Permutations. Example 1. Lecture #2 Sept 26, Chris Piech CS 109 Combinatorics

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

Solutions to Exercises Chapter 6: Latin squares and SDRs

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

CSE 100: RED-BLACK TREES

Module 3 Greedy Strategy

MITOCW watch?v=krzi60lkpek

MITOCW R3. Document Distance, Insertion and Merge Sort

Problem 4.R1: Best Range

Tile Complexity of Assembly of Length N Arrays and N x N Squares. by John Reif and Harish Chandran

Improving Text Indexes Using Compressed Permutations

COS 226 Algorithms and Data Structures Fall Midterm Exam

CS 473G: Combinatorial Algorithms, Fall 2005 Homework 0. I understand the Homework Instructions and FAQ.

CS256 Applied Theory of Computation

COS 226 Algorithms and Data Structures Fall Midterm Exam

The Complexity of Sorting with Networks of Stacks and Queues

Solutions to Problem Set 7

Module 3 Greedy Strategy

Skip Lists S 3 S 2 S 1. 2/6/2016 7:04 AM Skip Lists 1

Permutation Editing and Matching via Embeddings

Notes for Recitation 3

Design of Parallel Algorithms. Communication Algorithms

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

Analysis of Workflow Graphs through SESE Decomposition

Computing for Engineers in Python

CS 445 HW#2 Solutions

CSE 21 Practice Final Exam Winter 2016

Game Theory and Randomized Algorithms

Digital Integrated CircuitDesign

Arrays. Independent Part. Contents. Programming with Java Module 3. 1 Bowling Introduction Task Intermediate steps...

Sorting. Suppose behind each door (indicated below) there are numbers placed in a random order and I ask you to find the number 41.

MITOCW watch?v=c6ewvbncxsc

M14/4/COMSC/HP1/ENG/TZ0/XX. Computer science. Paper 1. Friday 16 May 2014 (afternoon) 2 hours 10 minutes INSTRUCTIONS TO CANDIDATES

Content. 1 Understanding and analyzing algorithms. 2 Using graphs and graph algorithms

PROBLEM SET 2 Due: Friday, September 28. Reading: CLRS Chapter 5 & Appendix C; CLR Sections 6.1, 6.2, 6.3, & 6.6;

Raising Permutations to Powers in Place

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Algorithms and Game Theory Date: 12/4/14

Fictitious Play applied on a simplified poker game

Lecture Notes 3: Paging, K-Server and Metric Spaces

SCHEDULING Giovanni De Micheli Stanford University

SORTING BY REVERSALS. based on chapter 7 of Setubal, Meidanis: Introduction to Computational molecular biology

Examination paper for TDT4120 Algorithms and Data Structures

Background. Game Theory and Nim. The Game of Nim. Game is Finite 1/27/2011

Transcription:

1 Sorting... more

2 Divide & conquer Which works better for multi-cores: insertion sort or merge sort? Why?

3 Divide & conquer Which works better for multi-cores: insertion sort or merge sort? Why? Merge sort! After the problem is split, each core and individually sort a sub-list and only merging needs to be done synchronized

4 Quicksort 1. Pick a pivot (any element!) 2. Sort the list into 3 parts: - Elements smaller than pivot - Pivot by itself - Elements larger than pivot 3. Recursively sort smaller & larger

5 Quicksort Pivot Larger Smaller

6 Quicksort Partition(A, start, end) x = A[end] i = start 1 for j = start to end -1 if A[j] < x i = i + 1 swap A[i] and A[j] swap A[i+1] with A[end]

7 Quicksort Sort: {4, 5, 3, 8, 1, 6, 2}

8 Quicksort Sort: {4, 5, 3, 8, 1, 6, 2} Pivot = 2 {4, 5, 3, 8, 1, 6, 2} sort 4 {4, 5, 3, 8, 1, 6, 2} sort 5 {4, 5, 3, 8, 1, 6, 2} sort 3 {4, 5, 3, 8, 1, 6, 2} sort 8 {4, 5, 3, 8, 1, 6, 2} sort 1, swap 4 {1, 5, 3, 8, 4, 6, 2} sort 6 {1, 5, 3, 8, 4, 6, 2},{1, 2, 5, 3, 8, 4, 6}

9 Quicksort For quicksort, you can pick any pivot you want The algorithm is just easier to write if you pick the last element (or first)

10 Quicksort Sort: {4, 5, 3, 8, 1, 6, 2} - Pivot = 3 {4, 5, 2, 8, 1, 6, 3} swap 2 and 3 {4, 5, 2, 8, 1, 6, 3} {4, 5, 2, 8, 1, 6, 3} {2, 5, 4, 8, 1, 6, 3} swap 2 & 4 {2, 5, 4, 8, 1, 6, 3} (first red ^) {2, 1, 4, 8, 5, 6, 3} swap 1 and 5 {2, 1, 4, 8, 5, 6, 3}{2, 1, 3, 8, 5, 6, 4}

11 Quicksort Runtime: Worst case? Average?

12 Quicksort Runtime: Worst case? Always pick lowest/highest element, so O(n 2 ) Average?

13 Quicksort Runtime: Worst case? Always pick lowest/highest element, so O(n 2 ) Average? Sort about half, so same as merge sort on average

14 Quicksort Can bound number of checks against pivot: Let X i,j = event A[i] checked to A[j] sum i,j X i,j = total number of checks E[sum i,j X i,j ]= sum i,j E[X i,j ] = sum i,j Pr(A[i] check A[j]) = sum i,j Pr(A[i] or A[j] a pivot)

15 Quicksort = sum Pr(A[i] or A[j] a pivot) i,j = sum (2 / j-i+1) // j-i+1 possibilties i,j < sum O(lg n) i = O(n lg n)

16 Quicksort Correctness: Base: Initially no elements are in the smaller or larger category Step (loop): If A[j] < pivot it will be added to smaller and smaller will claim next spot, otherwise it it stays put and claims a larger spot Termination: Loop on all elements...

17 Quicksort Two cases:

18 Quicksort Which is better for multi core, quicksort or merge sort? If the average run times are the same, why might you choose quicksort?

19 Quicksort Which is better for multi core, quicksort or merge sort? Neither, quicksort front ends the processing, merge back ends If the average run times are the same, why might you choose quicksort?

20 Quicksort Which is better for multi core, quicksort or merge sort? Neither, quicksort front ends the processing, merge back ends If the average run times are the same, why might you choose quicksort? Uses less space.

21 Sorting! So far we have been looking at comparative sorts (where we only can compute < or >, but have no idea on range of numbers) The minimum running time for this type of algorithm is Θ(n lg n)

24 Comparison sort All n! permutations must be leaves Worst case is tree height

25 Comparison sort A binary tree (either < or >) of height h has 2 h leaves: 2 h > n! lg(2 h ) > lg(n!) (Stirling's approx) h > (n lg n)

26 Comparison sort Today we will make assumptions about the input sequence to get O(n) running time sorts This is typically accomplished by knowing the range of numbers

27 Outline Sorting... again! -Comparison sort -Count sort -Radix sort -Bucket sort

28 Counting sort 1. Store in an array the number of times a number appears 2. Use above to find the last spot available for the number 3. Start from the last element, put it in the last spot (using 2.) decrease last spot array (2.)

32 Counting sort A = input, B= output, C = count for j = 1 to A.length C[ A[ j ]] = C[ A[ j ]] + 1 for i = 1 to k (range of numbers) C[ i ] = C[ i ] + C [ i 1 ] for j = A.length to 1 B[ C[ A[ j ]]] = A[ j ] C[ A[ j ]] = C[ A[ j ]] - 1

33 Counting sort k = 5 (numbers are 2-7) Sort: {2, 7, 4, 3, 6, 3, 6, 3} 1. Find number of times each number appears C = {1, 3, 1, 0, 2, 1} 2, 3, 4, 5, 6, 7

34 Counting sort Sort: {2, 7, 4, 3, 6, 3, 6, 3} 2. Change C to find last place of each element (first index is 1) C = {1, 3, 1, 0, 2, 1} {1, 4, 1, 0, 2, 1} {1, 4, 5, 0, 2, 1}{1, 4, 5, 5, 7, 1} {1, 4, 5, 5, 2, 1}{1, 4, 5, 5, 7, 8}

35 Counting sort Sort: {2, 7, 4, 3, 6, 3, 6, 3} 3. Go start to last, putting each element into the last spot avail. C = {1, 4, 5, 5, 7, 8}, last in list = 3 2 3 4 5 6 7 {,,,3,,,, }, C = 1 2 3 4 5 6 7 8 {1, 3, 5, 5, 7, 8}

36 Counting sort Sort: {2, 7, 4, 3, 6, 3, 6, 3} 3. Go start to last, putting each element into the last spot avail. C = {1, 4, 5, 5, 7, 8}, last in list = 6 2 3 4 5 6 7 {,,,3,,,6, }, C = 1 2 3 4 5 6 7 8 {1, 3, 5, 5, 6, 8}

37 Counting sort Sort: {2, 7, 4, 3, 6, 3, 6, 3} 1 2 3 4 5 6 7 8 2,3,4,5,6,7 {,,,3,,,6, }, C={1,3,5,5,6,8} {,,3,3,,,6, }, C={1,2,5,5,6,8} {,,3,3,,6,6, }, C={1,2,5,5,5,8} {, 3,3,3,,6,6, }, C={1,1,5,5,5,8} {, 3,3,3,4,6,6, }, C={1,1,4,5,5,8} {, 3,3,3,4,6,6,7}, C={1,1,4,5,5,7}

38 Counting sort Run time?

39 Counting sort Run time? Loop over C once, A twice k + 2n = O(n) as k a constant

41 Counting sort Sort: {2, 7, 4, 3, 6, 3, 6, 3} C = {1,3,1,0,2,1} -> {1,4,5,5,7,8} instead C[ i ] = sum j<i C[ j ] C' = {0, 1, 4, 5, 5, 7} Add from start of original and increment

42 Counting sort Counting sort is stable, which means the last element in the order of repeated numbers is preserved from input to output (in example, first '3' in original list is first '3' in sorted list)

43 Radix sort Use a stable sort to sort from the least significant digit to most Psuedo code: (A=input) for i = 1 to d stable sort of A on digit i

44 Radix sort Stable means you can draw lines without crossing for a single digit

45 Radix sort Run time?

46 Radix sort Run time? O( (b/r) (n+2 r ) ) b-bits total, r bits per 'digit' d = b/r digits Each count sort takes O(n + 2 r ) runs count sort d times... O( d(n+2 r )) = O( b/r (n + 2 r ))

47 Radix sort Run time? if b < lg(n), Θ(n) if b > lg(n), Θ(n lg n)

48 Bucket sort 1. Group similar items into a bucket 2. Sort each bucket individually 3. Merge buckets

49 Bucket sort As a human, I recommend this sort if you have large n

50 Bucket sort (specific to fractional numbers) (also assumes n buckets for n numbers) for i = 0 to A.length insert A[ i ] into B[ floor(n A[ i ]) ] for i = 0 to B.length sort list B[ i ] with insertion sort concatenate B[0] to B[1] to B[2]...

51 Bucket sort Run time?

52 Bucket sort Run time? Θ(n)