forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bead_Sort.py
30 lines (25 loc) · 911 Bytes
/
Bead_Sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#Bead sort (a.k.a Abacus sort, or Gravity sort) is a sorting algorithm
# that can sort a list of positive integers .
# We can sort negative numbers too by adding a constant to all the integers and subracting it later
#Bead sort is a natural algorithm
#It uses (imitates) gravity to sort an input list
def bead_sort(a):
minimum, maximum = min(a), max(a)
n = len(a)
# Initialize a temporary array filled with minimum
temp = [minimum] * n
for i in range(maximum-1, minimum-1, -1):
k = 0
for j in range(n):
if a[j] > i:
temp[k] += 1
k += 1
# Copy temp array back into original array
# replacing the array into sorted order
# temp array is reverse sorted, so copy backwards for ascending order
for i in range(n):
a[i] = temp[n-i-1]
return a
if __name__ == "__main__":
a=[]
print(bead_sort(a))