-
Notifications
You must be signed in to change notification settings - Fork 364
/
Bead_sort.py
44 lines (35 loc) · 1011 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
def display(arr):
for num in arr:
print(num, end=" ")
print()
def bead_sort(arr):
max_val = max(arr)
len_arr = len(arr)
beads = np.zeros((len_arr, max_val), dtype=int)
for i in range(len_arr):
for j in range(arr[i]):
beads[i][j] = 1
for j in range(max_val):
sum_val = 0
for i in range(len_arr):
sum_val += beads[i][j]
beads[i][j] = 0
for i in range(len_arr - sum_val, len_arr):
beads[i][j] = 1
for i in range(len_arr):
j = 0
while j < max_val and beads[i][j] != 0:
j += 1
arr[i] = j
if __name__ == '__main__':
n = int(input("Enter the size of the array: "))
arr = []
elements = input("Enter the elements of the array: ").split()
for i in range(n):
arr.append(int(elements[i]))
print("Original array:", end=" ")
display(arr)
bead_sort(arr)
print("Sorted array:", end=" ")
display(arr)