Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Merge Sort is a popular sor�ng algorithm that falls under the category of divide and

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

return arr else:

mid = len(arr) // 2 result.append(right[j])

le� = arr[:mid] j += 1

right = arr[mid:] result.extend(le�[i:])

le� = merge_sort(le�) result.extend(right[j:])

right = merge_sort(right) return result

return merge(le�, right) import random

def merge(le�, right): import �me

result = [] # Test standard merge sort

i=j=0 arr = [random.randint(1, 1000) for _ in


range(1000)]
while i < len(le�) and j < len(right):
start_�me = �me.�me()
if le�[i] < right[j]:
sorted_arr = merge_sort(arr) print(f"Time taken for standard merge sort:
{end_�me - start_�me} seconds")
end_�me = �me.�me()

Mul�threaded Merge Sort:


Mul�threaded Merge Sort, on the other hand, is a varia�on of the Merge Sort
algorithm that leverages mul�ple threads to speed up the sor�ng process on mul�-
core processors. The idea is to divide the work of sor�ng into smaller sub-tasks that
can be executed concurrently by separate threads. Each thread sorts a por�on of
the input data, and then the sorted sub-arrays are merged together, taking
advantage of parallel processing.
In the context of mul�threaded Merge Sort:
The original array is divided into smaller sub-arrays.
Each sub-array is assigned to a separate thread for sor�ng.
A�er sor�ng, the sorted sub-arrays are merged in parallel to produce the final
sorted array.
The primary advantage of mul�threaded Merge Sort is improved performance on
mul�-core CPUs, as it allows for beter u�liza�on of available CPU resources.
However, it also introduces some overhead related to thread management, so it
might not always be faster than standard Merge Sort, especially on smaller data
sets or systems with a single core. The effec�veness of mul�threaded Merge Sort
depends on factors like the size of the data, the number of available CPU cores, and
the efficiency of thread management.

Code for Mul�-threaded Merge Sort:


import threading

from queue import Queue

def parallel_merge_sort(arr, threads=4, result_queue=None):

if result_queue is None:

result_queue = Queue()
if len(arr) <= 1:

result_queue.put(arr)

return

if threads <= 1 or len(arr) <= 100:

result_queue.put(merge_sort(arr))

return

mid = len(arr) // 2

le� = arr[:mid]

right = arr[mid:]

le�_thread = threading.Thread(target=parallel_merge_sort, args=(le�, threads // 2, result_queue))

right_thread = threading.Thread(target=parallel_merge_sort, args=(right, threads // 2, result_queue))

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))

# Test mul�threaded merge sort

start_�me = �me.�me()

result_queue = Queue()

parallel_merge_sort(arr, result_queue=result_queue)

sorted_arr = result_queue.get()

end_�me = �me.�me()

print(f"Time taken for mul�threaded merge sort: {end_�me - start_�me} seconds")

You might also like