import time
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
# Taking input from the user and converting it into a list of integers
input_str = input("Enter numbers separated by space: ")
input_list = [int(x) for x in input_str.split()]
# Measure time for Merge Sort
merge_sort_input = input_list.copy()
start_time = time.time()
merge_sort(merge_sort_input)
merge_sort_time = time.time() - start_time
print("Sorted array using Merge Sort:", merge_sort_input)
print(f"Time taken for Merge Sort: {merge_sort_time:.6f} seconds")
# Measure time for Quick Sort
start_time = time.time()
quick_sort_result = quick_sort(input_list)
quick_sort_time = time.time() - start_time
print("Sorted array using Quick Sort:", quick_sort_result)
print(f"Time taken for Quick Sort: {quick_sort_time:.6f} seconds")