{"id":351,"date":"2021-01-09T14:27:23","date_gmt":"2021-01-09T14:27:23","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/implement-insertion-sort-in-php\/"},"modified":"2024-10-28T03:52:53","modified_gmt":"2024-10-28T03:52:53","slug":"implement-insertion-sort-in-php","status":"publish","type":"programming_with_php","link":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/implement-insertion-sort-in-php\/","title":{"rendered":"Implement Insertion Sort in PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start Iterate from data_array[1] to data_array[n].<\/li>\n\n\n\n<li>Compare the current element to its predecessor.<\/li>\n\n\n\n<li>If the current element is smaller than its predecessor, move the predecessor elements to current elements key until the current element is greater then predecessor element and place the current element in its appropriate position.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$data_array = [1,3,44,22,38,5,47,15,36,26,27,12];\n\nfunction insertion_sort( $data_array ){\n\tfor( $i=1; $i &lt; count( $data_array ); $i++ ){\n\t\t$item = $data_array[$i];\n\t\t$j = $i - 1;\n\t\twhile( $j >= 0 &amp;&amp; $data_array[$j] > $item ){\n\t\t\t$data_array[$j+1] = $data_array[$j];\n\t\t\t$j--;\n\t\t}\n\t\t$data_array[$j + 1] = $item;\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 = insertion_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=\"221\" height=\"618\" src=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2021\/01\/insertion-sort_php-1.png\" alt=\"\" class=\"wp-image-2247\" srcset=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2021\/01\/insertion-sort_php-1.png 221w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2021\/01\/insertion-sort_php-1-107x300.png 107w\" sizes=\"auto, (max-width: 221px) 100vw, 221px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Insertion sort is a sorting algorithm like bubble sort and selection sort. In practical life, we can use insertion sort. It&#8217;s worst-case complexity 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-351","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":"Insertion sort is a sorting algorithm like bubble sort and selection sort. In practical life, we can use insertion sort. It's worst-case complexity O(n^2). Check the complete code below:","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php\/351","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=351"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}