0
2.1kviews
Explain merge sort algorithm. Sort the following numbers with merge sort give output of each path

Subject: Analysis Of Algorithm

Topic: Divide Aqnd Conquer

Difficulty: Medium

Explain merge sort algorithm. Sort the following numbers with merge sort give output of each path

84, 25, 36, 15, 48, 09, 17, 55, 92 , 36

1 Answer
0
34views

Merge Sort :-

  1. Merge sort is one of the most efficient sorting algorithms.

  2. Merge sort is a sorting technique based on divide and conquer technique.

  3. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.

  4. Merge sort first divides the array into equal halves and then combines them in a sorted manner.



How Merge Sort Works ?


Let the example be {84, 25, 36, 15, 48, 09, 17, 55, 92 , 36}.

We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 10 items is divided into two arrays of size 5.

                    {84, 25, 36, 15, 48}     {09, 17, 55, 92 , 36}

                  {84, 25}  {36, 15} {48}     {09, 17} {55, 92} {36}

                 {84} {25} {36} {15} {48}     {9} {17} {55} {92} {36}

                 {25, 84} {15, 36} {48}        {9, 17} {55, 92} {36}

                    {15, 25, 36, 48, 84}          {9, 17, 36, 55, 92}

                         {9, 15, 17, 25, 36, 36, 48, 55, 84, 92}

Here, we are dividing again it into two equal parts and then will sort it one by one and then will combine it.



Algorithm of Merge Sort : -

Step 1 − if it is only one element in the list it is already sorted, return.

Step 2 − divide the list recursively into two halves until it can no more be divided.

Step 3 − merge the smaller lists into new list in sorted order.




Pseudocode : -

procedure mergesort( var a as array )
   if ( n == 1 ) return a

   var l1 as array = a[0] ... a[n/2]
   var l2 as array = a[n/2+1] ... a[n]

   l1 = mergesort( l1 )
   l2 = mergesort( l2 )

   return merge( l1, l2 )
end procedure

procedure merge( var a as array, var b as array )

   var c as array
   while ( a and b have elements )
      if ( a[0] > b[0] )
         add b[0] to the end of c
         remove b[0] from b
      else
         add a[0] to the end of c
         remove a[0] from a
      end if
   end while

   while ( a has elements )
      add a[0] to the end of c
      remove a[0] from a
   end while

   while ( b has elements )
      add b[0] to the end of c
      remove b[0] from b
   end while

   return c

end procedure




Program Implementation of Merge Sort (with Recursion) :-

void merge(int *Arr, int start, int mid, int end) {
    // create a temp array
    int temp[end - start + 1];

    // crawlers for both intervals and for temp
    int i = start, j = mid+1, k = 0;

    // traverse both arrays and in each iteration add smaller of both elements in temp 
    while(i <= mid && j <= end) {
        if(Arr[i] <= Arr[j]) {
            temp[k] = Arr[i];
            k += 1; i += 1;
        }
        else {
            temp[k] = Arr[j];
            k += 1; j += 1;
        }
    }

    // add elements left in the first interval 
    while(i <= mid) {
        temp[k] = Arr[i];
        k += 1; i += 1;
    }

    // add elements left in the second interval 
    while(j <= end) {
        temp[k] = Arr[j];
        k += 1; j += 1;
    }

    // copy temp to original interval
    for(i = start; i <= end; i += 1) {
        Arr[i] = temp[i - start]
    }
}

// Arr is an array of integer type
// start and end are the starting and ending index of current interval of Arr

void mergeSort(int *Arr, int start, int end) {

    if(start < end) {
        int mid = (start + end) / 2;
        mergeSort(Arr, start, mid);
        mergeSort(Arr, mid+1, end);
        merge(Arr, start, mid, end);
    }
}




Program Implementation of Merge Sort (Iteratively) :-

#include <iostream>
using namespace std;

// Merge two subarrays L and M into arr
void merge(int arr[], int p, int q, int r) {

  // Create L ← A[p..q] and M ← A[q+1..r]
  int n1 = q - p + 1;
  int n2 = r - q;

  int L[n1], M[n2];

  for (int i = 0; i < n1; i++)
    L[i] = arr[p + i];
  for (int j = 0; j < n2; j++)
    M[j] = arr[q + 1 + j];

  // Maintain current index of sub-arrays and main array
  int i, j, k;
  i = 0;
  j = 0;
  k = p;

  // Until we reach either end of either L or M, pick larger among
  // elements L and M and place them in the correct position at A[p..r]
  while (i < n1 && j < n2) {
    if (L[i] <= M[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = M[j];
      j++;
    }
    k++;
  }

  // When we run out of elements in either L or M,
  // pick up the remaining elements and put in A[p..r]
  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
  }

  while (j < n2) {
    arr[k] = M[j];
    j++;
    k++;
  }
}

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
  if (l < r) {
    // m is the point where the array is divided into two subarrays
    int m = l + (r - l) / 2;

    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);

    // Merge the sorted subarrays
    merge(arr, l, m, r);
  }
}

// Print the array
void printArray(int arr[], int size) {
  for (int i = 0; i < size; i++)
    cout << arr[i] << " ";
  cout << endl;
}

// Driver program
int main() {
  int arr[] = {6, 5, 12, 10, 9, 1};
  int size = sizeof(arr) / sizeof(arr[0]);

  mergeSort(arr, 0, size - 1);

  cout << "Sorted array: \n";
  printArray(arr, size);
  return 0;
}
Please log in to add an answer.