From c460a26850eca5ded047d0eb0d183b861087aa53 Mon Sep 17 00:00:00 2001 From: standenboy Date: Tue, 30 Apr 2024 14:22:45 +0100 Subject: write up update --- comp/work/31/__pycache__/bubble.cpython-312.pyc | Bin 0 -> 981 bytes comp/work/31/__pycache__/merge.cpython-312.pyc | Bin 0 -> 1641 bytes comp/work/31/bubble.py | 22 +++++++++++++ comp/work/31/menu.py | 20 ++++++++++++ comp/work/31/merge.py | 39 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 comp/work/31/__pycache__/bubble.cpython-312.pyc create mode 100644 comp/work/31/__pycache__/merge.cpython-312.pyc create mode 100644 comp/work/31/bubble.py create mode 100644 comp/work/31/menu.py create mode 100644 comp/work/31/merge.py (limited to 'comp/work/31') diff --git a/comp/work/31/__pycache__/bubble.cpython-312.pyc b/comp/work/31/__pycache__/bubble.cpython-312.pyc new file mode 100644 index 0000000..a91900d Binary files /dev/null and b/comp/work/31/__pycache__/bubble.cpython-312.pyc differ diff --git a/comp/work/31/__pycache__/merge.cpython-312.pyc b/comp/work/31/__pycache__/merge.cpython-312.pyc new file mode 100644 index 0000000..f390001 Binary files /dev/null and b/comp/work/31/__pycache__/merge.cpython-312.pyc differ diff --git a/comp/work/31/bubble.py b/comp/work/31/bubble.py new file mode 100644 index 0000000..c45f903 --- /dev/null +++ b/comp/work/31/bubble.py @@ -0,0 +1,22 @@ +import sys + + +def sort(list): + length = len(list) + for i in range(length - 1): + for j in range(length - i - 1): + if list[j] > list[j+1]: + tmp = list[j] + list[j] = list[j+1] + list[j+1] = tmp + return list + + +if __name__ == "__main__": + count = int(sys.argv[1]) + + num = [] + for i in range(count): + num.append(int(input(f"number {i}: "))) + + print(sort(num)) diff --git a/comp/work/31/menu.py b/comp/work/31/menu.py new file mode 100644 index 0000000..8f3c009 --- /dev/null +++ b/comp/work/31/menu.py @@ -0,0 +1,20 @@ +import merge +import bubble + + +while True: + list = [] + inp = "" + print("enter nums, then enter END") + while inp != "END": + inp = input("number: ") + if inp != "END": + list.append(inp) + + sort = int(input("1) bubble, 2) merge: ")) + + if sort == 1: + print(*merge.sort(list), sep=", ") + + else: + print(*bubble.sort(list), sep=", ") 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])) -- cgit v1.2.3