-
-
Save mikezink/899090300e3968e3af38475af279a3de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insertion_sort(bucket): | |
for i in range (1, len (bucket)): | |
var = bucket[i] | |
j = i - 1 | |
while (j >= 0 and var < bucket[j]): | |
bucket[j + 1] = bucket[j] | |
j = j - 1 | |
bucket[j + 1] = var | |
def insertionSort(alist): | |
for index in range(1,len(alist)): | |
currentvalue = alist[index] | |
position = index | |
while position>0 and alist[position-1]>currentvalue: | |
alist[position]=alist[position-1] | |
position = position-1 | |
alist[position]=currentvalue | |
def bucket_sort(input_list): | |
# Find maximum value in the list and use length of the list | |
# to determine which value in the list goes into which bucket | |
max_value = max(input_list) | |
size = max_value / len(input_list) | |
# Create n empty buckets where n is equal to the length of the input list | |
buckets_list = [] | |
for x in range(len(input_list)): | |
buckets_list.append([]) | |
# Put list elements into different buckets based on the size | |
for i in range(len(input_list)): | |
j = int(input_list[i] / size) | |
if j != len(input_list): | |
buckets_list[j].append(input_list[i]) | |
else: | |
buckets_list[len(input_list) - 1].append(input_list[i]) | |
# Sort elements within the buckets using Insertion Sort | |
for z in range(len(input_list)): | |
insertionSort(buckets_list[z]) | |
# Concatenate buckets with sorted elements into a single list | |
final_output = [] | |
for x in range(len(input_list)): | |
final_output = final_output + buckets_list[x] | |
return final_output | |
input_list = [1.20, 0.22, 0.43, 0.36,0.39,0.27] | |
print('ORIGINAL LIST:') | |
print(input_list) | |
sorted_list = bucket_sort(input_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment