Download presentation

Presentation is loading. Please wait.

Published byJazmin Wiles Modified over 2 years ago

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

2
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

3
An O(N) Sort Algorithm Consider a sorted list of unique integers, ranging from 0 to N - 1: 0123 0 1 2 3 4 4

4
An O(N) Sort Algorithm Consider a sorted list of unique integers, ranging from 0 to N - 1: 0123 0 1 2 3 4 4 Shuffle the list to randomize the numbers: 2104 0 1 2 3 4 3

5
An O(N) Sort Algorithm Consider a sorted list of unique integers, ranging from 0 to N - 1: 0123 0 1 2 3 4 4 Shuffle the list to randomize the numbers: 2104 0 1 2 3 4 3 How can we sort this randomly ordered list in linear time?

6
An O(N) Sort Algorithm 0 1 2 3 4 Create a temporary array of length N: 2104 0 1 2 3 4 3 How can we sort this randomly ordered list in linear time?

7
An O(N) Sort Algorithm 2 0 1 2 3 4 2104 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

8
An O(N) Sort Algorithm 12 0 1 2 3 4 2104 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

9
An O(N) Sort Algorithm 012 0 1 2 3 4 2104 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

10
An O(N) Sort Algorithm 012 0 1 2 3 4 4 2104 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

11
An O(N) Sort Algorithm 0123 0 1 2 3 4 4 2104 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

12
An O(N) Sort Algorithm 0123 0 1 2 3 4 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 position Copy ‘em back to the list 01234

13
Complexity Analysis 0123 0 1 2 3 4 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 position Copy ‘em back to the list 01234 No comparisons! 2 * N assignments O(N) memory

14
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: 2104 0 1 2 3 4 5 6 7 3 011 Each linked list will serve as a bucket to receive items from the original list

15
Lists with Duplicate Items 0 1 2 3 4 Copy items from the original list to the corresponding buckets in the array 2104 0 1 2 3 4 5 6 7 3 011 1 1 1 0234 0

16
Lists with Duplicate Items Copy ‘em back to the original list 0011 0 1 2 3 4 5 6 7 1 234 0 1 2 3 4 1 1 1 0234 0 No comparisons! 2 * N assignments O(N + K) memory

17
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

18
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

19
Some Buckets Can Be Empty 0 1 2 3 4 3104 0 1 2 3 4 5 6 7 3 011 1 1 1 0 3 34 0

20
For Wednesday Hashing and O(k) Sets and Dictionaries

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google