forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket-sorting.py
More file actions
52 lines (42 loc) · 1.55 KB
/
bucket-sorting.py
File metadata and controls
52 lines (42 loc) · 1.55 KB
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
45
46
47
48
49
50
51
52
# bucket sorting mechanism
data = [5, 3, 1, 2, 4, 6, 7, 8, 9, 10]
def bucket_sorting(arr):
# get the maximum value from the array
max_value = max(arr)
# create a bucket with the size of max_value + 1
bucket = [0] * (max_value + 1)
# loop through the array
for i in arr:
# increment the value of the bucket at the index of the current value of the array
bucket[i] += 1
# create a new array
new_arr = []
# loop through the bucket
for i in range(len(bucket)):
# loop through the value of the bucket at the current index
for j in range(bucket[i]):
# append the index to the new array
new_arr.append(i)
# return the new array
return new_arr
print(bucket_sorting(data))
# bucket sorting algorithm for float numbers
def bucket_sorting_float(arr):
# create a new array
new_arr = []
# loop through the array
for i in range(len(arr)):
# append the value of the array at the current index to the new array
new_arr.append(arr[i])
# loop through the new array
for i in range(len(new_arr)):
# loop through the new array
for j in range(len(new_arr)):
# if the value of the new array at the current index is greater than the value of the new array at the
# next index
if new_arr[i] < new_arr[j]:
# swap the values
new_arr[i], new_arr[j] = new_arr[j], new_arr[i]
# return the new array
return new_arr
print(bucket_sorting_float([0.1, 0.2, 0.3, 0.69, 0.5, 0.34]))