{"id":25046,"date":"2017-10-15T14:08:54","date_gmt":"2017-10-15T08:38:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25046"},"modified":"2017-10-15T14:08:54","modified_gmt":"2017-10-15T08:38:54","slug":"selection-sort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/selection-sort\/","title":{"rendered":"Selection Sort"},"content":{"rendered":"<p>The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) <span id=\"more-142312\"><\/span>from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.<\/p>\n<p>1) The subarray which is already sorted.<br \/>\n2) Remaining subarray which is unsorted.<\/p>\n<p>In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.<\/p>\n<p>Following example explains the above steps:<\/p>\n<p>arr[] = 64 25 12 22 11<\/p>\n<p>\/\/ Find the minimum element in arr[0&#8230;4]\n<p>\/\/ and place it at beginning <strong>11 <\/strong>25 12 22 64<\/p>\n<p>\/\/ Find the minimum element in arr[1&#8230;4]\n<p>\/\/ and place it at beginning of arr[1&#8230;4] 11 <strong>12 <\/strong>25 22 64<\/p>\n<p>\/\/ Find the minimum element in arr[2&#8230;4]\n<p>\/\/ and place it at beginning of arr[2&#8230;4] 11 12 <strong>22 <\/strong>25 64<\/p>\n<p>\/\/ Find the minimum element in arr[3&#8230;4]\n<p>\/\/ and place it at beginning of arr[3&#8230;4] 11 12 22 <strong>25 <\/strong>64<\/p>\n<p>C and C++<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c and c++<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program for implementation of selection sort<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>void swap(int *xp, int *yp)<br\/>{<br\/>    int temp = *xp;<br\/>    *xp = *yp;<br\/>    *yp = temp;<br\/>}<br\/> <br\/>void selectionSort(int arr[], int n)<br\/>{<br\/>    int i, j, min_idx;<br\/> <br\/>    \/\/ One by one move boundary of unsorted subarray<br\/>    for (i = 0; i &lt; n-1; i++)<br\/>    {<br\/>        \/\/ Find the minimum element in unsorted array<br\/>        min_idx = i;<br\/>        for (j = i+1; j &lt; n; j++)<br\/>          if (arr[j] &lt; arr[min_idx])<br\/>            min_idx = j;<br\/> <br\/>        \/\/ Swap the found minimum element with the first element<br\/>        swap(&amp;arr[min_idx], &amp;arr[i]);<br\/>    }<br\/>}<br\/> <br\/>\/* Function to print an array *\/<br\/>void printArray(int arr[], int size)<br\/>{<br\/>    int i;<br\/>    for (i=0; i &lt; size; i++)<br\/>        printf(&quot;%d &quot;, arr[i]);<br\/>    printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    int arr[] = {64, 25, 12, 22, 11};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    selectionSort(arr, n);<br\/>    printf(&quot;Sorted array: \\n&quot;);<br\/>    printArray(arr, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Python<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program for implementation of Selection<br\/># Sort<br\/>import sys<br\/>A = [64, 25, 12, 22, 11]<br\/> <br\/># Traverse through all array elements<br\/>for i in range(len(A)):<br\/>     <br\/>    # Find the minimum element in remaining <br\/>    # unsorted array<br\/>    min_idx = i<br\/>    for j in range(i+1, len(A)):<br\/>        if A[min_idx] &gt; A[j]:<br\/>            min_idx = j<br\/>             <br\/>    # Swap the found minimum element with <br\/>    # the first element        <br\/>    A[i], A[min_idx] = A[min_idx], A[i]<br\/> <br\/># Driver code to test above<br\/>print (&quot;Sorted array&quot;)<br\/>for i in range(len(A)):<br\/>    print(&quot;%d&quot; %A[i]), <\/code><\/pre> <\/div>\n<p>Java<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program for implementation of Selection Sort<br\/>class SelectionSort<br\/>{<br\/>    void sort(int arr[])<br\/>    {<br\/>        int n = arr.length;<br\/> <br\/>        \/\/ One by one move boundary of unsorted subarray<br\/>        for (int i = 0; i &lt; n-1; i++)<br\/>        {<br\/>            \/\/ Find the minimum element in unsorted array<br\/>            int min_idx = i;<br\/>            for (int j = i+1; j &lt; n; j++)<br\/>                if (arr[j] &lt; arr[min_idx])<br\/>                    min_idx = j;<br\/> <br\/>            \/\/ Swap the found minimum element with the first<br\/>            \/\/ element<br\/>            int temp = arr[min_idx];<br\/>            arr[min_idx] = arr[i];<br\/>            arr[i] = temp;<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ Prints the array<br\/>    void printArray(int arr[])<br\/>    {<br\/>        int n = arr.length;<br\/>        for (int i=0; i&lt;n; ++i)<br\/>            System.out.print(arr[i]+&quot; &quot;);<br\/>        System.out.println();<br\/>    }<br\/> <br\/>    \/\/ Driver code to test above<br\/>    public static void main(String args[])<br\/>    {<br\/>        SelectionSort ob = new SelectionSort();<br\/>        int arr[] = {64,25,12,22,11};<br\/>        ob.sort(arr);<br\/>        System.out.println(&quot;Sorted array&quot;);<br\/>        ob.printArray(arr);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<p>Sorted array: 11 12 22 25 64<\/p>\n<p><strong>Time Complexity:<\/strong> O(n<sup>2<\/sup>) as there are two nested loops.<\/p>\n<p><strong>Auxiliary Space:<\/strong> O(1)<\/p>\n<p>The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selection Sort &#8211; searching and sorting algorithm &#8211; The selection sort algorithm sorts an array by repeatedly finding the minimum element .<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,71670,83471],"tags":[70962,70963,70978,70975,70972,70260,70966,70979,70976,70969,70968,70977,70971,70974,70964,70539,70961,70973,70548,70960,70965,70970,70541,70967,70257],"class_list":["post-25046","post","type-post","status-publish","format-standard","hentry","category-coding","category-searching-and-sorting","category-selection-sort","tag-algorithm-for-selection-sort","tag-algorithm-of-selection-sort","tag-bubble-sort-c","tag-bubble-sort-example","tag-c-program-for-selection-sort","tag-complexity-of-selection-sort","tag-insert-sort","tag-insertion-sort-c","tag-insertion-sort-example","tag-java-sorting-algorithms","tag-program-for-selection-sort","tag-quicksort-java","tag-selection-sort-algorithm-in-c","tag-selection-sort-c-program","tag-selection-sort-code","tag-selection-sort-complexity","tag-selection-sort-example","tag-selection-sort-in-c-program","tag-selection-sort-in-data-structure","tag-selection-sort-java","tag-selection-sort-program","tag-selection-sort-program-in-c","tag-selection-sort-time-complexity","tag-sorting-algorithms-java","tag-time-complexity-of-selection-sort"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25046"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25046\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}