summaryrefslogtreecommitdiff
path: root/comp/work/31
diff options
context:
space:
mode:
authorstandenboy <standenboy@seacrossedlovers.xyz>2024-04-30 14:22:45 +0100
committerstandenboy <standenboy@seacrossedlovers.xyz>2024-04-30 14:22:45 +0100
commitc460a26850eca5ded047d0eb0d183b861087aa53 (patch)
tree000c4a117746de71870fbee820809f7df49bcfcf /comp/work/31
parent2e239a4ed2265f7c7dc2aabedfdb7d7b011704cb (diff)
write up update
Diffstat (limited to 'comp/work/31')
-rw-r--r--comp/work/31/__pycache__/bubble.cpython-312.pycbin0 -> 981 bytes
-rw-r--r--comp/work/31/__pycache__/merge.cpython-312.pycbin0 -> 1641 bytes
-rw-r--r--comp/work/31/bubble.py22
-rw-r--r--comp/work/31/menu.py20
-rw-r--r--comp/work/31/merge.py39
5 files changed, 81 insertions, 0 deletions
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
--- /dev/null
+++ b/comp/work/31/__pycache__/bubble.cpython-312.pyc
Binary files 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
--- /dev/null
+++ b/comp/work/31/__pycache__/merge.cpython-312.pyc
Binary files 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]))