{"id":17630,"date":"2020-11-09T09:57:46","date_gmt":"2020-11-09T04:27:46","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/"},"modified":"2024-09-03T11:24:37","modified_gmt":"2024-09-03T05:54:37","slug":"merge-sort","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/","title":{"rendered":"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?"},"content":{"rendered":"\n<p>In this article, we will cover the below-mentioned topics. If you want to learn Sorting in detail, do check out our <a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/algorithms-in-c\" target=\"_blank\">Free Course on Sorting Algorithms in C for beginners<\/a>. In this course, you will understand Sorting Algorithms and their Analysis. You will start by understanding Bubble Sort, how it works, modified Bubble sort, and its implementation. Then you will understand Selection Sort, its implementation, and analysis. Then we will understand Insertion Sort followed by Quick Sort. Then we will conclude our course with Merge Sort, its demonstration, implementation, and analysis along with time and space complexity. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-merge-sort\"><strong>What is Merge sort<\/strong><\/h2>\n\n\n\n<p>Merge sort is one of the most efficient sorting techniques and it's based on the \u201cdivide and conquer\u201d paradigm.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In merge sort, the problem is divided into two subproblems in every iteration.<\/li>\n\n\n\n<li>Hence efficiency is increased drastically.<\/li>\n\n\n\n<li>It follows the divide and conquer approach\n<ul class=\"wp-block-list\">\n<li>Divide break the problem into 2 subproblem which continues until the problem set is left with one element only<\/li>\n\n\n\n<li>Conquer basically merges the 2 sorted arrays into the original array<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pseudocode-for-mergesort\"><strong>Pseudocode for MergeSort&nbsp;<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Declare left and right var which will mark the extreme indices of the array<\/li>\n\n\n\n<li>Left will be assigned to 0 and right will be assigned to n-1<\/li>\n\n\n\n<li>Find mid = (left+right)\/2<\/li>\n\n\n\n<li>Call mergeSort on (left,mid) and (mid+1,rear)<\/li>\n\n\n\n<li>Above will continue till left&lt;right<\/li>\n\n\n\n<li>Then we will call merge on the 2 subproblems<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-algorithm\"><strong>Merge sort Algorithm<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>MergeSort(arr, left, right):\n    if left &gt; right \n        return\n    mid = (left+right)\/2\n    mergeSort(arr, left, mid)\n    mergeSort(arr, mid+1, right)\n    merge(arr, left, mid, right)\nend\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-algorithm-dry-run\"><strong>Merge sort Algorithm Dry Run<\/strong><\/h2>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/111.png\"><img decoding=\"async\" width=\"616\" height=\"357\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/111.png\" alt=\"merge sort\" class=\"wp-image-18064\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/111.png 616w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/111-300x174.png 300w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"time-complexity-of-merge-sort\"><strong>Time Complexity of Merge sort&nbsp;<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the worst case, in every iteration, we are dividing the problem into further 2 subproblems. Hence this will perform log n operations and this has to be done for n iteration resulting in n log n operations total.&nbsp;<\/li>\n\n\n\n<li>In the best case that is sorted array, we can do some modification by using a flag to check whether the lament is already sorted or not<\/li>\n\n\n\n<li>Best Time Complexity: O(nlogn)<\/li>\n\n\n\n<li>Average Time Complexity: O(nlogn)<\/li>\n\n\n\n<li>Worst Time Complexity: O(nlogn)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"space-complexity-of-merge-sort\"><strong>Space Complexity of Merge sort&nbsp;<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n auxiliary space is required in Merge Sort implementation as all the elements are copied into an auxiliary array<\/li>\n\n\n\n<li>Hence space complexity is: O(n)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-program-in-c\"><strong>Merge sort program in C&nbsp;<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid merge(int arr&#091;], int start, int mid, int end) {\n\n  int len1 = mid - start + 1;\n  int len2 = end - mid;\n\n  int leftArr&#091;len1], rightArr&#091;len2];\n\n  for (int i = 0; i &lt; len1; i++)\n    leftArr&#091;i] = arr&#091;start + i];\n  for (int j = 0; j &lt; len2; j++)\n    rightArr&#091;j] = arr&#091;mid + 1 + j];\n\n  int i, j, k;\n  i = 0;\n  j = 0;\n  k = start;\n\n  while (i &lt; len1 &amp;&amp; j &lt; len2) {\n    if (leftArr&#091;i] &lt;= rightArr&#091;j]) {\n      arr&#091;k] = leftArr&#091;i];\n      i++;\n    } else {\n      arr&#091;k] = rightArr&#091;j];\n      j++;\n    }\n    k++;\n  }\n\n  while (i &lt; len1) {\n    arr&#091;k] = leftArr&#091;i];\n    i++;\n    k++;\n  }\n\n  while (j &lt; len2) {\n    arr&#091;k] = rightArr&#091;j];\n    j++;\n    k++;\n  }\n}\n\nvoid mergeSort(int arr&#091;], int start, int end) {\n  if (start &lt; end) {\n\n    int mid = start + (end - start) \/ 2;\n    mergeSort(arr, start, mid);\n    mergeSort(arr, mid + 1, end);\n    merge(arr, start, mid, end);\n  }\n}\n\nvoid display(int arr&#091;], int size) {\n  for (int i = 0; i &lt; size; i++)\n    printf(\"%d \", arr&#091;i]);\n  printf(\"n\");\n}\n\nint main() {\n  int arr&#091;] = {6, 5, 12, 10, 9, 1};\n  int size = sizeof(arr) \/ sizeof(arr&#091;0]);\n  \n  printf(\"Original arrayn\");\n  display(arr, size);\n  \n  mergeSort(arr, 0, size - 1);\n\n  printf(\"Sorted arrayn\");\n  display(arr, size);\n}\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array\n 6 5 12 10 9 1 \n Sorted array\n 1 5 6 9 10 12 <\/pre>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy PRO<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-title-link\">C Programming Course: Master C with Hands-on Projects<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Join our C Programming Course and learn C syntax, operators, expressions, control flow, functions, pointers, structures, file handling, memory management, and modular programming. Build real-world skills through practical projects!<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>2 Projects<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>10 Hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-button\">\n                C Programming Course with Certificate\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p><strong><em><a href=\"https:\/\/www.mygreatlearning.com\/blog\/quick-sort-algorithm\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Click here to know Quick Sort Algorithm using C , C++, Java, and Python (opens in a new tab)\">Click here to know Quick Sort Algorithm using C , C++, Java, and Python<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-in-java\"><strong>Merge sort in Java<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Merge Sort Algorithm using Java | Sorting Algorithm in Data Structures | Great Learning\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/z7NegJS9fAo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class MergeSort {\n\n  void merge(int arr&#091;], int left, int mid, int right) {\n\n    int len1 = mid - left + 1;\n    int len2 = right - mid;\n\n    int leftArr&#091;] = new int&#091;len1];\n    int rightArr&#091;] = new int&#091;len2];\n\n    for (int i = 0; i &lt; len1; i++)\n      leftArr&#091;i] = arr&#091;left + i];\n    for (int j = 0; j &lt; len2; j++)\n      rightArr&#091;j] = arr&#091;mid + 1 + j];\n\n    int i, j, k;\n    i = 0;\n    j = 0;\n    k = left;\n\n    while (i &lt; len1 &amp;&amp; j &lt; len2) {\n      if (leftArr&#091;i] &lt;= rightArr&#091;j]) {\n        arr&#091;k] = leftArr&#091;i];\n        i++;\n      } else {\n        arr&#091;k] = rightArr&#091;j];\n        j++;\n      }\n      k++;\n    }\n\n    while (i &lt; len1) {\n      arr&#091;k] = leftArr&#091;i];\n      i++;\n      k++;\n    }\n\n    while (j &lt; len2) {\n      arr&#091;k] = rightArr&#091;j];\n      j++;\n      k++;\n    }\n  }\n\n  void mergeSort(int arr&#091;], int start, int right) {\n    if (start &lt; right) {\n\n      int mid = (start + right) \/ 2;\n\n      mergeSort(arr, start, mid);\n      mergeSort(arr, mid + 1, right);\n\n      merge(arr, start, mid, right);\n    }\n  }\n\n  static void display(int arr&#091;]) {\n    int n = arr.length;\n    for (int i = 0; i &lt; n; ++i)\n      System.out.print(arr&#091;i] + \" \");\n    System.out.println();\n  }\n\n  public static void main(String args&#091;]) {\n    int arr&#091;] = { 6, 5, 12, 10, 9, 1 };\n\n    MergeSort ob = new MergeSort();\n    \n    System.out.println(\"Original array\");\n    display(arr);    \n    \n    ob.mergeSort(arr, 0, arr.length - 1);\n\n    System.out.println(\"Sorted array\");\n    display(arr);\n  }\n}\n\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array\n 6 5 12 10 9 1 \n Sorted array\n 1 5 6 9 10 12 <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-in-c\"><strong>Merge sort in C++&nbsp;<\/strong><br><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid merge(int arr&#091;], int start, int mid, int end) {\n  \n  int len1 = mid - start + 1;\n  int len2 = end - mid;\n\n  int leftArr&#091;len1], rightArr&#091;len2];\n\n  for (int i = 0; i &lt; len1; i++)\n    leftArr&#091;i] = arr&#091;start + i];\n  for (int j = 0; j &lt; len2; j++)\n    rightArr&#091;j] = arr&#091;mid + 1 + j];\n\n  int i, j, k;\n  i = 0;\n  j = 0;\n  k = start;\n\n  while (i &lt; len1 &amp;&amp; j &lt; len2) {\n    if (leftArr&#091;i] &lt;= rightArr&#091;j]) {\n      arr&#091;k] = leftArr&#091;i];\n      i++;\n    } else {\n      arr&#091;k] = rightArr&#091;j];\n      j++;\n    }\n    k++;\n  }\n\n  while (i &lt; len1) {\n    arr&#091;k] = leftArr&#091;i];\n    i++;\n    k++;\n  }\n\n  while (j &lt; len2) {\n    arr&#091;k] = rightArr&#091;j];\n    j++;\n    k++;\n  }\n}\n\nvoid mergeSort(int arr&#091;], int start, int end) {\n  if (start &lt; end) {\n    int mid = start + (end - start) \/ 2;\n\n    mergeSort(arr, start, mid);\n    mergeSort(arr, mid + 1, end);\n\n    merge(arr, start, mid, end);\n  }\n}\n\nvoid display(int arr&#091;], int size) {\n  for (int i = 0; i &lt; size; i++)\n    cout &lt;&lt; arr&#091;i] &lt;&lt; \" \";\n  cout &lt;&lt; endl;\n}\n\nint main() {\n  int arr&#091;] = {6, 5, 12, 10, 9, 1};\n  int size = sizeof(arr) \/ sizeof(arr&#091;0]);\n\n  cout &lt;&lt; \"Original array n\";\n  display(arr, size);  \n  \n  mergeSort(arr, 0, size - 1);\n\n  cout &lt;&lt; \"Sorted array n\";\n  display(arr, size);\n  return 0;\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array \n 6 5 12 10 9 1 \n Sorted array \n 1 5 6 9 10 12 <\/pre>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-title-link\">C++ Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Beginner to Advanced Level<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>8.1 hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-in-python\"><strong>Merge sort in Python<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def mergeSort(arr):\n    if len(arr) &gt; 1:\n\n        r = len(arr)\/\/2\n        leftArr = arr&#091;:r]\n        rightArr = arr&#091;r:]\n\n        mergeSort(leftArr)\n        mergeSort(rightArr)\n\n        i = j = k = 0\n\n       \n        while i &lt; len(leftArr) and j &lt; len(rightArr):\n            if leftArr&#091;i] &lt; rightArr&#091;j]:\n                arr&#091;k] = leftArr&#091;i]\n                i += 1\n            else:\n                arr&#091;k] = rightArr&#091;j]\n                j += 1\n            k += 1\n\n       \n        while i &lt; len(leftArr):\n            arr&#091;k] = leftArr&#091;i]\n            i += 1\n            k += 1\n\n        while j &lt; len(rightArr):\n            arr&#091;k] = rightArr&#091;j]\n            j += 1\n            k += 1\n\n\ndef display(arr):\n    for i in range(len(arr)):\n        print(arr&#091;i], end=\" \")\n    print()\n\n\nif __name__ == '__main__':\n    arr = &#091;6, 5, 12, 10, 9, 1]\n    \n    print(\"Original array\")\n    display(arr)\n    \n    mergeSort(arr)\n\n    print(\"Sorted array\")\n    display(arr)\n \n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output of the program:\n Original array\n 6 5 12 10 9 1 \n Sorted array\n 1 5 6 9 10 12<\/pre>\n\n\n\n<p> <strong><em><a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/blog\/quick-sort-algorithm\/\" target=\"_blank\">Click here to know Quick Sort Algorithm using C , C++, Java, and Python<\/a><\/em><\/strong> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-sort-example\"><strong>Merge Sort Example<\/strong><br><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Merge 2 sorted array \nInput:\n\t1 5 8 10 20\n        4 6 9 15 40 55 92<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\n     1 4 5 6 8 9 10 15 20 40 55 92 <\/pre>\n\n\n\n<p><strong>JAVA Code<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\nclass Merge {\n    static int&#091;] merge(int&#091;] arr1, int&#091;] arr2, int n, int m){\n        int ans&#091;]=new int&#091;n+m];\n        int i=0,j=0,k=0;\n        while(i&lt;n &amp;&amp; j&lt;m){\n            if(arr1&#091;i]&lt;arr2&#091;j])\n                ans&#091;k++]=arr1&#091;i++];\n            else\n                ans&#091;k++]=arr2&#091;j++];\n        }\n        \n        while(i&lt;n)\n            ans&#091;k++]=arr1&#091;i++];\n            \n        while(j&lt;m)\n            ans&#091;k++]=arr2&#091;j++];\n        \n        return ans;\n    }\n    \n    static void display(int&#091;] arr){\n        for(int i=0;i&lt;arr.length;i++)\n            System.out.print(arr&#091;i]+\" \");\n    }\n    \n\tpublic static void main (String&#091;] args) {\n\t    int&#091;] arr1={1,5,8,10,20};\n\t    int&#091;] arr2={4,6,9,15,40,55,92};\n\t    \n\t    int&#091;] ans = merge(arr1,arr2,arr1.length,arr2.length);\n\t    display(ans);\n\t    \n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>C++ Code<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid merge(int arr1&#091;], int arr2&#091;], int n, int m){\n    int ans&#091;n+m];\n    int i=0,j=0,k=0;\n    while(i&lt;n &amp;&amp; j&lt;m){\n        if(arr1&#091;i]&lt;arr2&#091;j])\n            ans&#091;k++]=arr1&#091;i++];\n        else\n            ans&#091;k++]=arr2&#091;j++];\n    }\n        \n    while(i&lt;n)\n        ans&#091;k++]=arr1&#091;i++];\n        \n    while(j&lt;m)\n        ans&#091;k++]=arr2&#091;j++];\n    \n    for(int i=0;i&lt;n+m;i++)\n        cout&lt;&lt;ans&#091;i]&lt;&lt;\" \";\n}\n\n    \nint main () {\n    int arr1&#091;]={1,5,8,10,20};\n    int arr2&#091;]={4,6,9,15,40,55,92};\n    \n    merge(arr1,arr2,sizeof(arr1)\/sizeof(arr1&#091;0]),sizeof(arr2)\/sizeof(arr2&#091;0]));\n    return 0;\n    \n}\n<\/code><\/pre>\n\n\n\n<p><strong>C Code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid merge(int arr1&#091;], int arr2&#091;], int n, int m){\n    int ans&#091;n+m];\n    int i=0,j=0,k=0;\n    while(i&lt;n &amp;&amp; j&lt;m){\n        if(arr1&#091;i]&lt;arr2&#091;j])\n            ans&#091;k++]=arr1&#091;i++];\n        else\n            ans&#091;k++]=arr2&#091;j++];\n    }\n        \n    while(i&lt;n)\n        ans&#091;k++]=arr1&#091;i++];\n        \n    while(j&lt;m)\n        ans&#091;k++]=arr2&#091;j++];\n    \n    for(int i=0;i&lt;n+m;i++)\n        printf(\"%d \",ans&#091;i]);\n}\n\n    \nint main () {\n    int arr1&#091;]={1,5,8,10,20};\n    int arr2&#091;]={4,6,9,15,40,55,92};\n    \n    merge(arr1,arr2,sizeof(arr1)\/sizeof(arr1&#091;0]),sizeof(arr2)\/sizeof(arr2&#091;0]));\n    return 0;\n    \n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-quicksort-and-mergesort\"><strong>Difference between QuickSort and MergeSort<\/strong><br><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of algorithm and complexity<\/strong>\n<ul class=\"wp-block-list\">\n<li>In Quicksort, the partition of the array in the next iteration completely depends on the choice of the pivot element. We will have 2 arrays after placing the pivot to its correct position. So if we have a sorted array, the pivot will remain at the same position, leading to n^2 complexity, as no real partition will take place.<\/li>\n\n\n\n<li>In Mergesort, we take the mid index which is (beg index + end index)\/2. Our array will always break into two subsequent arrays with approximately equal length. This keeps the complexity to n log n and is much time-efficient<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of space complexity<\/strong>\n<ul class=\"wp-block-list\">\n<li>If we are implementing our sort algorithm using recursion, then obviously our recursion stack will take n space.<\/li>\n\n\n\n<li>But in merge sort in every iteration, we create two new temporary arrays. In one we copy all the left subarray and in other, we copy all the right subarray. Since all the elements are copied, it takes another n space.<\/li>\n\n\n\n<li>In quicksort no such temporary array creation and copying takes place. Therefore no extra n space is needed<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of speed<\/strong>\n<ul class=\"wp-block-list\">\n<li>Quicksort is faster than the merge sort because of previous explanation.<\/li>\n\n\n\n<li>No temporary array, copying, etc happens in quicksort, making it much faster in execution<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of in-place<\/strong>\n<ul class=\"wp-block-list\">\n<li>In-place states that the algorithm is in-place if it does not need extra memory barring some variable creation which counts to constant space.<\/li>\n\n\n\n<li>Quicksort is in place but merge sort is not as it needs extra n space for creating temporary arrays whose sixes adds up to n&nbsp;&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of stability<\/strong>\n<ul class=\"wp-block-list\">\n<li>Stability states that the algorithm is stable if the relative ordering of the same elements in the input and output array remains the same.<\/li>\n\n\n\n<li>In quicksort, a lot of swapping takes place leading it to lose its stability whereas the merge sort maintains the relative ordering, and hence it is stable.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>QUICK SORT<\/strong><\/td><td><strong>MERGE SORT<\/strong><\/td><\/tr><tr><td>Splitting of the array depends on the value of pivot and other array elements<\/td><td>Splitting of array generally done on half<\/td><\/tr><tr><td>Worst-case time complexity is O(n2)<\/td><td>Worst-case time complexity is O(n log n)<\/td><\/tr><tr><td>It takes less n space than merge sort<\/td><td>It takes more n space than quicksort<\/td><\/tr><tr><td>It works faster than other sorting algorithms for small data set like Selection sort etc<\/td><td>It has a consistent speed on any size of data<\/td><\/tr><tr><td>It is in-place<\/td><td>It is out-place<\/td><\/tr><tr><td>Not stable<\/td><td>Stable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This brings us to the end of this article where we learned about merge sort and its implementation in various languages. <\/p>\n\n\n\n<p>A certificate will improve your chances of getting recognized in the industry. Check out Great Learning\u2019s <strong><a href=\"https:\/\/www.mygreatlearning.com\/pg-program-data-science-and-business-analytics-course\" target=\"_blank\" rel=\"noreferrer noopener\">PG program in Data Science<\/a><\/strong> to upskill in the domain. This course will help you learn from a top-ranking global school to build job-ready Data Science skills. This 6-month program offers a hands-on learning experience with top faculty and mentors. On completion, you will receive a Certificate from The University of Texas at Austin.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will cover the below-mentioned topics. If you want to learn Sorting in detail, do check out our Free Course on Sorting Algorithms in C for beginners. In this course, you will understand Sorting Algorithms and their Analysis. You will start by understanding Bubble Sort, how it works, modified Bubble sort, and [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":17635,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36858],"content_type":[],"class_list":["post-17630","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-sorting-algorithm"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Merge Sort Algorithms and Examples| Merge Sort using Java, C++<\/title>\n<meta name=\"description\" content=\"Data Structure - Merge Sort using C, C++, Java, and Python: Merge sort is one of the most efficient sorting techniques and it&#039;s based on the \u201cdivide and conquer\u201d paradigm.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?\" \/>\n<meta property=\"og:description\" content=\"Data Structure - Merge Sort using C, C++, Java, and Python: Merge sort is one of the most efficient sorting techniques and it&#039;s based on the \u201cdivide and conquer\u201d paradigm.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-09T04:27:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:54:37+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?\",\"datePublished\":\"2020-11-09T04:27:46+00:00\",\"dateModified\":\"2024-09-03T05:54:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/\"},\"wordCount\":955,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Featured-image-set-july-2.jpg\",\"keywords\":[\"sorting algorithm\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/\",\"name\":\"Merge Sort Algorithms and Examples| Merge Sort using Java, C++\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Featured-image-set-july-2.jpg\",\"datePublished\":\"2020-11-09T04:27:46+00:00\",\"dateModified\":\"2024-09-03T05:54:37+00:00\",\"description\":\"Data Structure - Merge Sort using C, C++, Java, and Python: Merge sort is one of the most efficient sorting techniques and it's based on the \u201cdivide and conquer\u201d paradigm.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Featured-image-set-july-2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Featured-image-set-july-2.jpg\",\"width\":1000,\"height\":700,\"caption\":\"MergeSort_GreatLearning\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/merge-sort\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Merge Sort Algorithms and Examples| Merge Sort using Java, C++","description":"Data Structure - Merge Sort using C, C++, Java, and Python: Merge sort is one of the most efficient sorting techniques and it's based on the \u201cdivide and conquer\u201d paradigm.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/","og_locale":"en_US","og_type":"article","og_title":"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?","og_description":"Data Structure - Merge Sort using C, C++, Java, and Python: Merge sort is one of the most efficient sorting techniques and it's based on the \u201cdivide and conquer\u201d paradigm.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-11-09T04:27:46+00:00","article_modified_time":"2024-09-03T05:54:37+00:00","og_image":[{"width":1000,"height":700,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?","datePublished":"2020-11-09T04:27:46+00:00","dateModified":"2024-09-03T05:54:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/"},"wordCount":955,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg","keywords":["sorting algorithm"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/","url":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/","name":"Merge Sort Algorithms and Examples| Merge Sort using Java, C++","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg","datePublished":"2020-11-09T04:27:46+00:00","dateModified":"2024-09-03T05:54:37+00:00","description":"Data Structure - Merge Sort using C, C++, Java, and Python: Merge sort is one of the most efficient sorting techniques and it's based on the \u201cdivide and conquer\u201d paradigm.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg","width":1000,"height":700,"caption":"MergeSort_GreatLearning"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/merge-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",1000,700,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2-300x210.jpg",300,210,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2-768x538.jpg",768,538,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",1000,700,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",1000,700,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",1000,700,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",640,448,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",96,67,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Featured-image-set-july-2.jpg",150,105,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"In this article, we will cover the below-mentioned topics. If you want to learn Sorting in detail, do check out our Free Course on Sorting Algorithms in C for beginners. In this course, you will understand Sorting Algorithms and their Analysis. You will start by understanding Bubble Sort, how it works, modified Bubble sort, and&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/17630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=17630"}],"version-history":[{"count":28,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/17630\/revisions"}],"predecessor-version":[{"id":111490,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/17630\/revisions\/111490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/17635"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=17630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=17630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=17630"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=17630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}