diff options
author | standenboy <standenboy@seacrossedlovers.xyz> | 2024-04-19 10:31:01 +0100 |
---|---|---|
committer | standenboy <standenboy@seacrossedlovers.xyz> | 2024-04-19 10:31:01 +0100 |
commit | a241ad8e874a2220d81254c2ebfbe69d0470fd9b (patch) | |
tree | d8ac8e8cfdedd03554e0cb92452784e8af8f3bee /comp/work/30/merge.py | |
parent | c696b4d6ded69074cd08c51b91eabeaeac5823ac (diff) |
added a few bits
Diffstat (limited to 'comp/work/30/merge.py')
-rw-r--r-- | comp/work/30/merge.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/comp/work/30/merge.py b/comp/work/30/merge.py new file mode 100644 index 0000000..4e9b12e --- /dev/null +++ b/comp/work/30/merge.py @@ -0,0 +1,38 @@ +def split(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 = split(l1) + l2 = split(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 + + +# call split on the list, will return a sorted one +print(split([1, 4, 2, 4, 2, 5, 6, 2, 7])) |