{"id":340,"date":"2020-12-01T16:11:46","date_gmt":"2020-12-01T16:11:46","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/implement-selection-sort-in-php\/"},"modified":"2024-10-28T04:22:25","modified_gmt":"2024-10-28T04:22:25","slug":"implement-selection-sort-in-php","status":"publish","type":"programming_with_php","link":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/implement-selection-sort-in-php\/","title":{"rendered":"Implement Selection Sort in PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The outer loop needs to continue iterating from the index 0 to n &#8211; 1( here, n is the number of items). The inner loop starts from the current index + 1 of the outer loop to the end of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">function selection_sort( $data_array ){\n\tfor( $i=0; $i &lt; count( $data_array ) - 1; $i++ ){\n\t\t$temp = $data_array[$i];\n\t\t$min_index = $i;\n\t\tfor( $j=$i+1; $j &lt; count( $data_array ); $j++ ){\n\t\t\tif( $data_array[$j] &lt; $data_array[$min_index] ){\n\t\t\t\t$min_index = $j;\n\t\t\t}\n\t\t}\n\t\tif( $i != $min_index ){\n\t\t\t$data_array[$i] = $data_array[$min_index];\n\t\t\t$data_array[$min_index] = $temp;\n\t\t}\n\t}\n\treturn $data_array;\n}\n\necho '&lt;h1>Before Sort&lt;\/h1>';\necho '&lt;pre>';\nprint_r($data_array);\necho '&lt;\/pre>';\necho \"&lt;br>\";\necho '&lt;h1>After Sort&lt;\/h1>';\n$sorted_items = selection_sort( $data_array ); \necho '&lt;pre>';\nprint_r($sorted_items);\necho '&lt;\/pre>';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full \\&quot;wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"506\" src=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/12\/selection-sort-1.png\" alt=\"\" class=\"wp-image-2253\" srcset=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/12\/selection-sort-1.png 188w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2020\/12\/selection-sort-1-111x300.png 111w\" sizes=\"auto, (max-width: 188px) 100vw, 188px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>We need to use two &#8216;for loop&#8217; for the selection sort algorithm. The complexity of this algorithm is O(n^2). Check the complete code below:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"class_list":["post-340","programming_with_php","type-programming_with_php","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"We need to use two 'for loop' for the selection sort algorithm. The complexity of this algorithm is O(n^2). Check the complete code below:","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/programming_with_php"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=340"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}