a) Place each value of the one-dimensional array into a row of the bucket array, based on the value’s “ones” (rightmost) digit. For example, 97 is placed in row 7, 3 is placed in row 3, and 100 is placed in row 0. This procedure is called a distribution pass. b) Loop through the bucket array row by row, and copy the values back to the original array. This procedure is called a gathering pass. The new order of the preceding values in the one-dimensional array is 100, 3 and 97. c) Repeat this process for each subsequent digit position (tens, hundreds, thousands, etc.). On the second (tens digit) pass, 100 is placed in row 0, 3 is placed in row 0 (because 3 has no tens digit), and 97 is placed in row 9. After the gathering pass, the order of the values in the one-dimensional array is 100, 3 and 97. On the third (hundreds digit) pass, 100 is placed in row 1, 3 is placed in row 0, and 97 is placed in row 0 (after the 3). After this last gathering pass, the original array is in sorted order. The two-dimensional array of buckets is 10 times the length of the integer array being sorted. This sorting technique provides better performance than a selection and insertion sorts but requires much more memory—the selection and insertion sorts require space for only one additional element of data. This comparison is an example of a space/time trade-off: The bucket sort uses more memory than the selection and insertion sorts, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays. complete disctribute_element() and collect_element() functions without modifying rest of the code. Code 11.7 # Exercise 11.17 import math import numpy as np     def bucket_sort(data):     """Sort an array's values into ascending order using bucket sort."""       # Determine largest number of digits in numbers to sort     totalDigits = int(math.log10(max(data)) + 1)       # bucket array where numbers will be placed     pail = [[0] * len(data) for i in range(10)]       # go through all digit places and sort each number     # according to digit place value     for pass_number in range(1, totalDigits + 1):         distribute_elements(data, pail, pass_number)  # distribution pass         collect_elements(data, pail)  # gathering pass           if pass_number != totalDigits:             empty_bucket(pail)  # set size of buckets to 0     def distribute_elements(data, pail, digit):     """Distribute elements into buckets based on specified digit"""     """ COMPLETE CODE HERE """     def collect_elements(data, pails):     """Return elements to original array"""     """ COMPLETE CODE HERE """     def empty_bucket(pails):     """Set size of all buckets to zero"""     for i in range(10):         pails[i][0] = 0 # set size of bucket to 0           # MAIN data = np.random.randint(100, size=20)   print(f'Unsorted array: {data}') bucket_sort(data)  # sort array print(f'Sorted array: {data}')

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

11.17 (Bucket Sort) Use Python. Show the whole code input and output when done.

A bucket sort begins with a one-dimensional array of positive integers to be sorted and a two-dimensional array of integers with rows indexed from 0 to 9 and columns indexed from 0 to n – 1, where n is the number of values to be sorted.

Each row of the two-dimensional array is referred to as a bucket. Write a class named BucketSort containing a function called sort that operates as follows:

  1. a) Place each value of the one-dimensional array into a row of the bucket array, based on the value’s “ones” (rightmost) digit. For example, 97 is placed in row 7, 3 is placed in row 3, and 100 is placed in row 0. This procedure is called a distribution pass.
  2. b) Loop through the bucket array row by row, and copy the values back to the original array. This procedure is called a gathering pass. The new order of the preceding values in the one-dimensional array is 100, 3 and 97.
  3. c) Repeat this process for each subsequent digit position (tens, hundreds, thousands, etc.). On the second (tens digit) pass, 100 is placed in row 0, 3 is placed in row 0 (because 3 has no tens digit), and 97 is placed in row 9. After the gathering pass, the order of the values in the one-dimensional array is 100, 3 and 97. On the third (hundreds digit) pass, 100 is placed in row 1, 3 is placed in row 0, and 97 is placed in row 0 (after the 3). After this last gathering pass, the original array is in sorted order.

The two-dimensional array of buckets is 10 times the length of the integer array being sorted. This sorting technique provides better performance than a selection and insertion sorts but requires much more memory—the selection and insertion sorts require space for only one additional element of data. This comparison is an example of a space/time trade-off: The bucket sort uses more memory than the selection and insertion sorts, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays.

complete disctribute_element() and collect_element() functions without modifying rest of the code.

Code 11.7

# Exercise 11.17

import math

import numpy as np

 

 

def bucket_sort(data):

    """Sort an array's values into ascending order using bucket sort."""

 

    # Determine largest number of digits in numbers to sort

    totalDigits = int(math.log10(max(data)) + 1)

 

    # bucket array where numbers will be placed

    pail = [[0] * len(data) for i in range(10)]

 

    # go through all digit places and sort each number

    # according to digit place value

    for pass_number in range(1, totalDigits + 1):

        distribute_elements(data, pail, pass_number)  # distribution pass

        collect_elements(data, pail)  # gathering pass

 

        if pass_number != totalDigits:

            empty_bucket(pail)  # set size of buckets to 0

 

 

def distribute_elements(data, pail, digit):

    """Distribute elements into buckets based on specified digit"""

    """ COMPLETE CODE HERE """

 

 

def collect_elements(data, pails):

    """Return elements to original array"""

    """ COMPLETE CODE HERE """

 

 

def empty_bucket(pails):

    """Set size of all buckets to zero"""

    for i in range(10):

        pails[i][0] = 0 # set size of bucket to 0

       

 

# MAIN

data = np.random.randint(100, size=20)

 

print(f'Unsorted array: {data}')

bucket_sort(data)  # sort array

print(f'Sorted array: {data}')

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Quicksort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education