{"id":2457,"date":"2019-12-30T10:29:17","date_gmt":"2019-12-30T07:29:17","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2457"},"modified":"2024-12-16T22:32:31","modified_gmt":"2024-12-16T19:32:31","slug":"insertion-sort-merge-sort","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2019\/12\/30\/insertion-sort-merge-sort\/","title":{"rendered":"Insertion Sort, Merge Sort"},"content":{"rendered":"<h3>Insertion Sort<\/h3>\n<p>Insertion Sort &#8211; each <strong>element<\/strong> is compared to the <strong>previous<\/strong> ones in the list and the <strong>element<\/strong> is swapped with the larger one if there is one, otherwise the inner comparison loop stops. Since the elements are sorted from first to last, each element is compared to the already sorted list, which *may* reduce the overall running time. The time complexity of the algorithm is O(n^2), i.e. identical to bubble sort.<\/p>\n<h3>Merge Sort<\/h3>\n<p>Merge sort &#8211; the list is divided into groups of one element, then the groups are &#8220;merged&#8221; in pairs with simultaneous comparison. In my implementation, when merging pairs, the elements on the left are compared with the elements on the right, then moved to the resulting list, if there are no more elements on the left, then all the elements on the right are added to the resulting list (their additional comparison is unnecessary, since all elements in the groups undergo sorting iterations)<br \/>The operation of this algorithm is very easy to parallelize, the pair merging stage can be performed in threads, waiting for the end of iterations in the dispatcher.<br \/>Output of the algorithm for single-threaded execution:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>[\"John\", \"Alice\", \"Mike\", \"#1\", \"\u0410\u0440\u0442\u0435\u043c\", \"20\", \"60\", \"60\", \"DoubleTrouble\"]\n[[\"John\"], [\"Alice\"], [\"Mike\"], [\"#1\"], [\"\u0410\u0440\u0442\u0435\u043c\"], [\"20\"], [\"60\"], [\"60\"], [\"DoubleTrouble\"]]\n[[\"Alice\", \"John\"], [\"#1\", \"Mike\"], [\"20\", \"\u0410\u0440\u0442\u0435\u043c\"], [\"60\", \"60\"], [\"DoubleTrouble\"]]\n[[\"#1\", \"Alice\", \"John\", \"Mike\"], [\"20\", \"60\", \"60\", \"\u0410\u0440\u0442\u0435\u043c\"], [\"DoubleTrouble\"]]\n[[\"#1\", \"20\", \"60\", \"60\", \"Alice\", \"John\", \"Mike\", \"\u0410\u0440\u0442\u0435\u043c\"], [\"DoubleTrouble\"]]\n[\"#1\", \"20\", \"60\", \"60\", \"Alice\", \"DoubleTrouble\", \"John\", \"Mike\", \"\u0410\u0440\u0442\u0435\u043c\"]\n\n<\/code><\/pre>\n<\/div>\n<p>Output of the algorithm for multithreaded execution:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>[\"John\", \"Alice\", \"Mike\", \"#1\", \"\u0410\u0440\u0442\u0435\u043c\", \"20\", \"60\", \"60\", \"DoubleTrouble\"]\n[[\"John\"], [\"Alice\"], [\"Mike\"], [\"#1\"], [\"\u0410\u0440\u0442\u0435\u043c\"], [\"20\"], [\"60\"], [\"60\"], [\"DoubleTrouble\"]]\n[[\"20\", \"\u0410\u0440\u0442\u0435\u043c\"], [\"Alice\", \"John\"], [\"60\", \"60\"], [\"#1\", \"Mike\"], [\"DoubleTrouble\"]]\n[[\"#1\", \"60\", \"60\", \"Mike\"], [\"20\", \"Alice\", \"John\", \"\u0410\u0440\u0442\u0435\u043c\"], [\"DoubleTrouble\"]]\n[[\"DoubleTrouble\"], [\"#1\", \"20\", \"60\", \"60\", \"Alice\", \"John\", \"Mike\", \"\u0410\u0440\u0442\u0435\u043c\"]]\n[\"#1\", \"20\", \"60\", \"60\", \"Alice\", \"DoubleTrouble\", \"John\", \"Mike\", \"\u0410\u0440\u0442\u0435\u043c\"]\n\n<\/code><\/pre>\n<\/div>\n<p>The time complexity of the algorithm is O(n*log(n)), which is slightly better than O(n^2)<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Insertion_sort\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/en.wikipedia.org\/wiki\/Insertion_sort<\/a ><br \/><a href=\"https:\/\/en.wikipedia.org\/wiki\/Merge_sort\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/en.wikipedia.org\/wiki\/Merge_sort<\/a><\/p>\n<h3>Source code<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/insertionSort\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum \/algorithms\/-\/tree\/master\/sortAlgorithms\/insertionSort<\/a><br \/><a href=\"https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/mergeSort\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/mergeSort<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Insertion Sort Insertion Sort &#8211; each element is compared to the previous ones in the list and the element is swapped with the larger one if there is one, otherwise the inner comparison loop stops. Since the elements are sorted from first to last, each element is compared to the already sorted list, which *may*<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2019\/12\/30\/insertion-sort-merge-sort\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Insertion Sort, Merge Sort&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[131,138,139,190],"class_list":["post-2457","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-algorithms","tag-insertion-sort","tag-merge-sort","tag-sorting","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=2457"}],"version-history":[{"count":19,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2457\/revisions"}],"predecessor-version":[{"id":3927,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2457\/revisions\/3927"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=2457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=2457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=2457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}