{"id":3206,"date":"2022-06-27T22:30:32","date_gmt":"2022-06-27T19:30:32","guid":{"rendered":"https:\/\/demensdeum.com\/blog\/?p=3206"},"modified":"2024-12-16T22:32:19","modified_gmt":"2024-12-16T19:32:19","slug":"selection-sort","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2022\/06\/27\/selection-sort\/","title":{"rendered":"Selection Sort"},"content":{"rendered":"<p>Selection Sort &#8211; selection sorting algorithm. Selection of what? The minimum number!!!<br \/>The time complexity of the algorithm is <strong>O(<i>n<\/i><sup>2<\/sup>)<\/strong><\/p>\n<p>The algorithm works as follows:<\/p>\n<ol>\n<li>We go through the array in a loop from left to right, remember the current starting index and the number by index, we will call the number A<\/li>\n<li>Inside the loop, we start another one to go from left to right in search of something smaller than A<\/li>\n<li>When we find the smaller one, we remember the index, now the smaller one becomes the number A<\/li>\n<li>When the inner loop ends, swap the number at the starting index with the number A<\/li>\n<li>After the full pass of the upper loop, we get a sorted array<\/li>\n<\/ol>\n<p>Example of algorithm execution:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>(29, 49, 66, 35, 7, 12, 80)\n29 &gt; 7\n(7, 49, 66, 35, 29, 12, 80)\nRound 1 ENDED\nRound 2\n(7, 49, 66, 35, 29, 12, 80)\n49 &gt; 35\n35 &gt; 29\n29 &gt; 12\n(7, 12, 66, 35, 29, 49, 80)\nRound 2 ENDED\nRound 3\n(7, 12, 66, 35, 29, 49, 80)\n66 &gt; 35\n35 &gt; 29\n(7, 12, 29, 35, 66, 49, 80)\nRound 3 ENDED\nRound 4\n(7, 12, 29, 35, 66, 49, 80)\n(7, 12, 29, 35, 66, 49, 80)\nRound 4 ENDED\nRound 5\n(7, 12, 29, 35, 66, 49, 80)\n66 &gt; 49\n(7, 12, 29, 35, 49, 66, 80)\nRound 5 ENDED\nRound 6\n(7, 12, 29, 35, 49, 66, 80)\n(7, 12, 29, 35, 49, 66, 80)\nRound 6 ENDED\nSorted: (7, 12, 29, 35, 49, 66, 80)\n<\/code><\/pre>\n<\/div>\n<p>Having failed to find an Objective-C implementation on <a href=\"https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Selection_sort#C\" target=\"_blank\" rel=\"noopener\">Rosetta Code<\/a>, I wrote it myself:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>#include &lt;Foundation\/Foundation.h&gt;\n\n@implementation SelectionSort\n- (void)performSort:(NSMutableArray *)numbers\n{\n   NSLog(@\"%@\", numbers);   \n   for (int startIndex = 0; startIndex &lt; numbers.count-1; startIndex++) {\n      int minimalNumberIndex = startIndex;\n      for (int i = startIndex + 1; i &lt; numbers.count; i++) {\n         id lhs = [numbers objectAtIndex: minimalNumberIndex];\n         id rhs = [numbers objectAtIndex: i];\n         if ([lhs isGreaterThan: rhs]) {\n            minimalNumberIndex = i;\n         }\n      }\n      id temporary = [numbers objectAtIndex: minimalNumberIndex];\n      [numbers setObject: [numbers objectAtIndex: startIndex] \n               atIndexedSubscript: minimalNumberIndex];\n      [numbers setObject: temporary\n               atIndexedSubscript: startIndex];\n   }\n   NSLog(@\"%@\", numbers);\n}\n\n@end <\/code><\/pre>\n<p>\u0421\u043e\u0431\u0440\u0430\u0442\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u043d\u0430 MacOS\/Xcode, \u043b\u0438\u0431\u043e \u043d\u0430 \u043b\u044e\u0431\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0435\u0439 GNUstep, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0443 \u043c\u0435\u043d\u044f \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f Clang \u043d\u0430 Arch Linux.<br \/>\n\u0421\u043a\u0440\u0438\u043f\u0442 \u0441\u0431\u043e\u0440\u043a\u0438:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>        main.m \\\n        -lobjc \\\n        `gnustep-config --objc-flags` \\\n        `gnustep-config --objc-libs` \\\n        -I \/usr\/include\/GNUstepBase \\\n        -I \/usr\/lib\/gcc\/x86_64-pc-linux-gnu\/12.1.0\/include\/ \\\n        -lgnustep-base \\\n        -o SelectionSort \\<\/code><\/pre>\n<h3>Links<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/selectionSort\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/selectionSort<\/a><\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Selection_sort\" target=\"_blank\" rel=\"noopener\">https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Selection_sort<\/a><br \/>\n<a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%B2%D1%8B%D0%B1%D0%BE%D1%80%D0%BE%D0%BC\">https:\/\/ru.wikipedia.org\/wiki\/\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430_\u0432\u044b\u0431\u043e\u0440\u043e\u043c<\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Selection_sort\" target=\"_blank\" rel=\"noopener\">https:\/\/en.wikipedia.org\/wiki\/Selection_sort<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=LJ7GYbX7qpM\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/watch?v=LJ7GYbX7qpM<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selection Sort &#8211; selection sorting algorithm. Selection of what? The minimum number!!!The time complexity of the algorithm is O(n2) The algorithm works as follows: We go through the array in a loop from left to right, remember the current starting index and the number by index, we will call the number A Inside the loop,<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2022\/06\/27\/selection-sort\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Selection 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,192,190],"class_list":["post-3206","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-algorithms","tag-selection-sort","tag-sorting","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","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\/wp-json\/wp\/v2\/posts\/3206","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=3206"}],"version-history":[{"count":14,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/3206\/revisions"}],"predecessor-version":[{"id":3878,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/3206\/revisions\/3878"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=3206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=3206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=3206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}