Professional Documents
Culture Documents
Mini Project
Mini Project
conquer algorithms. It works by dividing the unsorted list into mul�ple smaller lists,
sor�ng those smaller lists, and then merging them back together to produce a single
sorted list.
Here's how Merge Sort works:
• Divide: The unsorted list is divided into two halves recursively un�l each sub-
list contains only one element, which is, by defini�on, sorted.
• Conquer: The sor�ng phase involves merging two sorted sub-lists into a
single sorted list. This is done by comparing the elements from the two sub-
lists and combining them into a new list in sorted order.
• Merge: The process of merging con�nues recursively un�l you have a single
sorted list containing all the elements from the original unsorted list.
Merge Sort is known for its stability (the rela�ve order of equal elements is
preserved), predictable �me complexity (O(n log n)), and it's not dependent on the
ini�al order of elements, making it suitable for sor�ng a wide range of data types.
Code for Merge Sort:
def merge_sort(arr): result.append(le�[i])
if len(arr) <= 1: i += 1
le� = arr[:mid] j += 1
if result_queue is None:
result_queue = Queue()
if len(arr) <= 1:
result_queue.put(arr)
return
result_queue.put(merge_sort(arr))
return
mid = len(arr) // 2
le� = arr[:mid]
right = arr[mid:]
le�_thread.start()
right_thread.start()
le�_thread.join()
right_thread.join()
le� = result_queue.get()
right = result_queue.get()
result_queue.put(merge(le�, right))
start_�me = �me.�me()
result_queue = Queue()
parallel_merge_sort(arr, result_queue=result_queue)
sorted_arr = result_queue.get()
end_�me = �me.�me()