What do you understand by Merge sort? Write the algorithm to sort using merge sort. Take an example to explain your answer.


Q.) What do you understand by Merge sort? Write the algorithm to sort using merge sort. Take an example to explain your answer.

Subject: Data Structures

I. Introduction

Merge Sort is a popular sorting algorithm that follows the divide and conquer paradigm. It was invented by John von Neumann in 1945. The main idea behind the algorithm is to divide the unsorted list into n sublists, each containing one element (a list of one element is considered sorted), and then repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining. This will be the sorted list. The time complexity of Merge Sort is O(n log n) in all three cases (worst, average, and best) as merge sort always divides the array into two halves and takes linear time to merge two halves.

II. Detailed Explanation of Merge Sort

Merge Sort employs a divide and conquer strategy. It works as follows:

  1. Divide: The unsorted list is divided into two halves. This is done using the mid-point of the list. If the list has an odd number of elements, the extra element is put in the first half. This division continues until we get sub-arrays of size 1.

  2. Conquer: The sub-arrays are then merged back in a way that they come back sorted. The merging happens in a manner that the elements are sorted.

III. Merge Sort Algorithm

The algorithm for Merge Sort is as follows:

  1. Base Case: If the length of the array is 1, the array is already sorted, so return the array.

  2. Divide Step: Find the middle point to divide the array into two halves.

  3. Conquer Step: Call merge sort for the first half and then for the second half.

  4. Merge Step: The last step is to merge the two halves sorted in step 3.

Here is the pseudocode for the Merge Sort algorithm:

function mergeSort(array)
    if length(array) > 1
        mid = length(array) / 2
        leftHalf = elements of array from start to mid
        rightHalf = elements of array from mid to end

        mergeSort(leftHalf)
        mergeSort(rightHalf)

        merge(leftHalf, rightHalf, array)

function merge(leftHalf, rightHalf, array)
    i = j = k = 0

    while i < length(leftHalf) and j < length(rightHalf)
        if leftHalf[i] < rightHalf[j]
            array[k] = leftHalf[i]
            i++
        else
            array[k] = rightHalf[j]
            j++
        k++

    while i < length(leftHalf)
        array[k] = leftHalf[i]
        i++
        k++

    while j < length(rightHalf)
        array[k] = rightHalf[j]
        j++
        k++

IV. Example

Let's take an example to understand Merge Sort. Consider the array [38, 27, 43, 3, 9, 82, 10].

  1. The array is first divided into two halves [38, 27, 43, 3] and [9, 82, 10].

  2. These halves are further divided until we get sub-arrays of size 1.

  3. The sub-arrays are then merged back in sorted order to give [27, 38, 3, 43] and [9, 10, 82].

  4. Finally, these arrays are merged to give the sorted array [3, 9, 10, 27, 38, 43, 82].

V. Program to Implement Merge Sort

Here is a Python program to implement Merge Sort:

def mergeSort(arr):
    if len(arr) > 1:
        mid = len(arr)//2
        L = arr[:mid]
        R = arr[mid:]

        mergeSort(L)
        mergeSort(R)

        i = j = k = 0

        while i < len(L) and j < len(R):
            if L[i] < R[j]:
                arr[k] = L[i]
                i += 1
            else:
                arr[k] = R[j]
                j += 1
            k += 1

        while i < len(L):
            arr[k] = L[i]
            i += 1
            k += 1

        while j < len(R):
            arr[k] = R[j]
            j += 1
            k += 1

arr = [38, 27, 43, 3, 9, 82, 10]
mergeSort(arr)
print("Sorted array is:", arr)

When you run this program, it will print: Sorted array is: [3, 9, 10, 27, 38, 43, 82]

VI. Conclusion

Merge Sort is a powerful sorting algorithm that uses the divide and conquer strategy. It is efficient and works faster than the simple comparison-based algorithms like Bubble Sort or Insertion Sort. It is widely used in various applications, including large data set sorting and external sorting. However, it requires equal amount of additional space as the unsorted array. Hence, it is not as space-efficient as the in-place sorting algorithms like Quick Sort.

Diagram: Not necessary for this question.

Summary

Merge Sort is a popular sorting algorithm that follows the divide and conquer paradigm. It divides the unsorted list into n sublists, each containing one element, and then repeatedly merges sublists to produce new sorted sublists until there is only one sublist remaining. The time complexity of Merge Sort is O(n log n) in all three cases (worst, average, and best).

Analogy

Merge Sort is like organizing a messy bookshelf. You divide the books into smaller groups, sort each group individually, and then merge the sorted groups back together to create a fully sorted bookshelf.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is the time complexity of Merge Sort?
  • O(n)
  • O(n^2)
  • O(log n)
  • O(n log n)