1 / 20

Computer Science 112

Computer Science 112. Fundamentals of Programming II Bucket Sort: An O( N ) Sort Algorithm. N 2 and N log 2 N Sort Algorithms. Selection sort and bubble sort are O( N 2 ), because they run nested loops over the entire list

Download Presentation

Computer Science 112

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

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

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

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

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

  7. An O(N) Sort Algorithm For each integer in the unsorted list: Copy the integer to the array at that position 0 1 2 3 4 2 2 1 0 4 3 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

  8. An O(N) Sort Algorithm For each integer in the unsorted list: Copy the integer to the array at that position 0 1 2 3 4 1 2 2 1 0 4 3 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

  9. An O(N) Sort Algorithm For each integer in the unsorted list: Copy the integer to the array at that position 0 1 2 3 4 0 1 2 2 1 0 4 3 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

  10. An O(N) Sort Algorithm For each integer in the unsorted list: Copy the integer to the array at that position 0 1 2 3 4 0 1 2 4 2 1 0 4 3 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

  11. An O(N) Sort Algorithm For each integer in the unsorted list: Copy the integer to the array at that position 0 1 2 3 4 0 1 2 3 4 2 1 0 4 3 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

  12. An O(N) Sort Algorithm For each integer in the unsorted list: Copy the integer to the array at that position Copy ‘em back to the list 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

  13. Complexity Analysis For each integer in the unsorted list: Copy the integer to the array at that position Copy ‘em back to the list 0 1 2 3 4 No comparisons! 2 * N assignments O(N) memory 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 How can we sort this randomly ordered list in linear time?

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

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

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

  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. Bucket Sort of a Keyed List from arrays import Array from node import Node defbucketSort(keyedList): # Create an array to accommodate the keys array = Array(keyedList.getMaxKey()) # Copy items from the list to the buckets for item inkeyedList: 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 Computer Science 112

  19. Some Buckets Can Be Empty 0 1 2 3 4 0 1 3 4 0 1 3 1 3 1 0 4 3 0 1 1 0 1 2 3 4 5 6 7

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

More Related