Tutorialspoint
Problem
Solution
Submissions

Implement Merge Sort Algorithm

Certification: Intermediate Level Accuracy: 100% Submissions: 1 Points: 15

Write a Python program that implements the Merge Sort algorithm to sort a list of integers in ascending order. Merge Sort is a divide-and-conquer algorithm that divides the input array into two halves, recursively sorts them, and then merges the sorted halves.

Example 1
  • Input: [38, 27, 43, 3, 9, 82, 10]
  • Output: [3, 9, 10, 27, 38, 43, 82]
  • Explanation:
    • Step 1: Take the input array [38, 27, 43, 3, 9, 82, 10].
    • Step 2: Divide the array into two halves: [38, 27, 43, 3] and [9, 82, 10].
    • Step 3: Further divide and recursively sort each half.
    • Step 4: Merge the sorted halves back together, comparing elements.
    • Step 5: Continue merging until the entire array is sorted.
    • Step 6: Return the sorted array [3, 9, 10, 27, 38, 43, 82].
Example 2
  • Input: [5, 2, 4, 7, 1, 3, 2, 6]
  • Output: [1, 2, 2, 3, 4, 5, 6, 7]
  • Explanation:
    • Step 1: Take the input array [5, 2, 4, 7, 1, 3, 2, 6].
    • Step 2: Divide the array into two halves: [5, 2, 4, 7] and [1, 3, 2, 6].
    • Step 3: Further divide and recursively sort each half.
    • Step 4: Merge the sorted halves back together, comparing elements.
    • Step 5: Continue merging until the entire array is sorted.
    • Step 6: Return the sorted array [1, 2, 2, 3, 4, 5, 6, 7].
Constraints
  • 1 ≤ len(arr) ≤ 10^5
  • -10^6 ≤ arr[i] ≤ 10^6
  • Time Complexity: O(n log n), where n is the length of the array
  • Space Complexity: O(n)
ArraysNumberRecursionEYAdobe
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Divide the array into two halves recursively until each subarray has only one element
  • Merge the subarrays by comparing elements from both subarrays
  • Use a temporary array to store the merged results
  • Implement a merge function to combine two sorted subarrays
  • The base case of the recursion is when the subarray has one or zero elements
  • The merge function should compare elements from both subarrays and place them in sorted order
  • The recursion depth is log(n), where n is the length of the array

Steps to solve by this approach:

 Step 1: Create a copy of the input array to avoid modifying the original.
 Step 2: Define the base case: if array has 0 or 1 element, it's already sorted.
 Step 3: Divide the array into two halves at the midpoint.
 Step 4: Recursively sort the left half and right half.
 Step 5: Define a merge function to combine two sorted subarrays.
 Step 6: Merge the sorted halves by comparing elements and adding them in order.
 Step 7: Return the final merged and sorted array.

Submitted Code :