Skip to content

Add Bead Sort Algorithm #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from algorithms.radixSort import radixSort
from algorithms.treeSort import treeSort
from algorithms.slowSort import slowSort
from algorithms.beadSort import beadSort


__all__ = [
Expand Down Expand Up @@ -53,4 +54,5 @@
"treeSort",
"exchangeSort",
"slowSort",
"beadSort",
]
50 changes: 50 additions & 0 deletions src/algorithms/beadSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
def beadSort(array, *args):
"""
Bead Sort (also known as Gravity Sort) is a natural sorting algorithm inspired by
beads falling under gravity on parallel vertical rods.

For each number in the input array, imagine a row of beads on vertical rods.
The number of beads in each row corresponds to the value of the number.
When gravity is applied, beads fall down as far as possible, naturally sorting the array.

This algorithm only works on positive integers.

Time complexity: O(n + m), where n is the number of elements and m is the maximum value
Space complexity: O(n*m) for the beads matrix
"""

if not array:
return

if any(not isinstance(x, int) or x < 0 for x in array):
raise ValueError("Bead sort only works with non-negative integers.")

n = len(array)
max_val = max(array)

beads = [[0 for _ in range(n)] for _ in range(max_val)]

# Drop beads (setup)
for col in range(n):
for row in range(array[col]):
beads[row][col] = 1
yield array[:], -1, col, row, -1 # Visual feedback when placing beads

# Simulate gravity
for row in range(max_val):
count = 0
for col in range(n):
if beads[row][col]:
count += 1
beads[row][col] = 0
for col in range(n - count, n):
beads[row][col] = 1

# Reconstruct a temporary array to show progress visually
temp_array = [sum(beads[i][j] for i in range(max_val)) for j in range(n)]
yield temp_array, -1, -1, row, -1 # Show current state during gravity fall

# Final update to actual array
for col in range(n):
array[col] = sum(beads[row][col] for row in range(max_val))
yield array[:], -1, col, -1, -1
1 change: 1 addition & 0 deletions src/algs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
'exchangeSort' : exchangeSort,
'treeSort' : treeSort,
'slowSort' : slowSort,
'beadSort' : beadSort,
}