Merge Sort

Kulani Baloyi / May 21, 2024

3 min read

A champion in this domain, known for its ability to conquer large datasets with remarkable speed.


The Divide-and-Conquer Strategy

Imagine a tangled mess of cables. Merge Sort breaks down the problem by:

Dividing the list into halves (or sublists) recursively. This continues until you reach single-element sublists, which are already sorted by default. Merging the sorted sublists back together in the correct order. Merging involves comparing elements from each sublist and placing the smaller element into the final sorted list. This process continues until all sublists are merged, leading to a completely sorted list.

Conquering Chaos: The Merge Function

The core of Merge Sort lies in the merge function:

Take two sorted sublists. Compare the first elements of each sublist. Place the smaller element into the final sorted list. Repeat step 3, removing the used element from the sublist it belonged to. Once one sublist is empty, append the remaining elements from the other sublist to the final sorted list.

def merge(left, right):
 
  merged = []
  i, j = 0, 0  # Indices for left and right sublists
 
  # Compare elements and append the smaller one to the merged list
  while i < len(left) and j < len(right):
    if left[i] <= right[j]:
      merged.append(left[i])
      i += 1
    else:
      merged.append(right[j])
      j += 1
 
  # Append remaining elements (if any)
  merged += left[i:]
  merged += right[j:]
 
  return merged
 
def merge_sort(data):
  """
  Sorts a list of data in ascending order using Merge Sort algorithm.
 
  Args:
      data: A list of sortable elements.
 
  Returns:
      A new list containing the sorted data.
  """
  if len(data) <= 1:
    return data  # Base case: single-element list is already sorted
 
  # Divide the list into halves
  mid = len(data) // 2
  left = merge_sort(data[:mid])
  right = merge_sort(data[mid:])
 
  # Merge the sorted sublists
  return merge(left, right)
 
# Example usage
unsorted_data = [8, 3, 1, 4, 2]
sorted_data = merge_sort(unsorted_data.copy())  # Avoid modifying original list
print(f"Unsorted data: {unsorted_data}")
print(f"Sorted data: {sorted_data}")
 

This code implements both the merge function and the merge_sort function. The merge_sort function recursively divides the list, calls merge to combine sorted sublists, and returns the final sorted list.

Strengths and Weaknesses of Merge Sort:

Strengths: Highly efficient for large datasets (logarithmic time complexity), stable sorting algorithm (preserves order of equal elements). Weaknesses: More complex to implement than Insertion Sort, requires additional memory for temporary sublists. Merge Sort's divide-and-conquer approach makes it a powerful tool for handling massive datasets with optimal efficiency.

Related articles:

Dijktras Algorithm
Jul 15, 2024
Depth First Sort
Jul 12, 2024
Orders of growth
Jul 3, 2024
Bubble Sort
May 22, 2024
Binary Search
May 21, 2024