Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm
N 2 and Nlog 2 N Sort Algorithms Selection sort and bubble sort are O(N 2 ), because they run nested loops over the entire list Quicksort and heap sort are O(Nlog 2 N), because one executes a linear process log 2 N times and the other executes a log 2 N process N times
An O(N) Sort Algorithm Consider a sorted list of unique integers, ranging from 0 to N - 1:
An O(N) Sort Algorithm Consider a sorted list of unique integers, ranging from 0 to N - 1: Shuffle the list to randomize the numbers:
An O(N) Sort Algorithm Consider a sorted list of unique integers, ranging from 0 to N - 1: Shuffle the list to randomize the numbers: How can we sort this randomly ordered list in linear time?
An O(N) Sort Algorithm Create a temporary array of length N: How can we sort this randomly ordered list in linear time?
An O(N) Sort Algorithm How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position
An O(N) Sort Algorithm How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position
An O(N) Sort Algorithm How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position
An O(N) Sort Algorithm How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position
An O(N) Sort Algorithm How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position
An O(N) Sort Algorithm How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position Copy ‘em back to the list 01234
Complexity Analysis How can we sort this randomly ordered list in linear time? For each integer in the unsorted list: Copy the integer to the array at that position Copy ‘em back to the list No comparisons! 2 * N assignments O(N) memory
Lists with Duplicate Items Create a temporary array of linked lists of length K, for the integers in the list ranging from 0 to K - 1: Each linked list will serve as a bucket to receive items from the original list
Lists with Duplicate Items Copy items from the original list to the corresponding buckets in the array
Lists with Duplicate Items Copy ‘em back to the original list No comparisons! 2 * N assignments O(N + K) memory
Generalize to a Keyed List Each item in the list must have an integer key The keys can be repeated, but must be integers from 0 through a positive upper bound The keys can be stored with the items, or computed as needed
Computer Science 112 from arrays import Array from node import Node def bucketSort(keyedList): # Create an array to accommodate the keys array = Array(keyedList.getMaxKey()) # Copy items from the list to the buckets for item in keyedList: key = item.getKey() array[key] = Node(item, array[key]) # Copy items from buckets back to the list index = 0 for node in array: while node != None: keyedList[index] = node.data node = node.next index += 1 Bucket Sort of a Keyed List
Some Buckets Can Be Empty
For Wednesday Hashing and O(k) Sets and Dictionaries