Skip to content

Commit

Permalink
solved: practice #2 merge_sort algorithm -@iamserda
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Feb 23, 2025
1 parent 5a72783 commit b9a3e2c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import math, random


def merge_sort(arr):
"""sorts an array, in ascending order"""
if not arr:
print(ValueError("List is empty. Cannot sort an empty list."))
return []

if len(arr) == 1:
return arr
else:
mid = int(math.ceil(len(arr) / 2))
arr1 = merge_sort(arr[:mid])
arr2 = merge_sort(arr[mid:])
arr3 = merge_sorted_arrays(arr1, arr2)
return arr3


def merge_sorted_arrays(sorted_arr1, sorted_arr2):
sorted_arr3 = []
i = 0
j = 0
while True:
if i >= len(sorted_arr1) and j >= len(sorted_arr2):
return sorted_arr3

if i < len(sorted_arr1) and j >= len(sorted_arr2):
sorted_arr3.append(sorted_arr1[i])
i += 1
continue

if j < len(sorted_arr2) and i >= len(sorted_arr1):
sorted_arr3.append(sorted_arr2[j])
j += 1
continue

if sorted_arr1[i] <= sorted_arr2[j]:
sorted_arr3.append(sorted_arr1[i])
i += 1
continue

if sorted_arr1[i] > sorted_arr2[j]:
sorted_arr3.append(sorted_arr2[j])
j += 1
continue
break
return sorted_arr3


# Testing Arenas:

arr = [random.randint(-10, 10) for i in range(10)]
print("before:", arr)
sorted_arr = merge_sort(arr)
sorted_arr_prime = sorted(arr)
assert sorted_arr == sorted_arr_prime
print("after:", sorted_arr)

0 comments on commit b9a3e2c

Please sign in to comment.