{"id":42,"date":"2022-03-30T04:56:53","date_gmt":"2022-03-30T04:56:53","guid":{"rendered":"http:\/\/www.programminginpython.com\/2023\/03\/30\/merge-sort-algorithm-in-python\/"},"modified":"2023-05-08T18:38:29","modified_gmt":"2023-05-08T18:38:29","slug":"merge-sort-algorithm-python","status":"publish","type":"post","link":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/","title":{"rendered":"Merge Sort Algorithm in Python"},"content":{"rendered":"<p>Hello everyone, welcome back to <a href=\"\/\" target=\"_blank\" rel=\"noopener\">Programming In Python<\/a>. Here in this post am going to tell you how to implement Merge Sort Algorithm in Python. In the previous posts, I have said about <a href=\"http:\/\/programminginpython.com\/selection-sort-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Selection Sort<\/a> and <a href=\"http:\/\/programminginpython.com\/bubble-sort-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Bubble Sort<\/a>, here this Merge sort is much more efficient than those two algorithms as their\u00a0time complexity is O(n<sup>2<\/sup>) in the average case and here it is O(n log n) in the worst case, this says how efficient Merge sort is than <a href=\"http:\/\/programminginpython.com\/selection-sort-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Selection<\/a> and <a href=\"http:\/\/programminginpython.com\/bubble-sort-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Bubble<\/a> sort.<\/p>\n<p>Merge sort algorithm is a divide and conquer algorithm, where the main unsorted list is divided into 2 halves, and each of the halves is again divided into 2 halves until only a single element is left in the list. Now merge these sublists by sorting them and finally, the main list is sorted.<\/p>\n<div class=\"yrc-shell-cover yrc-single\" data-yrc-uid=\"69e0d686474d0\" data-yrc-channel=\"{&quot;meta&quot;:{&quot;user&quot;:&quot;Programming In Python&quot;,&quot;channel&quot;:&quot;UCLBlXUMLYLxopRljNdLXOeQ&quot;,&quot;key&quot;:&quot;yrc_1683138754&quot;,&quot;apikey&quot;:&quot;AIzaSyC3vU6rFyzsO0shrRsAWmjuSiuWYTQhafc&quot;,&quot;cache&quot;:&quot;1440&quot;,&quot;channel_uploads&quot;:&quot;UULBlXUMLYLxopRljNdLXOeQ&quot;,&quot;onlyonce&quot;:&quot;&quot;,&quot;tag&quot;:&quot;&quot;,&quot;per_page&quot;:&quot;24&quot;,&quot;maxv&quot;:&quot;0&quot;,&quot;consent&quot;:{&quot;ask&quot;:&quot;&quot;,&quot;url&quot;:&quot;&quot;},&quot;ads&quot;:&quot;1&quot;,&quot;uid&quot;:&quot;69e0d686474d0&quot;,&quot;nocookie&quot;:&quot;&quot;,&quot;single&quot;:true},&quot;style&quot;:{&quot;colors&quot;:{&quot;item&quot;:{&quot;background&quot;:&quot;inherit&quot;},&quot;button&quot;:{&quot;background&quot;:&quot;#333&quot;,&quot;color&quot;:&quot;#fff&quot;},&quot;color&quot;:{&quot;text&quot;:&quot;#fff&quot;,&quot;link&quot;:&quot;inherit&quot;,&quot;menu&quot;:&quot;#000&quot;,&quot;meta&quot;:&quot;inherit&quot;}},&quot;theme&quot;:{&quot;videos&quot;:{&quot;style&quot;:&quot;__grid&quot;,&quot;thumb&quot;:[&quot;large&quot;,&quot;open&quot;],&quot;desc&quot;:&quot;&quot;,&quot;carousel&quot;:{&quot;thumbs&quot;:&quot;4&quot;,&quot;thumbs_to_slide&quot;:&quot;2&quot;,&quot;spacing&quot;:&quot;10&quot;},&quot;carousel_nav&quot;:{&quot;modifier&quot;:&quot;__sides&quot;,&quot;position&quot;:&quot;left-none&quot;,&quot;location&quot;:&quot;prepend&quot;,&quot;background&quot;:&quot;#fff&quot;,&quot;color&quot;:&quot;#000&quot;,&quot;font_size&quot;:&quot;2&quot;,&quot;border_radius&quot;:&quot;0&quot;}},&quot;a&quot;:&quot;1&quot;},&quot;fit&quot;:&quot;false&quot;,&quot;playlists&quot;:&quot;true&quot;,&quot;uploads&quot;:&quot;true&quot;,&quot;player_mode&quot;:&quot;1&quot;,&quot;truncate&quot;:&quot;&quot;,&quot;banner&quot;:&quot;true&quot;,&quot;thumb_margin&quot;:&quot;8&quot;,&quot;play_icon&quot;:&quot;hover&quot;,&quot;youtube_play_icon&quot;:&quot;&quot;,&quot;thumb_image_size&quot;:&quot;medium&quot;,&quot;default_tab&quot;:&quot;uploads&quot;,&quot;sticky&quot;:{&quot;enable&quot;:&quot;&quot;,&quot;width&quot;:&quot;400&quot;,&quot;position&quot;:&quot;bottom-right&quot;,&quot;only_above&quot;:&quot;768&quot;,&quot;margin&quot;:&quot;12&quot;},&quot;player&quot;:{&quot;show_desc&quot;:&quot;&quot;,&quot;show_meta&quot;:&quot;&quot;},&quot;menu&quot;:&quot;1&quot;,&quot;rating_style&quot;:&quot;NaN&quot;,&quot;rtl&quot;:false}}\" data-yrc-setup=\"\"><\/div>\r\n\t\t\t<script data-cfasync=\"false\" type=\"text\/javascript\">\r\n\t\t\t\tif( !window.YRC ) var YRC = {Data:{}};\r\n\t\t\t\tYRC.Data[\"69e0d686474d0\"] = {\"video\":\"tRTu9vM7wIw\"};\r\n\t\t\t\t(function(){\r\n\t\t\t\t\tif(!YRC.loaded){\r\n\t\t\t\t\t    YRC.loaded = true;\r\n\t\t\t\t\t\tfunction YRC_Loader(){\r\n\t\t\t\t\t\t\t\/\/YRC.loaded = true;\r\n\t\t\t\t\t\t\tYRC.is_pro = false;\r\n\t\t\t\t\t\t\tYRC.is_pb = false;\r\n\t\t\t\t\t\t\tYRC.lang = {\"form\":{\"Videos\":\"Videos\",\"Playlists\":\"Playlists\",\"Search\":\"Search\",\"Loading\":\"Loading\",\"more\":\"more\",\"Nothing_found\":\"Nothing found\",\"Prev\":\"Previous\",\"Next\":\"Next\",\"consent_statement\":\"Allow cookies?\",\"consent_button\":\"Allow\",\"consent_privacy_policy\":\"Privacy policy\"},\"fui\":{\"sort_by\":\"Sort by\",\"relevant\":\"Relevant\",\"latest\":\"Latest\",\"liked\":\"Liked\",\"title\":\"Title\",\"views\":\"Views\",\"duration\":\"Duration\",\"any\":\"Any\",\"_short\":\"Short\",\"medium\":\"Medium\",\"_long\":\"Long\",\"uploaded\":\"Uploaded\",\"all_time\":\"All time\",\"live_now\":\"Live Now\",\"today\":\"Today\",\"ago\":\"ago\",\"last\":\"Last\",\"day\":\"day\",\"days\":\"days\",\"week\":\"week\",\"weeks\":\"weeks\",\"month\":\"month\",\"months\":\"months\",\"year\":\"year\",\"years\":\"years\",\"older\":\"Older\",\"show_more\":\"Show More\",\"show_less\":\"Show Less\",\"reply\":\"REPLY\",\"view_replies\":\"View replies\",\"write_comment\":\"Write comment...\",\"billion\":\"B\",\"million\":\"M\",\"thousand\":\"K\",\"max_plain_number\":1000,\"wplocale\":\"en_US\"}};\r\n\t\t\t\t\t\t\tYRC.is_admin = false;\r\n\t\t\t\t\t\t\tvar script = document.createElement(\"script\");\r\n\t\t\t\t\t\t\t\tscript.setAttribute(\"data-cfasync\", \"false\");\r\n\t\t\t\t\t\t\t\tscript.setAttribute(\"type\", \"text\/javascript\");\r\n\t\t\t\t\t\t\t\tscript.src = \"https:\/\/www.programminginpython.com\/wp-content\/plugins\/yourchannel\/js\/yrc.js?1.2.9\";\r\n\t\t\t\t\t\t\t\tscript.id = \"yrc-script\";\r\n\t\t\t\t\t\t\t\tdocument.querySelector(\"head\").appendChild(script);\r\n\t\t\t\t\t\t\tvar style = document.createElement(\"link\");\r\n\t\t\t\t\t\t\t\tstyle.rel = \"stylesheet\";\r\n\t\t\t\t\t\t\t\tstyle.href = \"https:\/\/www.programminginpython.com\/wp-content\/plugins\/yourchannel\/css\/style.css?1.2.9\";\r\n\t\t\t\t\t\t\t\tstyle.type = \"text\/css\";\r\n\t\t\t\t\t\t\t\tdocument.querySelector(\"head\").appendChild(style);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tif(window.jQuery){YRC_Loader();}else { var yrctimer2324 = window.setInterval(function(){\r\n\t\t\t\t\t\t\tif(window.jQuery){YRC_Loader(); window.clearInterval(yrctimer2324); }\r\n\t\t\t\t\t\t}, 250);}\r\n\t\t\t\t\t} else {if(YRC.EM)YRC.EM.trigger(\"yrc.newchannel\");}\r\n\t\t\t\t}());<\/script>\n<p style=\"text-align: center;\"><strong>You can also watch the video on YouTube <a href=\"https:\/\/youtu.be\/tRTu9vM7wIw\" target=\"_blank\" rel=\"noopener\">here<\/a><\/strong><\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/Algorithms\/Sorting%20Algorithms\/merge_sort.py\" target=\"_blank\" rel=\"noopener\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<p>See the animation below,<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-758 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-sort-example-300px-300x180-1.gif\" alt=\"Merge Sort\" width=\"300\" height=\"180\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 300px; --smush-placeholder-aspect-ratio: 300\/180;\" \/><\/p>\n<p style=\"text-align: center;\">By <a class=\"new\" title=\"User:Swfung8 (page does not exist)\" href=\"\/\/commons.wikimedia.org\/w\/index.php?title=User:Swfung8&amp;action=edit&amp;redlink=1\">Swfung8<\/a> \u2013 <span class=\"int-own-work\" lang=\"en\">Own work<\/span>, <a title=\"Creative Commons Attribution-Share Alike 3.0\" href=\"https:\/\/creativecommons.org\/licenses\/by-sa\/3.0\">CC BY-SA 3.0<\/a>, <a href=\"https:\/\/commons.wikimedia.org\/w\/index.php?curid=14961648\">Link<\/a><\/p>\n<h4 class=\"post_h4\">Time Complexity Of Merge Sort<\/h4>\n<table width=\"100%\">\n<tbody>\n<tr>\n<td><strong>Best Case<\/strong><\/td>\n<td><strong>O(n<span style=\"font-size: 14.4px;\">\u00a0log n<\/span>)<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Average Case<\/strong><\/td>\n<td><strong>O(n<span style=\"font-size: 14.4px;\">\u00a0log n<\/span>)<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Worst Case<\/strong><\/td>\n<td><strong>O(n<span style=\"font-size: 14.4px;\">\u00a0log n<\/span>)<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 class=\"post_h4\">Algorithm<\/h2>\n<ol>\n<li>Divide the unsorted list into\u00a0<i>n<\/i>\u00a0sublists, until each list has only 1 element (a list of 1 element is considered sorted).<\/li>\n<li>Repeatedly\u00a0merge\u00a0sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.<\/li>\n<\/ol>\n<h2 class=\"visualize_iframe post_h4\">Merge Sort Algorithm \u2013 Code Visualization<\/h2>\n<p><center><iframe src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=__author__%20%3D%20'Avinash'%0A%0A%0Adef%20merge_sort%28sort_list%29%3A%0A%20%20%20%20print%28%22splitting%22,%20sort_list%29%0A%20%20%20%20if%20len%28sort_list%29%20%3E%201%3A%0A%20%20%20%20%20%20%20%20mid%20%3D%20len%28sort_list%29%20\/\/%202%0A%20%20%20%20%20%20%20%20leftHalf%20%3D%20sort_list%5B%3Amid%5D%0A%20%20%20%20%20%20%20%20rightHalf%20%3D%20sort_list%5Bmid%3A%5D%0A%0A%20%20%20%20%20%20%20%20merge_sort%28leftHalf%29%0A%20%20%20%20%20%20%20%20merge_sort%28rightHalf%29%0A%0A%20%20%20%20%20%20%20%20i%20%3D%200%0A%20%20%20%20%20%20%20%20j%20%3D%200%0A%20%20%20%20%20%20%20%20k%20%3D%200%0A%20%20%20%20%20%20%20%20while%20i%20%3C%20len%28leftHalf%29%20and%20j%20%3C%20len%28rightHalf%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20leftHalf%5Bi%5D%20%3C%20rightHalf%5Bj%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sort_list%5Bk%5D%20%3D%20leftHalf%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20i%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sort_list%5Bk%5D%20%3D%20rightHalf%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20j%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20k%20%3D%20k%20%2B%201%0A%0A%20%20%20%20%20%20%20%20while%20i%20%3C%20len%28leftHalf%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sort_list%5Bk%5D%20%3D%20leftHalf%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20i%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20k%20%3D%20k%20%2B%201%0A%0A%20%20%20%20%20%20%20%20while%20j%20%3C%20len%28rightHalf%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sort_list%5Bk%5D%20%3D%20rightHalf%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20j%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20k%20%3D%20k%20%2B%201%0A%20%20%20%20print%28%22merging...%22,%20sort_list%29%0A%0A%0Alst%20%3D%20%5B%5D%0Asize%20%3D%20int%28input%28%22Enter%20size%20of%20the%20list%3A%20%5Ct%22%29%29%0A%0Afor%20i%20in%20range%28size%29%3A%0A%20%20%20%20elements%20%3D%20int%28input%28%22Enter%20an%20element%3A%20%5Ct%22%29%29%0A%20%20%20%20lst.append%28elements%29%0A%0Amerge_sort%28lst%29&amp;codeDivHeight=400&amp;codeDivWidth=350&amp;cumulative=false&amp;curInstr=0&amp;heapPrimitives=false&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false\" width=\"800\" height=\"500\" frameborder=\"0\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span> <\/iframe><\/center><\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/Algorithms\/Sorting%20Algorithms\/merge_sort.py\" target=\"_blank\" rel=\"noopener\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<h2 class=\"post_h4\">Program<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">__author__ = 'Avinash'\r\n\r\n\r\ndef merge_sort(sort_list):\r\n    print(\"splitting\", sort_list)\r\n    if len(sort_list) &gt; 1:\r\n\r\n        mid = len(sort_list) \/\/ 2\r\n        leftHalf = sort_list[:mid]\r\n        rightHalf = sort_list[mid:]\r\n\r\n        merge_sort(leftHalf)\r\n        merge_sort(rightHalf)\r\n\r\n        i = 0\r\n        j = 0\r\n        k = 0\r\n\r\n        while i &lt; len(leftHalf) and j &lt; len(rightHalf):\r\n            if leftHalf[i] &lt; rightHalf[j]:\r\n                sort_list[k] = leftHalf[i]\r\n                i = i + 1\r\n            else:\r\n                sort_list[k] = rightHalf[j]\r\n                j = j + 1\r\n            k = k + 1\r\n\r\n        while i &lt; len(leftHalf):\r\n            sort_list[k] = leftHalf[i]\r\n            i = i + 1\r\n            k = k + 1\r\n\r\n        while j &lt; len(rightHalf):\r\n            sort_list[k] = rightHalf[j]\r\n            j = j + 1\r\n            k = k + 1\r\n\r\n    print(\"merging...\", sort_list)\r\n\r\nlst = []\r\n\r\nsize = int(input(\"Enter size of the list: \\t\"))\r\n\r\nfor i in range(size):\r\n    elements = int(input(\"Enter an element: \\t\"))\r\n    lst.append(elements)\r\n\r\nmerge_sort(lst)<\/pre>\n<blockquote><p>Ad:<br \/>\n<span style=\"font-size: 18pt;\">Learn Python Programming Masterclass \u2013 <a href=\"https:\/\/bit.ly\/python-programming-masterclass\" target=\"_blank\" rel=\"noopener\">Enroll Now<\/a>.<\/span><br \/>\n<span style=\"font-size: 12px;\">Udemy<\/span><\/p><\/blockquote>\n<h2 class=\"post_h4\">Output<\/h2>\n<figure id=\"attachment_927\" class=\"wp-caption aligncenter\" style=\"width: 1024px;\" aria-describedby=\"caption-attachment-927\">\n<p><figure id=\"attachment_759\" aria-describedby=\"caption-attachment-759\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-759 size-full lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/MergeSort-1024x466-1.png\" alt=\"Merge Sort Algorithm\" width=\"1024\" height=\"466\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/MergeSort-1024x466-1.png 1024w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/MergeSort-1024x466-1.png 300w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/MergeSort-1024x466-1.png 768w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/466;\" \/><figcaption id=\"caption-attachment-759\" class=\"wp-caption-text\">Merge Sort Algorithm in Python<\/figcaption><\/figure><\/figure>\n<figure id=\"attachment_930\" class=\"wp-caption aligncenter\" style=\"width: 1024px;\" aria-describedby=\"caption-attachment-930\">\n<p><figure id=\"attachment_760\" aria-describedby=\"caption-attachment-760\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-760 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/merge_sort-1024x315-1.png\" alt=\"Merge Sort Algorithm\" width=\"1024\" height=\"315\" data-srcset=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/merge_sort-1024x315-1.png 1024w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/merge_sort-1024x315-1.png 300w, https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/merge_sort-1024x315-1.png 768w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/315;\" \/><figcaption id=\"caption-attachment-760\" class=\"wp-caption-text\">Merge Sort Algorithm<\/figcaption><\/figure><\/figure>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/Algorithms\/Sorting%20Algorithms\/merge_sort.py\" target=\"_blank\" rel=\"noopener\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<p>Also, feel free to look at other <a href=\"http:\/\/programminginpython.com\/category\/algorithms\/sorting-algorithms\/\" target=\"_blank\" rel=\"noopener\">sorting algorithms<\/a> like <a href=\"http:\/\/programminginpython.com\/bubble-sort-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Bubble Sort<\/a> or <a href=\"http:\/\/programminginpython.com\/selection-sort-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Selection Sort<\/a> and <a href=\"http:\/\/programminginpython.com\/category\/algorithms\/sorting-algorithms\/\" target=\"_blank\" rel=\"noopener\">searching algorithms<\/a> like <a href=\"http:\/\/programminginpython.com\/python-program-linear-search-algorithm\/\" target=\"_blank\" rel=\"noopener\">Linear Search<\/a> and <a href=\"http:\/\/programminginpython.com\/binary-search-algorithm-python\/\" target=\"_blank\" rel=\"noopener\">Binary Search<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, welcome back to programminginpython.com. Here in this post am going to tell you how to implement Merge Sort Algorithm in Python. In the previous posts, I have said about Selection Sort and Bubble &hellip;<\/p>\n","protected":false},"author":1,"featured_media":327,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,9],"tags":[90,96,91],"class_list":["post-42","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithms","category-sorting-algorithms","tag-algorithms","tag-merge-sort-algorithm","tag-sorting-algorithms"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Merge Sort Algorithm in Python - Programming In Python<\/title>\n<meta name=\"description\" content=\"A program to implement the merge sort algorithm in Python, which is of divide and conquer principle, where a list is broke into sub lists.\" \/>\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.programminginpython.com\/merge-sort-algorithm-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Merge Sort Algorithm in Python - Programming In Python\" \/>\n<meta property=\"og:description\" content=\"A program to implement the merge sort algorithm in Python, which is of divide and conquer principle, where a list is broke into sub lists.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Programming In Python\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/programminginpython\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-30T04:56:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-08T18:38:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python_Fb.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"AVINASH NETHALA\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@python_pip\" \/>\n<meta name=\"twitter:site\" content=\"@python_pip\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AVINASH NETHALA\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Merge Sort Algorithm in Python - Programming In Python","description":"A program to implement the merge sort algorithm in Python, which is of divide and conquer principle, where a list is broke into sub lists.","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.programminginpython.com\/merge-sort-algorithm-python\/","og_locale":"en_US","og_type":"article","og_title":"Merge Sort Algorithm in Python - Programming In Python","og_description":"A program to implement the merge sort algorithm in Python, which is of divide and conquer principle, where a list is broke into sub lists.","og_url":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/","og_site_name":"Programming In Python","article_publisher":"https:\/\/www.facebook.com\/programminginpython","article_published_time":"2022-03-30T04:56:53+00:00","article_modified_time":"2023-05-08T18:38:29+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python_Fb.png","type":"image\/png"}],"author":"AVINASH NETHALA","twitter_card":"summary_large_image","twitter_creator":"@python_pip","twitter_site":"@python_pip","twitter_misc":{"Written by":"AVINASH NETHALA","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#article","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/"},"author":{"name":"AVINASH NETHALA","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c"},"headline":"Merge Sort Algorithm in Python","datePublished":"2022-03-30T04:56:53+00:00","dateModified":"2023-05-08T18:38:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/"},"wordCount":301,"commentCount":0,"publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"image":{"@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python1.png","keywords":["algorithms","merge sort algorithm","sorting algorithms"],"articleSection":["Algorithms","Sorting Algorithms"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/","url":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/","name":"Merge Sort Algorithm in Python - Programming In Python","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#primaryimage"},"image":{"@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python1.png","datePublished":"2022-03-30T04:56:53+00:00","dateModified":"2023-05-08T18:38:29+00:00","description":"A program to implement the merge sort algorithm in Python, which is of divide and conquer principle, where a list is broke into sub lists.","breadcrumb":{"@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#primaryimage","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python1.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python1.png","width":1920,"height":400,"caption":"Merge Sort Algorithm in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.programminginpython.com\/merge-sort-algorithm-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.programminginpython.com\/"},{"@type":"ListItem","position":2,"name":"Merge Sort Algorithm in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.programminginpython.com\/#website","url":"https:\/\/www.programminginpython.com\/","name":"Programming In Python","description":"All About Python","publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"alternateName":"pip","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.programminginpython.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.programminginpython.com\/#organization","name":"Programming In Python","alternateName":"PIP","url":"https:\/\/www.programminginpython.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","width":500,"height":500,"caption":"Programming In Python"},"image":{"@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/programminginpython","https:\/\/x.com\/python_pip","https:\/\/www.youtube.com\/programminginpython","https:\/\/github.com\/avinashn\/programminginpython.com"]},{"@type":"Person","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c","name":"AVINASH NETHALA","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","caption":"AVINASH NETHALA"},"sameAs":["https:\/\/www.programminginpython.com\/"],"url":"https:\/\/www.programminginpython.com\/author\/avinash\/"}]}},"jetpack_featured_media_url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/Merge-Sort-Algorithm-Python1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":4,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":762,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/42\/revisions\/762"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media\/327"}],"wp:attachment":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}