Bucket sort: diferenças entre revisões

Conteúdo apagado Conteúdo adicionado
DannyS712 (discussão | contribs)
m <source> -> <syntaxhighlight> (phab:T237267)
Code in Python
Linha 262:
}
</syntaxhighlight>
 
== Exemplo em Python ==
<syntaxhighlight lang="python" line="1">
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)):
insertion_sort(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
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 main():
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)
print('SORTED LIST:')
print(sorted_list)
</syntaxhighlight><ref>{{Citar web |url=https://stackabuse.com/bucket-sort-in-python/ |titulo=Bucket Sort in Python |data=2020-06-26 |acessodata=2021-09-24 |website=Stack Abuse |lingua=en}}</ref>
 
== Ver também ==