{"id":68,"date":"2025-05-12T12:03:46","date_gmt":"2025-05-12T06:33:46","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=68"},"modified":"2025-07-07T19:50:00","modified_gmt":"2025-07-07T14:20:00","slug":"python-program-for-selection-sort-algorithm","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/","title":{"rendered":"Python Program for Selection Sort Algorithm"},"content":{"rendered":"\n<p>Hi everyone! In this article, we&#8217;ll guide you through the Python Program for Selection Sort Algorithm. With detailed examples, approach, code, it&#8217;s explanation &amp; dry run with a comprehensive complexity analysis.<\/p>\n\n\n\n<p>So let&#8217;s get started with the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/selection-sort\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>].<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-8080ee75-17a8-4a81-b1d0-4f59d0829c96\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#0-problem-statement\" style=\"\">Problem Statement:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#1-approach-for-selection-sort-in-python\" style=\"\">Approach for Selection Sort in Python:<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#2-key-steps-in-the-approach\" style=\"\">Key Steps in the Approach:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#3-code\" style=\"\">Code:<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#4-code-explanation\" style=\"\">Code Explanation:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#5-dry-run\" style=\"\">Dry Run:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-for-selection-sort-algorithm\/#6-complexity-analysis\" style=\"\">Complexity Analysis:<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-problem-statement\">Problem Statement:<\/h2>\n\n\n\n<p>Given an unsorted array of size &#8220;N&#8221;, use selection sort to sort &#8220;arr[]&#8221; in increasing order.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Example 1:\n\n  Input:   N = 5\n           arr[] = {4, 1, 3, 9, 7}\n  Output:  1 3 4 7 9\n\n  Explanation:  Maintain sorted (in bold) and unsorted subarrays.\n                Select 1. Array becomes 1 4 3 9 7.\n                Select 3. Array becomes 1 3 4 9 7.\n                Select 4. Array becomes 1 3 4 9 7.\n                Select 7. Array becomes 1 3 4 7 9.\n                Select 9. Array becomes 1 3 4 7 9.\n\nExample 2:\n\n  Input:  N = 10\n          arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}\n  Output: 1 2 3 4 5 6 7 8 9 10\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input:   N = <\/span><span style=\"color: #B5CEA8\">5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">           arr[] = {<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output:  <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation:  Maintain <\/span><span style=\"color: #DCDCAA\">sorted<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> bold) <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> unsorted subarrays.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                Select <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">. Array becomes <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                Select <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">. Array becomes <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                Select <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">. Array becomes <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                Select <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">. Array becomes <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                Select <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">. Array becomes <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input:  N = <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">          arr[] = {<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">8<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">9<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Your Task:&nbsp;<\/strong>You don&#8217;t need to read input or print anything. Complete the functions <strong>&#8220;select()&#8221; <\/strong>and <strong>&#8220;selectionSort()&#8221;<\/strong> ,where &#8220;select()&#8221; takes &#8220;arr[]&#8221; and starting point of unsorted array &#8220;i&#8221; as input parameters and returns the selected element for each iteration in selection sort, and &#8220;selectionSort()&#8221; takes the array and it&#8217;s size and sorts the array.<\/p>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong> O(N<sup>2<\/sup>)<\/p>\n\n\n\n<p><strong>Expected Auxiliary Space:<\/strong> O(1)<\/p>\n\n\n\n<p><em><strong>Constraints:<\/strong> 1 \u2264 N \u2264 10<sup>3<\/sup><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-approach-for-selection-sort-in-python\">Approach for Selection Sort in Python:<\/h2>\n\n\n\n<p>The <strong>&#8220;Selection Sort&#8221;<\/strong> algorithm works by dividing the array into two parts: the sorted and unsorted portions. It repeatedly selects the smallest element from the unsorted portion and swaps it with the first element of that unsorted section, effectively growing the sorted portion one element at a time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-key-steps-in-the-approach\">Key Steps in the Approach:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start with the first element as the current minimum<\/li>\n\n\n\n<li>Compare it with every other element in the unsorted portion of the array<\/li>\n\n\n\n<li>If a smaller element is found, update the current minimum index<\/li>\n\n\n\n<li>Swap the minimum element with the first element of the unsorted portion<\/li>\n\n\n\n<li>Move the boundary of the sorted and unsorted portions by one and repeat until the entire array is sorted<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Read about the Python Program to <strong><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Reverse Array Using Recursion and While Loop<\/span><\/mark><\/a><\/strong>.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-code\">Code:<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def select(self, arr, i):\n        n = len(arr)\n        for i in range(0, n):\n            min_index = i\n            for j in range(i + 1, n):\n                if arr[j] &lt; arr[min_index]:\n                    min_index = j\n            arr[i], arr[min_index] = arr[min_index], arr[i]\n\n    def selectionSort(self, arr):\n        self.select(arr, 0)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">select<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">i<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            min_index = i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> j <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[j] &lt; arr[min_index]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    min_index = j<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr[i], arr[min_index] = arr[min_index], arr[i]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">selectionSort<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.select(arr, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Outer Loop (for &#8220;i&#8221; in range(0, n))<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This loop iterates through the entire array<\/li>\n\n\n\n<li>At each iteration, it considers the element at index &#8220;i&#8221; as the current minimum<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Initialization of &#8220;min_index&#8221;<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The variable &#8220;min_index&#8221; stores the index of the smallest element in the unsorted portion of the array<\/li>\n\n\n\n<li>Initially, &#8220;min_index&#8221; is set to &#8220;i&#8221;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Inner Loop (for &#8220;j&#8221; in range(i + 1, n))<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This loop iterates through the unsorted portion of the array, starting from i + 1<\/li>\n\n\n\n<li>If a smaller element than the current minimum is found (arr[j] &lt; arr[min_index]), the &#8220;min_index&#8221; is updated to &#8220;j&#8221;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Swapping (<\/strong><strong>arr[i], arr[min_index] = arr[min_index], arr[i]<\/strong><strong>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After finding the smallest element in the unsorted portion, it is swapped with the element at index &#8220;i&#8221;<\/li>\n\n\n\n<li>This ensures that the element at index &#8220;i&#8221; is the smallest in the remaining array<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Repeat Until Sorted<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The boundary of the sorted and unsorted portions of the array moves forward with each iteration, and the process is repeated until the array is completely sorted<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run:<\/h2>\n\n\n\n<p>Let\u2019s see an example below:<\/p>\n\n\n\n<p><img decoding=\"async\" style=\"\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeb1QR_msybcTwUK62_-18nLEd7lUOxz4vgayGjlFpv3UguzMuI2M17sMeCBywdbgC0OGcHU9KPs5ws7mFvHCFaFpeMjNRgyi2xlWtLkC2OtJR8gyGUvLzRvB33WfvhViVHWKHmrA?key=jtAdDVnKTVeONYzTnhS-w-ps\" alt=\"Dry Run of the Python Program for Selection Sort Algorithm\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-1024x576.png\" alt=\"\" class=\"wp-image-69\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-selection-sort.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-complexity-analysis\">Complexity Analysis:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Outer Loop<\/strong>: Runs <strong><em>n<\/em><\/strong> times<\/li>\n\n\n\n<li><strong>Inner Loop<\/strong>: Runs n\u22121, n\u22122, &#8230;, 1 times<\/li>\n\n\n\n<li>Total comparisons: n \u00d7 (n\u22121)\/2 = <strong>O(n<sup>2<\/sup>)<\/strong><\/li>\n\n\n\n<li><strong>Worst Case<\/strong>: <strong>O(n<sup>2<\/sup>)<\/strong>, when the array is in reverse order<\/li>\n\n\n\n<li><strong>Best Case<\/strong>: <strong>O(n<sup>2<\/sup>)<\/strong>, as no early exit optimization is included<\/li>\n\n\n\n<li><strong>Average Case<\/strong>: <strong>O(n<\/strong><strong><sup>2<\/sup><\/strong><strong>)<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The algorithm operates <strong>in-place<\/strong>, so no additional memory is required apart from a few variables<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>: O(1)<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">enrolL in our free dsa course<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone! In this article, we&#8217;ll guide you through the Python Program for Selection Sort Algorithm. With detailed examples, approach, code, it&#8217;s explanation &amp; dry run with a comprehensive complexity analysis. So let&#8217;s get started with the [Problem Link]. Problem Statement: Given an unsorted array of size &#8220;N&#8221;, use selection sort to sort &#8220;arr[]&#8221; in<\/p>\n","protected":false},"author":1,"featured_media":70,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,12],"class_list":{"0":"post-68","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-sorting-algos"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Python-Program-for-Selection-Sort-Algorithm.webp","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions\/538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/70"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}