initial commit
This commit is contained in:
commit
39f56a3b10
83 files changed
+3583
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def merge_sort(arr: list[int]) -> list[int]:
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
|
||||
mid = len(arr) // 2
|
||||
left_half = arr[:mid]
|
||||
right_half = arr[mid:]
|
||||
|
||||
left_half = merge_sort(left_half)
|
||||
right_half = merge_sort(right_half)
|
||||
|
||||
return merge(left_half, right_half)
|
||||
|
||||
def merge(left: list[int], right: list[int], merged=None) -> list[int]:
|
||||
if merged is None:
|
||||
merged = []
|
||||
l_index = 0
|
||||
r_index = 0
|
||||
|
||||
while l_index < len(left) and r_index < len(right):
|
||||
if left[l_index] <= right[r_index]:
|
||||
merged.append(left[l_index])
|
||||
l_index += 1
|
||||
else:
|
||||
merged.append(right[r_index])
|
||||
r_index += 1
|
||||
|
||||
merged += left[l_index:]
|
||||
merged += right[r_index:]
|
||||
return merged
|
||||
|
||||
NUM_ELEMENTS = 1_000_000
|
||||
l = [random.randint(0, 2*NUM_ELEMENTS) for _ in range(NUM_ELEMENTS)]
|
||||
|
||||
start = time.time()
|
||||
print(merge_sort(l))
|
||||
end = time.time()
|
||||
print(f"time elapsed: {end - start} sec")
|
||||
@@ -0,0 +1,10 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
NUM_ELEMENTS = 1_000_000
|
||||
l = [random.randint(0, 2*NUM_ELEMENTS) for _ in range(NUM_ELEMENTS)]
|
||||
|
||||
start = time.time()
|
||||
sorted(l)
|
||||
end = time.time()
|
||||
print(end - start)
|
||||
Reference in new issue
Block a user