{"id":3263,"date":"2022-07-09T14:51:13","date_gmt":"2022-07-09T11:51:13","guid":{"rendered":"https:\/\/demensdeum.com\/blog\/?p=3263"},"modified":"2024-12-16T22:32:18","modified_gmt":"2024-12-16T19:32:18","slug":"cocktail-shaker-sort","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/ru\/2022\/07\/09\/cocktail-shaker-sort\/","title":{"rendered":"Cocktail Shaker Sort"},"content":{"rendered":"<p>Cocktail Shaker Sort &#8211; \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432 \u0448\u0435\u0439\u043a\u0435\u0440\u0435, \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c.<br \/>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\u0412\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0432 \u0446\u0438\u043a\u043b\u0435 (\u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043b\u0435\u0432\u0430-\u043d\u0430\u043f\u0440\u0430\u0432\u043e)<\/li>\n<li>\u0414\u0430\u043b\u0435\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u043f\u043e\u043f\u0430\u0440\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0446\u0438\u0444\u0440\u044b<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u043e \u043e\u043d\u0438 \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043c\u0435\u0441\u0442\u0430\u043c\u0438<\/li>\n<li>\u041f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044e, \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043d\u043e\u0432\u043e \u0441 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0430\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438<\/li>\n<\/ol>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u043e\u0439 &#8211; <span class=\"ILfuVd\" lang=\"en\"><span class=\"hgKElc\">O(n<sup>2<\/sup>)<\/span><\/span>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 PHP:<\/p>\n<div class=\"hcb_wrap\">\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>&lt?php\n\nfunction cocktailShakeSort($numbers)\n{\n    echo implode(\",\", $numbers),\"\\n\";\n    $direction = false;\n    $sorted = false;\n    do {\n        $direction = !$direction;        \n        $firstIndex = $direction == true ? 0 : count($numbers) - 1;\n        $lastIndex = $direction == true ? count($numbers) - 1 : 0;\n        \n        $sorted = true;\n        for (\n            $i = $firstIndex;\n            $direction == true ? $i < $lastIndex : $i > $lastIndex;\n            $direction == true ? $i++ : $i--\n        ) {\n            $lhsIndex = $direction ? $i : $i - 1;\n            $rhsIndex = $direction ? $i + 1 : $i;\n\n            $lhs = $numbers[$lhsIndex];\n            $rhs = $numbers[$rhsIndex];\n\n            if ($lhs > $rhs) {\n                $numbers[$lhsIndex] = $rhs;\n                $numbers[$rhsIndex] = $lhs;\n                $sorted = false;\n            }\n        }\n    } while ($sorted == false);\n\n    echo implode(\",\", $numbers);\n}\n\n$numbers = [2, 1, 4, 3, 69, 35, 55, 7, 7, 2, 6, 203, 9];\ncocktailShakeSort($numbers);\n\n?><\/code><\/pre>\n<\/div>\n<h3>\u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/blob\/master\/sortAlgorithms\/cocktailShakerSort\/cocktailShakerSort.php\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/blob\/master\/sortAlgorithms\/cocktailShakerSort\/cocktailShakerSort.php<\/a><\/p>\n<h3>\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438<\/h3>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=njClLBoEbfI\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/watch?v=njClLBoEbfI<\/a><br \/>\n<a href=\"https:\/\/www.geeksforgeeks.org\/cocktail-sort\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.geeksforgeeks.org\/cocktail-sort\/<\/a><br \/>\n<a href=\"https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Cocktail_sort\" target=\"_blank\" rel=\"noopener\">https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Cocktail_sort<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cocktail Shaker Sort &#8211; \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432 \u0448\u0435\u0439\u043a\u0435\u0440\u0435, \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c.\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0432 \u0446\u0438\u043a\u043b\u0435 (\u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043b\u0435\u0432\u0430-\u043d\u0430\u043f\u0440\u0430\u0432\u043e) \u0414\u0430\u043b\u0435\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u043f\u043e\u043f\u0430\u0440\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0446\u0438\u0444\u0440\u044b \u0415\u0441\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u043e \u043e\u043d\u0438 \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u041f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044e, \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u043d\u043e\u0432\u043e \u0441 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0430\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/ru\/2022\/07\/09\/cocktail-shaker-sort\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Cocktail Shaker 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,195,190],"class_list":["post-3263","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-algorithms","tag-cocktail-shaker-sort","tag-sorting","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"ru","enabled_languages":["en","ru","zh","de","fr","ja","pt"],"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}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/posts\/3263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/comments?post=3263"}],"version-history":[{"count":30,"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/posts\/3263\/revisions"}],"predecessor-version":[{"id":3874,"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/posts\/3263\/revisions\/3874"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/media?parent=3263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/categories?post=3263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/ru\/wp-json\/wp\/v2\/tags?post=3263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}