{"id":3221,"date":"2022-06-29T02:16:02","date_gmt":"2022-06-28T23:16:02","guid":{"rendered":"https:\/\/demensdeum.com\/blog\/?p=3221"},"modified":"2024-12-16T22:32:19","modified_gmt":"2024-12-16T19:32:19","slug":"stalin-sort","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2022\/06\/29\/stalin-sort\/","title":{"rendered":"Stalin Sort"},"content":{"rendered":"<p>Stalin Sort &#8211; sorting through, one of the algorithms of sorting with data loss.<br \/>The algorithm is very <em>productive<\/em> and <strong>efficient<\/strong>, time complexity is O(n).<\/p>\n<p>It works like this:<\/p>\n<ol>\n<li>We loop through the array, comparing the current element with the next one<\/li>\n<li>If the next element is less than the current one, then delete it<\/li>\n<li>As a result, we get a sorted array in O(n)<\/li>\n<\/ol>\n<p>Example of the algorithm output:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>Gulag: [1, 3, 2, 4, 6, 42, 4, 8, 5, 0, 35, 10]\nElement 2 sent to Gulag\nElement 4 sent to Gulag\nElement 8 sent to Gulag\nElement 5 sent to Gulag\nElement 0 sent to Gulag\nElement 35 sent to Gulag\nElement 10 sent to Gulag\nNumbers: [1, 3, 4, 6, 42]\nGulag: [2, 4, 8, 5, 0, 35, 10]\n<\/code><\/pre>\n<\/div>\n<p>Python 3 code:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>gulag = []\n\nprint(f\"Numbers: {numbers}\")\nprint(f\"Gulag: {numbers}\")\n\ni = 0\nmaximal = numbers[0]\n\nwhile i < len(numbers):\n    element = numbers[i]\n    if maximal > element:\n        print(f\"Element {element} sent to Gulag\")\n        gulag.append(element)\n        del numbers[i]\n    else:\n        maximal = element        \n        i += 1\n\nprint(f\"Numbers: {numbers}\")\nprint(f\"Gulag: {gulag}\")\n<\/code><\/pre>\n<\/div>\n<p>Among the disadvantages, one can note the loss of data, but if we move towards a utopian, ideal, sorted list in O(n), then how else?<\/p>\n<h3>Links<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/stalinSort\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum \/algorithms\/-\/tree\/master\/sortAlgorithms\/stalinSort<\/a> <\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/github.com\/gustavo-depaula\/stalin-sort\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/gustavo-depaula\/stalin-sort <\/a><br \/><a href=\"https:\/\/www.youtube.com\/shorts\/juRL-Xn-E00\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/shorts\/juRL-Xn-E00<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=L78i2YcyYfk\" rel=\"noopener\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=L78i2YcyYfk<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stalin Sort &#8211; sorting through, one of the algorithms of sorting with data loss.The algorithm is very productive and efficient, time complexity is O(n). It works like this: We loop through the array, comparing the current element with the next one If the next element is less than the current one, then delete it As<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2022\/06\/29\/stalin-sort\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Stalin 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,190,193],"class_list":["post-3221","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-algorithms","tag-sorting","tag-stalin-sort","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","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\/wp-json\/wp\/v2\/posts\/3221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/comments?post=3221"}],"version-history":[{"count":9,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/3221\/revisions"}],"predecessor-version":[{"id":3877,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/3221\/revisions\/3877"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=3221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=3221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=3221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}