CS61BL. Lecture 5: Graphs Sorting

Similar documents
Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Data Structures and Algorithms

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

We can use a max-heap to sort data.

COMP Data Structures

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Data Structures Brett Bernstein

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CS350: Data Structures Dijkstra s Shortest Path Alg.

Searching in General

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Sorting and Selection

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Programming II (CS300)

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Practice Problems for the Final

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

CSci 231 Final Review

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Design and Analysis of Algorithms - - Assessment

Announcements Problem Set 4 is out!

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

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

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

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

CSE 100: GRAPH ALGORITHMS

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

About this exam review

CSC 273 Data Structures

CS240 Fall Mike Lam, Professor. Quick Sort

Real-world sorting (not on exam)

Question 7.11 Show how heapsort processes the input:

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

CSC263 Week 8. Larry Zhang.

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

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

SORTING AND SELECTION

Overview of Sorting Algorithms

Faster Sorting Methods

SORTING, SETS, AND SELECTION

Lecture Summary CSC 263H. August 5, 2016

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Sorting Pearson Education, Inc. All rights reserved.

CSE 373: Practice Final

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Cpt S 122 Data Structures. Sorting

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Algorithms and Applications

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

A loose end: binary search

CS Data Structures and Algorithm Analysis

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Chapter 9. Priority Queue

CS 61BL Data Structures & Programming Methodology

Data Structures Question Bank Multiple Choice

INSTITUTE OF AERONAUTICAL ENGINEERING

Data Structures and Algorithms

Prelim 2, CS2110. SOLUTION

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Module 2: Classical Algorithm Design Techniques

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

CS 310 Advanced Data Structures and Algorithms

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Graphs & Digraphs Tuesday, November 06, 2007

Sorting Goodrich, Tamassia Sorting 1

Mergesort again. 1. Split the list into two equal parts

Course Review for Finals. Cpt S 223 Fall 2008

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

Multiple-choice (35 pt.)

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Programming II (CS300)

Algorithms: Design & Practice

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Solutions to relevant spring 2000 exam problems

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

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

Total Score /1 /20 /41 /15 /23 Grader

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS302 Data Structures using C++

COMP 251 Winter 2017 Online quizzes with answers

Lecture 8: Mergesort / Quicksort Steven Skiena

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Quick Sort. CSE Data Structures May 15, 2002

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Transcription:

CS61BL Lecture 5: Graphs Sorting

Graphs

Graphs Edge Vertex

Graphs (Undirected)

Graphs (Directed)

Graphs (Multigraph)

Graphs (Acyclic)

Graphs (Cyclic)

Graphs (Connected)

Graphs (Disconnected)

Graphs (Unweighted)

Graphs (Weighted) 3 5 1-1 2 1

Graph Representation Adjacency Matrix A Matrix (e.g. 2D array) specifying which edges are present in a graph A B C D E F A 0 1 0 0 0 0 B D B 0 0 0 1 1 0 F C 1 0 0 0 0 0 D 0 0 0 0 0 1 C E E 0 0 1 0 0 1 F 0 0 0 0 0 0

Graph Representation Adjacency Matrix A 3 Matrix (e.g. 2D array) specifying which edges are present in a graph A B C D E F 1 A X 3 X X X X B D 1 B X X X 1 1 X -1 C 5 X X X X X F D X X X X X 1 C E 2 E X X 3 X X 2 3 F X X X X X X 5

Graph Representation Adjacency List Collection of lists of neighbors Each list specifies a single vertex s neighbors A B C D E F A: [B] B: [D, E] C: [A] D: [F] E: [C, F] F: [ ]

Graph Representation Adjacency List Collection of lists of neighbors Each list specifies a single vertex s neighbors A 3 5 B C 1 3-1 D E 1 2 F A: [(B, 3)] B: [(D, 1), (E, -1)] C: [(A, 5)] D: [(F, 1)] E: [(C, 3), (F, 2)] F: [ ]

Graph Traversal Almost same as tree traversal Keep track of which vertices have been already been visited Pick any vertex to start at instead of root BFS, DFS

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E A

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E B

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E D E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E E F

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E F

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E F C

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E C

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E F

Graph Traversal: BFS Example Mark starting vertex as visited and add to queue While queue is not empty: A Remove a vertex from queue Mark its unvisited neighbors as visited and add to queue B D F Head of queue C E

Shortest Path Problem Find shortest path from some start vertex s to some end vertex t Google Maps, puzzles, pathing AIs, etc Unweighted graph: use BFS Weighted graph:?

Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B 1 B 1 C A 3 A C

Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B 100? 1 C A 300

Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B 1000000? 1 C A 3000000

Shortest Path Problem Weighted graph? Split each weighted edge into multiple intermediary vertices and edges? Then run BFS? B 3000000? 1 C A 3000000

Dijkstra s Algorithm Finds shortest path from start vertex to every other vertex in graph Use priority queue as fringe Priority value of vertex u is shortest path length we ve found from starting vertex s so far

Dijkstra s Algorithm Example Add starting vertex s to priority queue w/ priority 0 Add all other vertices with priority While priority queue is not empty: u = vertex dequeued from priority queue For each neighbor v: If v is in priority queue, update v s priority value with minimum of: Current priority Priority of u + weight of edge (u, v)

Dijkstra s Algorithm Example Add s to priority queue with priority 0 Add all other vertices with priority Priority Queue A B C D E 1 B A 5 2 0 D 1 9 C 3 4 E Shortest Distances A B C D E?????

Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeue d from priority queue Priority Queue A B C D E 1 B A 5 2 D 1 9 C 3 4 E Shortest Distances A B C D E 0????

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 D 1 9 C 3 4 E Shortest Distances A B C D E 0????

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 1 9 5 D 1 9 C 3 4 E Shortest Distances A B C D E 0????

Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E 1 B A 5 2 9 5 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1???

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 9 5 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1???

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 9 3 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1???

Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E 1 B A 5 2 9 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3?

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 9 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3?

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 6 4 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3?

Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E 1 B A 5 2 6 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3 4

Dijkstra s Algorithm Example For each neighbor v, if v is in priority queue, update v s priority value with minimum of: [Current priority, Priority of u + weight of edge (u, v)] Priority Queue A B C D E 1 B A 5 2 6 D 1 9 C 3 4 E Shortest Distances A B C D E 0 1? 3 4

Dijkstra s Algorithm Example While priority queue is not empty: u = vertex dequeued from priority queue Priority Queue A B C D E B 2 D 1 1 A 5 9 C 3 4 E Shortest Distances A B C D E 0 1 6 3 4

Dijkstra Example Priority Queue A B C B -2 0 3 A 2 C Shortest Distances A B C???

Dijkstra Example Priority Queue A B C B -2 3 A 2 C Shortest Distances A B C 0??

Dijkstra Example Priority Queue A B C B -2 3 2 3 A 2 C Shortest Distances A B C 0??

Dijkstra Example Priority Queue A B C B -2 3 3 A 2 C Shortest Distances A B C 0? 2

Dijkstra Example Priority Queue A B C B -2 3 A 2 C Shortest Distances A B C 0 3 2

Dijkstra Example Priority Queue A B C B -2 3 A 2 C Shortest Distances A B C 0 3 2

Another Example Priority Queue A B C B 2 0 1 A 2 C Shortest Distances A B C???

Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0??

Another Example Priority Queue A B C B 2 1 1 A 2 C Shortest Distances A B C 0??

Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0 1?

Another Example Priority Queue A B C B 2 3 1 A 2 C Shortest Distances A B C 0 1?

Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0 1 3

Another Example Priority Queue A B C B 2 1 A 2 C Shortest Distances A B C 0 1 3 Cycles are fine

Why do we care about graphs? Facebook, LinkedIn Maps (Google Maps, GPS) The internet Puzzle configurations (Rubik s cube, project 3)

Final Exam Information Next Wednesday (8/13), 3-6pm Last name A-H, Q-Z: 2050 VLSB Last name J-P: 100 GPB All lab and quiz material up to and including this Friday s (balanced search trees) Review Session Sunday (8/10), 1-3pm in 100 GPB

Sorting Given a collection of elements, sort them from smallest to largest

Terminology: Stable Elements with the same value are ordered the same before and after sorting Original: 1 2 3 1 2 3 3 4 Sorted (stable): 1 1 2 2 3 3 3 4 Sorted (unstable): 1 1 2 2 3 3 3 4

Terminology: In-place Only uses O(1) additional space E.g. doesn t copy all contents of input array into a new array Used to describe any algorithm, not just sorting algorithms

Selection Sort for i = 0, 1,..., (n-1): Search positions i to(n-1)for smallest element Swap with element at position i Stable, in-place Best / average / worst case running time: O(n 2 ) for a list with n elements

Selection Sort Example Current index Smallest found this iteration Already sorted http://en.wikipedia.org/wiki/selection_sort#mediaviewer/file:selection-sort-animation.gif

Bubble Sort for i = 0, 1,..., (n-1): Go through the first n-i elements Swap pairs of adjacent elements that are out of order Stable, in-place Average / worst case running time: O n 2 for a list with n elements Best case running time: O n with optimization

Bubble Sort Example http://en.wikipedia.org/wiki/bubble_sort#mediaviewer/file:bubble-sort-example-300px.gif

Insertion Sort for i = 0, 1,..., (n-1): Take element at index i Insert it into the correct location in the sorted sublist consisting of the first i+1 elements Stable, in-place Average / worst case running time: O n 2 list with n elements Best case running time: O n for a

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Insertion Sort Example

Faster Sorting Divide and Conquer: Solve large problem by splitting it into smaller instances of same problem

Heap Sort (From last lecture) Create heap Remove smallest element from heap, one at a time NOT Stable In-place* Best / average / worst case running time: O(n log n) for a list with n elements

In-place Heap Sort 1 3 6 5 8 9 7 0 1 2 3 4 5 6 7 X 1 3 6 5 8 9 7

In-place Heap Sort 7 3 6 5 8 9 1 0 1 2 3 4 5 6 7 X 7 3 6 5 8 9 1

In-place Heap Sort 3 5 6 7 8 9 0 1 2 3 4 5 6 0 X 3 5 6 7 8 9 1

In-place Heap Sort 9 5 6 7 8 3 0 1 2 3 4 5 6 0 X 9 5 6 7 8 3 1

In-place Heap Sort 5 7 6 9 8 0 1 2 3 4 5 0 1 X 5 7 6 9 8 3 1

In-place Heap Sort Etc

In-place Heap Sort 0 1 2 3 4 5 6 X 9 8 7 6 5 3 1

Merge Sort Base case: list of 1 element is already sorted Recursively sort left and right halves Merge Stable* Can be done in-place (not efficient) Best / average / worst case running time: O(n log n) for a list with n elements

Merge Sort Running Time

Merge Sort Running Time

Merge Sort Running Time 1 merge: lists of n 2 elements each 2 merges: lists of n 4 elements each n 2 merges: lists of 1 element each

Merge Sort Running Time 1 merge: lists of n elements each 2 2 merges: lists of n elements each 4 n 2 merges: lists of 1 element each O(n) O(n) O(n) log n depth

Merge Sort Running Time 1 merge: lists of n elements each 2 2 merges: lists of n elements each 4 n 2 merges: lists of 1 element each O(n) O(n) O(n) log n depth O(n log n)

Quick Sort Base case: list of 1 element is already sorted Choose one of the list elements as the pivot Partition list into elements that are less than pivot and elements that are greater than pivot Recursively do this on both partitions Stable, in-place Best / average case running time: O(n log n) where n is number of elements in input list Worst case running time: O(n 2 )

Quick Sort Running Time 6 3 2 9 4 5 1 11 10 7 8 Pivot 3 2 4 5 1 6 9 11 10 7 8 Pivot Pivot 2 1 3 4 5 6 7 8 9 11 10

Quick Sort Running Time Pivot 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Pivot 1 2 3 4 5 6 7 8

Quick Sort Running Time All partitioning at each depth level: O(n) time Number of depth levels: O(log n) to O(n) Total running time: O(n log n) to O(n 2 )

Sorting in practice Base case: use insertion sort on smaller lists Quick sort is fastest on randomized lists Choosing a pivot (options): Choose first element of list Calculate median Pick 3 elements at random and take median

Java s Arrays.sort Dual-pivot quicksort! Vladimir Yaroslavskiy, 2009 Used in Java s Arrays.sort starting with Java 7 (2011)

Comparison Sorts Compares elements Given items a and b: a > b? a < b? (a == b?) All sorting algorithms we ve learned so far Selection, bubble, insertion, merge, heap, quick

Comparison Sorts: Number of Comparisons (Lower Bound) Each comparison distinguishes between 2 possibilities f(n) comparisons 2 f n list permutations n elements: n! possible list permutations 2 f n n! f n log 2 (n!)

Comparison Sorts Lower bound on number of comparisons: log 2 (n!) Want to show: log 2 (n!) Ω(n log 2 n)

Want to show: log 2 (n!) Ω(n log 2 n) log 2 (n!) = log 2 n + log 2 n 1 + + log 2 2 n > log 2 n + log 2 n 1 + + log 2 2 > log 2 n 2 + log 2 n 2 + + log 2 n 2 = n 2 log 2 n 2

Want to show: log 2 (n!) Ω(n log 2 n) log 2 n! > n log 2 2 2 n Θ(n log 2 ) 2 Θ(n log 2 n n log 2 2) Θ(n log 2 n) log 2 (n!) Ω(n log 2 n) n Lower bound on number of comparisons: Ω(n log 2 n)

Comparison Sorts Lower bound on number of comparisons: Ω(n log 2 n)

Non-comparison Sorts We can do better

Counting Sort Input: Large unsorted list of n integers in the range [0, k] (hopefully k n) Solution: Create an int array of size k Traverse input list Increment int at array position i whenever you come across i

Counting Sort Example Input list 4 1 2 1 Counter array 0 0 0 0 0 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 0 0 0 1 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 0 0 0 1 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 1 0 0 1 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 1 0 0 1 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 1 1 0 1 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 1 1 0 1 0 1 2 3 4

Counting Sort Example Input list 4 1 2 1 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 2 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 2 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 2 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 2 4 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Example Output: 1 1 2 4 Counter array 0 2 1 0 1 0 1 2 3 4

Counting Sort Best / average / worst case running time: O n + k Stable? N/A In-place? No

Radix Sort (version 1) Base case: list of only 1 element is already sorted Put elements into buckets according to most significant digit Recursively sort each bucket on next most significant digit (and so forth)

Radix Sort v1 Example Input list: 49, 26, 32, 39, 65, 61, 33, 31

Radix Sort v1 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Put elements into buckets according to most significant digit 33 21 39 65 26 49 32 61 0 1 2 3 4 5 6 7 8 9

Radix Sort v1 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Recursively sort on next most significant digit(s) 39 26 33 65 21 49 32 61 0 1 2 3 4 5 6 7 8 9

Radix Sort v1 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Output sorted list: 21, 26, 32, 33, 39, 49, 61, 65 39 26 33 65 21 49 32 61 0 1 2 3 4 5 6 7 8 9

Radix Sort (version 2) Base case: list of size 1 is already sorted Put elements into buckets according to least significant digit Output all of the elements to a list: In bucket order In the order they were added to each bucket Repeat with next digit until you reach the most significant digit

Radix Sort v2 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21

Radix Sort v2 Example Input list: 49, 26, 32, 39, 61, 65, 33, 21 Put elements into buckets according to least significant digit 21 61 32 33 65 39 26 49 0 1 2 3 4 5 6 7 8 9

Radix Sort v2 Example Output all of the elements to a list: In bucket order In the order they were added to each bucket Output: 61, 21, 32, 33, 65, 26, 49, 39 21 61 32 33 65 39 26 49 0 1 2 3 4 5 6 7 8 9

Radix Sort v2 Example Input: 61, 21, 32, 33, 65, 26, 49, 39 Repeat with next digit 39 26 33 65 21 32 49 61 0 1 2 3 4 5 6 7 8 9

Radix Sort v2 Example Input: 61, 21, 32, 33, 65, 26, 49, 39 Output: 21, 26, 32, 33, 39, 49, 61, 65 39 26 33 65 21 32 49 61 0 1 2 3 4 5 6 7 8 9

Radix Sort Running Time Best / average / worst case: O( n + b k) n: Number of elements in input list b: Number of buckets k: Number of digits of largest number Usually, b n, so running time can be reduced to: O(nk) If k is also small (basically a constant), running time can be reduced to: O n Sorting Java ints: b=2, k = 32

Good luck next week.