mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 02:13:15 +08:00
Added Whitespace and Docstring (#924)
* Added Whitespace and Docstring I modified the file to make Pylint happier and make the code more readable. * Beautified Code and Added Docstring I modified the file to make Pylint happier and make the code more readable. * Added DOCSTRINGS, Wikipedia link, and whitespace I added DOCSTRINGS and whitespace to make the code more readable and understandable. * Improved Formatting * Wrapped comments * Fixed spelling error for `movement` variable * Added DOCSTRINGs * Improved Formatting * Corrected whitespace to improve readability. * Added docstrings. * Made comments fit inside an 80 column layout.
This commit is contained in:
@ -1,19 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""Illustrate how to implement bucket sort algorithm."""
|
||||
|
||||
# Author: OMKAR PATHAK
|
||||
# This program will illustrate how to implement bucket sort algorithm
|
||||
|
||||
# Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
|
||||
# elements of an array into a number of buckets. Each bucket is then sorted individually, either using
|
||||
# a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a
|
||||
# distribution sort, and is a cousin of radix sort in the most to least significant digit flavour.
|
||||
# Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons
|
||||
# and therefore can also be considered a comparison sort algorithm. The computational complexity estimates
|
||||
# involve the number of buckets.
|
||||
# Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
|
||||
# by distributing the elements of an array into a number of buckets.
|
||||
# Each bucket is then sorted individually, either using a different sorting
|
||||
# algorithm, or by recursively applying the bucket sorting algorithm. It is a
|
||||
# distribution sort, and is a cousin of radix sort in the most to least
|
||||
# significant digit flavour.
|
||||
# Bucket sort is a generalization of pigeonhole sort. Bucket sort can be
|
||||
# implemented with comparisons and therefore can also be considered a
|
||||
# comparison sort algorithm. The computational complexity estimates involve the
|
||||
# number of buckets.
|
||||
|
||||
# Time Complexity of Solution:
|
||||
# Best Case O(n); Average Case O(n); Worst Case O(n)
|
||||
|
||||
DEFAULT_BUCKET_SIZE=5
|
||||
DEFAULT_BUCKET_SIZE = 5
|
||||
|
||||
|
||||
def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
|
||||
if len(my_list) == 0:
|
||||
@ -24,12 +31,14 @@ def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
|
||||
buckets = [[] for _ in range(int(bucket_count))]
|
||||
|
||||
for i in range(len(my_list)):
|
||||
buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i])
|
||||
buckets[int((my_list[i] - min_value) // bucket_size)
|
||||
].append(my_list[i])
|
||||
|
||||
return sorted([buckets[i][j] for i in range(len(buckets))
|
||||
for j in range(len(buckets[i]))])
|
||||
for j in range(len(buckets[i]))])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
user_input = input('Enter numbers separated by a comma:').strip()
|
||||
unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0]
|
||||
print(bucket_sort(unsorted))
|
||||
print(bucket_sort(unsorted))
|
||||
|
Reference in New Issue
Block a user