@ -0,0 +1,27 @@ | |||||
def merge_sort(a): | |||||
if len(a) < 2: | |||||
return a | |||||
l = a[0:len(a)//2] | |||||
r = a[len(a)//2:] | |||||
return merge(merge_sort(l), merge_sort(r)) | |||||
def merge(a, b): | |||||
out = [] | |||||
i = 0 | |||||
j = 0 | |||||
while i < len(a) or j < len(b): | |||||
if i >= len(a): | |||||
out.append(b[j]) | |||||
j += 1 | |||||
elif j >= len(b): | |||||
out.append(a[i]) | |||||
i += 1 | |||||
else: | |||||
if a[i] <= b[j]: | |||||
out.append(a[i]) | |||||
i += 1 | |||||
else: | |||||
out.append(b[j]) | |||||
j += 1 | |||||
return out |
@ -0,0 +1,14 @@ | |||||
def radix_sort(lista): | |||||
max_len = max([len(numero) for numero in lista]) | |||||
padded = list([str(num).rjust(max_len, "0") for num in lista]) | |||||
for pos in reversed(range(max_len)): | |||||
buckets = [list() for x in range(0, 10)] | |||||
for num in padded: | |||||
bucket = int(num[pos]) | |||||
buckets[bucket] += [num] | |||||
padded = sum(buckets, []) | |||||
return padded | |||||
if __name__ == "__main__": | |||||
print(radix_sort(["13", "105", "10", "150"])) |