diff options
author | standenboy <standenboy@seacrossedlovers.xyz> | 2024-04-30 14:22:45 +0100 |
---|---|---|
committer | standenboy <standenboy@seacrossedlovers.xyz> | 2024-04-30 14:22:45 +0100 |
commit | c460a26850eca5ded047d0eb0d183b861087aa53 (patch) | |
tree | 000c4a117746de71870fbee820809f7df49bcfcf /comp/work/31/merge.py | |
parent | 2e239a4ed2265f7c7dc2aabedfdb7d7b011704cb (diff) |
write up update
Diffstat (limited to 'comp/work/31/merge.py')
-rw-r--r-- | comp/work/31/merge.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/comp/work/31/merge.py b/comp/work/31/merge.py new file mode 100644 index 0000000..0947aa4 --- /dev/null +++ b/comp/work/31/merge.py @@ -0,0 +1,39 @@ +def sort(list): + length = len(list) + l1 = [] + l2 = [] + for i in range(length): + if i >= (length / 2): + l2.append(list[i]) + else: + l1.append(list[i]) + if length > 2: + l1 = sort(l1) + l2 = sort(l2) + return merge(l1, l2) + + +def merge(l1, l2): + i = 0 + j = 0 + output = [] + while i < len(l1) and j < len(l2): + if l1[i] < l2[j]: + output.append(l1[i]) + i += 1 + else: + output.append(l2[j]) + j += 1 + + while i < len(l1): + output.append(l1[i]) + i += 1 + while j < len(l2): + output.append(l2[j]) + j += 1 + return output + + +if __name__ == "__main__": + # call split on the list, will return a sorted one + print(sort([1, 4, 2, 4, 2, 5, 6, 2, 7])) |