{"id":296,"date":"2025-06-13T13:42:52","date_gmt":"2025-06-13T08:12:52","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=296"},"modified":"2025-07-07T20:05:14","modified_gmt":"2025-07-07T14:35:14","slug":"merge-sort-algorithm-in-python","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/","title":{"rendered":"Merge Sort Algorithm in Python | Explained"},"content":{"rendered":"\n<p><strong>Merge Sort<\/strong> is a classic <strong>divide-and-conquer<\/strong> sorting technique. It divides the list into two halves, sorts each half recursively, and then <strong>merges<\/strong> the two sorted halves. <\/p>\n\n\n\n<p>So let&#8217;s get started with the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/merge-sort\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>].<\/p>\n\n\n\n<p>The major steps are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Divide<\/strong>: Split the array into two parts.<\/li>\n\n\n\n<li><strong>Conquer<\/strong>: Recursively sort each half.<\/li>\n\n\n\n<li><strong>Combine<\/strong>: Merge the two sorted halves into a single sorted list.<\/li>\n<\/ol>\n\n\n\n<p>This approach ensures a <strong>guaranteed<\/strong> time complexity of <strong><em>O(nlog\u2061n)<\/em><\/strong> in the worst, average, and best cases, making Merge Sort a reliable and efficient sorting algorithm, especially for large datasets.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-270a93a2-c156-46b3-a60d-ecaebfcb0a7e\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#0-1-the-merge_sort-function-\" style=\"\">1. The merge_sort Function<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#1-explanation-\" style=\"\">Explanation<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#2-2-the-merge_array-function-\" style=\"\">2. The merge_array Function<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#3-explanation-\" style=\"\">Explanation<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#4-3-putting-it-all-together-\" style=\"\">3. Putting It All Together<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#5-4-dry-run-example-\" style=\"\">4. Dry Run Example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#6-5-time-amp-space-complexity-\" style=\"\">5. Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-sort-algorithm-in-python\/#7-6-key-takeaways-\" style=\"\">6. Key Takeaways<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1-the-merge_sort-function-\"><strong>1. The <\/strong><strong>merge_sort<\/strong><strong> Function<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def mergeSort(self, arr, l, r):\n    if l &lt; r:\n        mid = (l + r) \/\/ 2\n        # Sort first and second halves\n        self.mergeSort(arr, l, mid)\n        self.mergeSort(arr, mid + 1, r)\n        self.merge(arr, l, mid, r)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">mergeSort<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> l &lt; r:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        mid = (l + r) \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Sort first and second halves<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.mergeSort(arr, l, mid)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.mergeSort(arr, mid + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, r)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.merge(arr, l, mid, r)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-explanation-\"><strong>Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Base Condition<\/strong>: If the list has 0 or 1 elements, it\u2019s already sorted, so return it as is.<\/li>\n\n\n\n<li><strong>Find Midpoint<\/strong>: Compute mid = len(arr) \/\/ 2 to split the array into two nearly equal halves.<\/li>\n\n\n\n<li><strong>Recursive Sort<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Recursively call merge_sort(left_half) and merge_sort(right_half).<\/li>\n\n\n\n<li>This breaks each subarray further until it reaches the base condition (lists of size 1 or 0).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Merge<\/strong>: After both halves return in sorted form, pass them to merge_array to produce a single, sorted list.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-2-the-merge_array-function-\"><strong>2. The <\/strong><strong>merge_array<\/strong><strong> Function<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def merge(self, arr, l, mid, r):\n    # Create temp arrays\n    left = arr[l : mid + 1]\n    right = arr[mid + 1 : r + 1]\n\n    i = 0\n    j = 0\n    k = l\n\n    # Merge the temp arrays back into arr[l..r]\n    while i &lt; len(left) and j &lt; len(right):\n        if left[i] &lt;= right[j]:\n            arr[k] = left[i]\n            i += 1\n        else:\n            arr[k] = right[j]\n            j += 1\n        k += 1\n\n    # Copy the remaining elements of left[], if any\n    while i &lt; len(left):\n        arr[k] = left[i]\n        i += 1\n        k += 1\n\n    # Copy the remaining elements of right[], if any\n    while j &lt; len(right):\n        arr[k] = right[j]\n        j += 1\n        k += 1\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">merge<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">mid<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Create temp arrays<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    left = arr[l : mid + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    right = arr[mid + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> : r + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    i = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    j = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    k = l<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Merge the temp arrays back into arr[l..r]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> i &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(left) <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> j &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(right):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> left[i] &lt;= right[j]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr[k] = left[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            i += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr[k] = right[j]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            j += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        k += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Copy the remaining elements of left[], if any<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> i &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(left):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        arr[k] = left[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        i += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        k += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Copy the remaining elements of right[], if any<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> j &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(right):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        arr[k] = right[j]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        j += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        k += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-explanation-\"><strong>Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>: Create an empty list merged. Set pointers i and j to 0, indicating the current index in left and right, respectively.<\/li>\n\n\n\n<li><strong>Compare and Pick<\/strong>:\n<ul class=\"wp-block-list\">\n<li>While both i &lt; len(left) and j &lt; len(right), compare left[i] and right[j].<\/li>\n\n\n\n<li>Whichever element is smaller, append it to merged and move that pointer forward.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Collect Remaining<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If any elements remain in left after one of the lists is exhausted, append all of them.<\/li>\n\n\n\n<li>Do the same for the remaining elements in right.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Result<\/strong>: merged now contains a sorted combination of the two sublists.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Whole Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n \n    def mergeSort(self, arr, l, r):\n        if l &lt; r:\n            mid = (l + r) \/\/ 2\n            # Sort first and second halves\n            self.mergeSort(arr, l, mid)\n            self.mergeSort(arr, mid + 1, r)\n            self.merge(arr, l, mid, r)\n    \n    def merge(self, arr, l, mid, r):\n        # Create temp arrays\n        left = arr[l:mid+1]\n        right = arr[mid+1:r+1]\n        \n        i = 0\n        j = 0\n        k = l\n        \n        # Merge the temp arrays back into arr[l..r]\n        while i &lt; len(left) and j &lt; len(right):\n            if left[i] &lt;= right[j]:\n                arr[k] = left[i]\n                i += 1\n            else:\n                arr[k] = right[j]\n                j += 1\n            k += 1\n        \n        # Copy the remaining elements of left[], if any\n        while i &lt; len(left):\n            arr[k] = left[i]\n            i += 1\n            k += 1\n        \n        # Copy the remaining elements of right[], if any\n        while j &lt; len(right):\n            arr[k] = right[j]\n            j += 1\n            k += 1\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">mergeSort<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> l &lt; r:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            mid = (l + r) \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Sort first and second halves<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.mergeSort(arr, l, mid)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.mergeSort(arr, mid + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, r)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.merge(arr, l, mid, r)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">merge<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">mid<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Create temp arrays<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        left = arr[l:mid+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        right = arr[mid+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:r+<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        i = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        j = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        k = l<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Merge the temp arrays back into arr[l..r]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> i &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(left) <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> j &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(right):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> left[i] &lt;= right[j]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr[k] = left[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                i += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr[k] = right[j]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                j += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            k += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Copy the remaining elements of left[], if any<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> i &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(left):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr[k] = left[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            i += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            k += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Copy the remaining elements of right[], if any<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> j &lt; <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(right):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr[k] = right[j]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            j += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            k += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read about the Python Program to <strong><a href=\"https:\/\/codeanddebug.in\/blog\/quick-sort-in-python\/\" data-type=\"post\" data-id=\"299\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Implement Quick Sort Algorithm<\/span><\/mark><\/a><\/strong>.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-3-putting-it-all-together-\"><strong>3. Putting It All Together<\/strong><\/h2>\n\n\n\n<p><strong>Initial Array<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"arr = [64, 34, 25, 12, 22, 11, 90]\nsorted_arr = merge_sort(arr)\nprint(f&quot;Sorted array = {sorted_arr}&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">arr = [<\/span><span style=\"color: #B5CEA8\">64<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">34<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">25<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">12<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">22<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">11<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">90<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">sorted_arr = merge_sort(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&quot;Sorted array = <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">sorted_arr<\/span><span style=\"color: #569CD6\">}<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Call <\/strong><strong>merge_sort(arr)<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Splits arr into two halves.<\/li>\n\n\n\n<li>Recursively sorts each half.<\/li>\n\n\n\n<li>Merges them via merge_array.<\/li>\n<\/ol>\n\n\n\n<p><strong>Final Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Sorted array = [11, 12, 22, 25, 34, 64, 90]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Sorted array = [<\/span><span style=\"color: #B5CEA8\">11<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">12<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">22<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">25<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">34<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">64<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">90<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-4-dry-run-example-\"><strong>4. Dry Run Example<\/strong><\/h2>\n\n\n\n<p>Let\u2019s dry run a smaller sub-problem: [34, 25, 11].<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Split<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Left: [34]<\/li>\n\n\n\n<li>Right: [25, 11]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursively Sort Left<\/strong>: [34] is a single element, so it\u2019s already sorted.<\/li>\n\n\n\n<li><strong>Recursively Sort Right<\/strong>: Split [25, 11] into [25] and [11], both single elements.\n<ul class=\"wp-block-list\">\n<li>Merge [25] and [11] \u2192 [11, 25].<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Merge<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Merge [34] and [11, 25].<\/li>\n\n\n\n<li>Compare 34 with 11 \u2192 take 11. Next compare 34 with 25 \u2192 take 25. Finally, take 34.<\/li>\n\n\n\n<li>Final merged list: [11, 25, 34].<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-5-time-amp-space-complexity-\"><strong>5. Time &amp; Space Complexity<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Each time we split the list into two halves and then merge them.<\/li>\n\n\n\n<li>The depth of recursion is about <strong><em>log\u2061n<\/em><\/strong>.<\/li>\n\n\n\n<li>At each level, merging all elements takes <strong><em>O(n)<\/em><\/strong>.<\/li>\n\n\n\n<li>Overall time complexity: <strong>O(n.log\u2061n)<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We allocate extra space for the sublists and for merging.<\/li>\n\n\n\n<li>Overall space complexity: <strong>O(n)<\/strong> in most implementations.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-6-key-takeaways-\"><strong>6. Key Takeaways<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Divide-and-Conquer<\/strong>: Merge Sort systematically splits the array and merges results.<\/li>\n\n\n\n<li><strong>Consistent Performance<\/strong>: It guarantees <strong>O(nlog\u2061n)<\/strong> even in the worst case.<\/li>\n\n\n\n<li><strong>Stable Sorting<\/strong>: Merge Sort preserves the order of equal elements, which can be advantageous in certain scenarios.<\/li>\n<\/ol>\n\n\n\n<p><strong>Requires Extra Space<\/strong>: Unlike <strong>in-place<\/strong> sorts, Merge Sort needs additional memory to merge sublists.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.codeanddebug.in\/course\/zero-to-hero-python-dsa\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Merge Sort is a classic divide-and-conquer sorting technique. It divides the list into two halves, sorts each half recursively, and then merges the two sorted halves. So let&#8217;s get started with the [Problem Link]. The major steps are: This approach ensures a guaranteed time complexity of O(nlog\u2061n) in the worst, average, and best cases, making<\/p>\n","protected":false},"author":1,"featured_media":297,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,4],"tags":[12],"class_list":{"0":"post-296","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-sorting-algos"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/merge-sort-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":551,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/296\/revisions\/551"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/297"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}