Merge Sort
Kalyanasundar Arunachalam
UX UI Designer | Front End Developer | Figma | ReactJS
Sorting is a fundamental concept in computer science. Whether you're working on a large dataset or a small array, selecting the right algorithm can impact performance. One of the most efficient and widely used sorting algorithms is Merge Sort. In this article, we’ll break down what merge sort is, how it works, and how to implement it in Java.
What is Merge Sort?
Merge Sort is a divide and conquer algorithm that breaks a problem into smaller subproblems, solves them independently, and then merges the results to produce the final sorted array.
Here’s how it works:
- Divide: The array is recursively split into two halves until each sub-array contains a single element.
- Conquer: The sub-arrays are then sorted recursively using the same algorithm.
- Merge: The sorted sub-arrays are merged back together to form the final sorted array.
Why Use Merge Sort?
- Efficiency: Merge sort runs in O(n log n) time, which is optimal for comparison-based sorting algorithms.
- Stability: Merge sort preserves the relative order of elements with equal keys, making it a stable sorting algorithm.
- Predictable Performance: Unlike quicksort, merge sort has a guaranteed worst-case time complexity of O(n log n).
Merge Sort Visualization
Imagine we want to sort the array [38, 27, 43, 3, 9, 82, 10]:
- Divide Step:
- Split into two halves: [38, 27, 43, 3] and [9, 82, 10].
- Split again: [38, 27], [43, 3], [9], [82, 10].
- Continue until we have single elements: [38], [27], [43], [3], [9], [82], [10].
- Merge Step:
- Start merging: [38, 27] becomes [27, 38], [43, 3] becomes [3, 43], and so on.
- Continue merging pairs until the entire array is sorted: [3, 9, 10, 27, 38, 43, 82].
Java code example
Explanation of the Code
mergeSort Function:
- This is the recursive function that splits the array into two halves.
- It calls itself recursively until each sub-array has only one element.
merge Function:
- This function merges two sorted sub-arrays. It compares the elements of the two sub-arrays and merges them in sorted order.
- After merging, the resulting sub-array is sorted.
main Function:
- In the main method, we initialize an unsorted array.
- We call the mergeSort function, passing the array along with the left and right indices.
- The sorted array is then printed to the console.
Time Complexity of Merge Sort
- Best, Average, and Worst Case Time Complexity: O(n log n)
- Space Complexity: O(n)
Merge Sort in Real-World Applications
Merge Sort is highly suitable for scenarios where stability is important and the data set is large. Some of the common applications include:
- External Sorting: For large datasets that don’t fit into memory, merge sort can be used as it efficiently handles data by dividing it into smaller chunks.
- Inversion Count: Merge sort is useful in counting the number of inversions in an array (a common problem in algorithm challenges).
- Linked Lists: Merge sort is the preferred sorting algorithm for linked lists because it doesn’t require random access like quicksort.
Stay tuned! I am going to upload an article on recursion this Sunday ??