From e2ff29c04b64410f04f8bfebb94b5aedd2be7ed6 Mon Sep 17 00:00:00 2001 From: Adriano Laureano Date: Fri, 12 Oct 2018 21:37:23 -0300 Subject: [PATCH 1/2] Create radix_sort.py --- sorting/radix_sort.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 sorting/radix_sort.py diff --git a/sorting/radix_sort.py b/sorting/radix_sort.py new file mode 100644 index 0000000..8f3c4fa --- /dev/null +++ b/sorting/radix_sort.py @@ -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"])) From 639d7d30b67446599a06bb3e25070c0f63b9c972 Mon Sep 17 00:00:00 2001 From: Adriano Laureano Date: Fri, 12 Oct 2018 21:37:53 -0300 Subject: [PATCH 2/2] Create merge_sort.py --- sorting/merge_sort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sorting/merge_sort.py diff --git a/sorting/merge_sort.py b/sorting/merge_sort.py new file mode 100644 index 0000000..20beb82 --- /dev/null +++ b/sorting/merge_sort.py @@ -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