Browse Source

Merge pull request #26 from sourabhsahu123/master

bucket sort added
pull/33/head
Jeffery Russell 5 years ago
committed by GitHub
parent
commit
8edb7ae282
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 0 deletions
  1. +35
    -0
      sorting/bucketSort.py

+ 35
- 0
sorting/bucketSort.py View File

@ -0,0 +1,35 @@
def bucket_sort(alist):
largest = max(alist)
length = len(alist)
size = largest/length
buckets = [[] for _ in range(length)]
for i in range(length):
j = int(alist[i]/size)
if j != length:
buckets[j].append(alist[i])
else:
buckets[length - 1].append(alist[i])
for i in range(length):
insertion_sort(buckets[i])
result = []
for i in range(length):
result = result + buckets[i]
return result
def insertion_sort(alist):
for i in range(1, len(alist)):
temp = alist[i]
j = i - 1
while (j >= 0 and temp < alist[j]):
alist[j + 1] = alist[j]
j = j - 1
alist[j + 1] = temp
alist = [54,26,93,17,77,31,44,55,20]
print(bucket_sort(alist))

Loading…
Cancel
Save