mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-21 08:42:03 +00:00
Added doctests to bucket sort (#2079)
* Added doctests to bucket sort * Missing typehint * Wrap long lines * updating DIRECTORY.md * Update bucket_sort.py * updating DIRECTORY.md * Update bucket_sort.py Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
6f80ca821c
commit
d7a75da8ef
|
@ -147,6 +147,7 @@
|
||||||
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
|
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
|
||||||
|
|
||||||
## Digital Image Processing
|
## Digital Image Processing
|
||||||
|
* [Change Brightness](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_brightness.py)
|
||||||
* [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
|
* [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
|
||||||
* [Convert To Negative](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/convert_to_negative.py)
|
* [Convert To Negative](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/convert_to_negative.py)
|
||||||
* Dithering
|
* Dithering
|
||||||
|
@ -268,6 +269,7 @@
|
||||||
## Hashes
|
## Hashes
|
||||||
* [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py)
|
* [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py)
|
||||||
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
|
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
|
||||||
|
* [Djb2](https://github.com/TheAlgorithms/Python/blob/master/hashes/djb2.py)
|
||||||
* [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py)
|
* [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py)
|
||||||
* [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py)
|
* [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py)
|
||||||
* [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py)
|
* [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py)
|
||||||
|
|
|
@ -1,34 +1,54 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Illustrate how to implement bucket sort algorithm.
|
||||||
|
|
||||||
"""Illustrate how to implement bucket sort algorithm."""
|
Author: OMKAR PATHAK
|
||||||
|
This program will illustrate how to implement bucket sort algorithm
|
||||||
|
|
||||||
# Author: OMKAR PATHAK
|
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works
|
||||||
# This program will illustrate how to implement bucket sort algorithm
|
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
|
Time Complexity of Solution:
|
||||||
# by distributing the elements of an array into a number of buckets.
|
Worst case scenario occurs when all the elements are placed in a single bucket.
|
||||||
# Each bucket is then sorted individually, either using a different sorting
|
The overall performance would then be dominated by the algorithm used to sort each
|
||||||
# algorithm, or by recursively applying the bucket sorting algorithm. It is a
|
bucket. In this case, O(n log n), because of TimSort
|
||||||
# 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:
|
Average Case O(n + (n^2)/k + k), where k is the number of buckets
|
||||||
# Worst case scenario occurs when all the elements are placed in a single bucket. The
|
|
||||||
# overall performance would then be dominated by the algorithm used to sort each bucket.
|
|
||||||
# In this case, O(n log n), because of TimSort
|
|
||||||
#
|
|
||||||
# Average Case O(n + (n^2)/k + k), where k is the number of buckets
|
|
||||||
#
|
|
||||||
# If k = O(n), time complexity is O(n)
|
|
||||||
|
|
||||||
|
If k = O(n), time complexity is O(n)
|
||||||
|
|
||||||
|
Source: https://en.wikipedia.org/wiki/Bucket_sort
|
||||||
|
"""
|
||||||
DEFAULT_BUCKET_SIZE = 5
|
DEFAULT_BUCKET_SIZE = 5
|
||||||
|
|
||||||
|
|
||||||
def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
|
def bucket_sort(my_list: list, bucket_size: int = DEFAULT_BUCKET_SIZE) -> list:
|
||||||
|
"""
|
||||||
|
>>> data = [-1, 2, -5, 0]
|
||||||
|
>>> bucket_sort(data) == sorted(data)
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> data = [9, 8, 7, 6, -12]
|
||||||
|
>>> bucket_sort(data) == sorted(data)
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> data = [.4, 1.2, .1, .2, -.9]
|
||||||
|
>>> bucket_sort(data) == sorted(data)
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> bucket_sort([])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
Exception: Please add some elements in the array.
|
||||||
|
"""
|
||||||
if len(my_list) == 0:
|
if len(my_list) == 0:
|
||||||
raise Exception("Please add some elements in the array.")
|
raise Exception("Please add some elements in the array.")
|
||||||
|
|
||||||
|
@ -40,11 +60,10 @@ def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
|
||||||
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(
|
return sorted(
|
||||||
[buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))]
|
buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
user_input = input("Enter numbers separated by a comma:").strip()
|
assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
|
||||||
unsorted = [float(n) for n in user_input.split(",") if len(user_input) > 0]
|
assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
|
||||||
print(bucket_sort(unsorted))
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user