Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

20
Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm

Transcript of Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Page 1: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Computer Science 112

Fundamentals of Programming IIBucket Sort: An O(N) Sort Algorithm

Page 2: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

N2 and Nlog2N Sort Algorithms

• Selection sort and bubble sort are O(N2), because they run nested loops over the entire list

• Quicksort and heap sort are O(Nlog2N), because one executes a linear process log2N times and the other executes a log2N process N times

Page 3: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

Consider a sorted list of unique integers, ranging from 0 to N - 1:

0 1 2 3 0 1 2 3 4

4

Page 4: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

Consider a sorted list of unique integers, ranging from 0 to N - 1:

0 1 2 3 0 1 2 3 4

4

Shuffle the list to randomize the numbers:

2 1 0 4 0 1 2 3 4

3

Page 5: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

Consider a sorted list of unique integers, ranging from 0 to N - 1:

0 1 2 3 0 1 2 3 4

4

Shuffle the list to randomize the numbers:

2 1 0 4 0 1 2 3 4

3

How can we sort this randomly ordered list in linear time?

Page 6: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

0 1 2 3 4

Create a temporary array of length N:

2 1 0 4 0 1 2 3 4

3

How can we sort this randomly ordered list in linear time?

Page 7: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

2 0 1 2 3 4

2 1 0 4 0 1 2 3 4

3

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

Page 8: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

1 2 0 1 2 3 4

2 1 0 4 0 1 2 3 4

3

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

Page 9: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

0 1 2 0 1 2 3 4

2 1 0 4 0 1 2 3 4

3

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

Page 10: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

0 1 2 0 1 2 3 4

4

2 1 0 4 0 1 2 3 4

3

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

Page 11: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

0 1 2 3 0 1 2 3 4

4

2 1 0 4 0 1 2 3 4

3

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

Page 12: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

An O(N) Sort Algorithm

0 1 2 3 0 1 2 3 4

4

0 1 2 3 4

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 positionCopy ‘em back to the list

0 1 2 3 4

Page 13: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Complexity Analysis

0 1 2 3 0 1 2 3 4

4

0 1 2 3 4

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 positionCopy ‘em back to the list

0 1 2 3 4

No comparisons!

2 * N assignments

O(N) memory

Page 14: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Lists with Duplicate Items

0 1 2 3 4

Create a temporary array of linked lists of length K, for the integers in the list ranging from 0 to K - 1:

2 1 0 4 0 1 2 3 4 5 6 7

3 0 1 1

Each linked list will serve as a bucket to receive items from the original list

Page 15: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Lists with Duplicate Items

0 1 2 3 4

Copy items from the original list to the corresponding buckets in the array

2 1 0 4 0 1 2 3 4 5 6 7

3 0 1 1

1

1

1

0 2 3 4

0

Page 16: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Lists with Duplicate Items

Copy ‘em back to the original list

0 0 1 1 0 1 2 3 4 5 6 7

1 2 3 4

0 1 2 3 4

1

1

1

0 2 3 4

0

No comparisons!

2 * N assignments

O(N + K) memory

Page 17: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

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

Page 18: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Computer Science 112

from arrays import Arrayfrom 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

Page 19: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

Some Buckets Can Be Empty

0 1 2 3 4

3 1 0 4 0 1 2 3 4 5 6 7

3 0 1 1

1

1

1

0

3

3 4

0

Page 20: Computer Science 112 Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm.

For Wednesday

Hashing and O(k) Sets and Dictionaries