Skip to content

Commit c3b2209

Browse files
authored
Added Bucket Sorting Algorithm
1 parent 1192952 commit c3b2209

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

allalgorithms/sorting/bucket_sort.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Bucket Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Dwij Sukeshkumar Sheth
7+
# Github: @dwij2812
8+
#
9+
10+
# Python3 program to sort an array
11+
# using bucket sort
12+
def insertionSort(b):
13+
for i in range(1, len(b)):
14+
up = b[i]
15+
j = i - 1
16+
while j >=0 and b[j] > up:
17+
b[j + 1] = b[j]
18+
j -= 1
19+
b[j + 1] = up
20+
return b
21+
22+
def bucketSort(x):
23+
arr = []
24+
slot_num = 10 # 10 means 10 slots, each
25+
# slot's size is 0.1
26+
for i in range(slot_num):
27+
arr.append([])
28+
29+
# Put array elements in different buckets
30+
for j in x:
31+
index_b = int(slot_num * j)
32+
arr[index_b].append(j)
33+
34+
# Sort individual buckets
35+
for i in range(slot_num):
36+
arr[i] = insertionSort(arr[i])
37+
38+
# concatenate the result
39+
k = 0
40+
for i in range(slot_num):
41+
for j in range(len(arr[i])):
42+
x[k] = arr[i][j]
43+
k += 1
44+
return x
45+
46+
# For using this make the function call bucketSort(data) where data is the array of the numbers to be sorted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy