summaryrefslogtreecommitdiff
path: root/comp/work/30
diff options
context:
space:
mode:
Diffstat (limited to 'comp/work/30')
-rw-r--r--comp/work/30/bubble.py20
-rw-r--r--comp/work/30/merge.py38
2 files changed, 58 insertions, 0 deletions
diff --git a/comp/work/30/bubble.py b/comp/work/30/bubble.py
new file mode 100644
index 0000000..e152c7d
--- /dev/null
+++ b/comp/work/30/bubble.py
@@ -0,0 +1,20 @@
+import sys
+
+def bubble(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
+
+
+count = int(sys.argv[1])
+
+num = []
+for i in range(count):
+ num.append(int(input(f"number {i}: ")))
+
+print(bubble(num))
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]))