Site icon C1CTech

Selection Sort(Java Implementation)

<p><strong><span style&equals;"color&colon; &num;008000&semi;">Selection sort<&sol;span><&sol;strong> is a simple sorting algorithm&period; It sorts an array by repeatedly finding the minimum element &lpar;considering ascending order&rpar; from unsorted part and putting it at the beginning&period; The algorithm maintains two subarrays in a given array&period;<&sol;p>&NewLine;<p>1&rpar; The subarray which is already sorted&period;<br &sol;>&NewLine;2&rpar; Remaining subarray which is unsorted&period;<&sol;p>&NewLine;<p>Initially&comma; the sorted part is empty and the unsorted part is the entire list&period;<&sol;p>&NewLine;<p>In every iteration of selection sort&comma; the minimum element &lpar;considering ascending order&rpar; from the unsorted subarray is picked and moved to the sorted subarray&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">How Selection Sort Works&quest;<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">arr&lbrack;&rsqb; &equals; 66 24 16 20 17<&sol;span><&sol;strong>&NewLine;&NewLine;<strong>&sol;&sol; Find the minimum element in arr&lbrack;0&period;&period;&period;4&rsqb;&NewLine;&sol;&sol; and place it at beginning&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">16 <&sol;span>24 66 20 17&NewLine;&NewLine;&sol;&sol; Find the minimum element in arr&lbrack;1&period;&period;&period;4&rsqb;&NewLine;&sol;&sol; and place it at beginning of arr&lbrack;1&period;&period;&period;4&rsqb;&NewLine;16 <span style&equals;"color&colon; &num;0000ff&semi;">17 <&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000000&semi;">66<&sol;span><&sol;span> 20 24&NewLine;&NewLine;&sol;&sol; Find the minimum element in arr&lbrack;2&period;&period;&period;4&rsqb;&NewLine;&sol;&sol; and place it at beginning of arr&lbrack;2&period;&period;&period;4&rsqb;&NewLine;16 17 <span style&equals;"color&colon; &num;0000ff&semi;">20<&sol;span> 66 24&NewLine;&NewLine;&sol;&sol; Find the minimum element in arr&lbrack;3&period;&period;&period;4&rsqb;&NewLine;&sol;&sol; and place it at beginning of arr&lbrack;3&period;&period;&period;4&rsqb;&NewLine;16 17 <span style&equals;"color&colon; &num;000000&semi;">20 <&sol;span><span style&equals;"color&colon; &num;0000ff&semi;">24 <&sol;span>66<&sol;strong><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Algorithm<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li class&equals;"result notranslate"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Step 1<&sol;b><&sol;span> − Set SMALL to location 0<&sol;li>&NewLine;<li class&equals;"result notranslate"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Step 2<&sol;b><&sol;span> − Search the minimum element in the list<&sol;li>&NewLine;<li class&equals;"result notranslate"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Step 3<&sol;b><&sol;span> − Swap with value at location SMALL<&sol;li>&NewLine;<li class&equals;"result notranslate"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Step 4<&sol;b><&sol;span> − Increment SMALL to point to next element<&sol;li>&NewLine;<li class&equals;"result notranslate"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Step 5<&sol;b><&sol;span> − Repeat until list is sorted<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Let’s take a look at the implementation&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre style&equals;"padding-left&colon; 60px&semi;"><code><strong>public class Sort &lbrace;<&sol;strong>&NewLine;<strong>&NewLine;public static void selectionSort&lpar;int arr&lbrack;&rsqb;&comma;int small&comma;int pos&rpar; &lbrace;<&sol;strong>&NewLine;<strong>&NewLine;int temp&semi;<&sol;strong>&NewLine;<strong>&NewLine; for&lpar;int i&equals;0&semi;i&lt&semi;&equals;arr&period;length-2&semi;i&plus;&plus;&rpar;<&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;set current element as minimum or small<&sol;span>&NewLine; &lbrace;<&sol;strong>&NewLine;<strong>&NewLine; small &equals; arr&lbrack;i&rsqb;&semi;<&sol;strong>&NewLine;<strong>&NewLine; pos &equals; i&semi;<&sol;strong>&NewLine;<strong>&NewLine; for&lpar;int j&equals;i&plus;1&semi;j&lt&semi;&equals;arr&period;length-1&semi;j&plus;&plus;&rpar;&NewLine;<&sol;strong><strong> &lbrace;<&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;check the element to be small&NewLine;<&sol;span> if&lpar;arr&lbrack;j&rsqb;&lt&semi;small&rpar;<&sol;strong><strong>&lbrace;<&sol;strong>&NewLine;<strong>&NewLine; small &equals; arr&lbrack;j&rsqb;&semi;<&sol;strong>&NewLine;<strong>&NewLine; pos &equals; j&semi;<&sol;strong>&NewLine;<strong>&NewLine; &rcub;<&sol;strong>&NewLine;<strong>&NewLine; &rcub;<&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;swap the minimum element with the current element&NewLine;<&sol;span> temp&equals;arr&lbrack;i&rsqb;&semi;<&sol;strong>&NewLine;<strong>&NewLine; arr&lbrack;i&rsqb;&equals;arr&lbrack;pos&rsqb;&semi;<&sol;strong>&NewLine;<strong>&NewLine; arr&lbrack;pos&rsqb;&equals;temp&semi;<&sol;strong>&NewLine;<strong>&NewLine; &rcub;<&sol;strong>&NewLine;<strong>&NewLine;&rcub;&NewLine;&NewLine;public static void main&lpar;String&lbrack;&rsqb; strr&rpar; &lbrace;&NewLine;&NewLine;int small&comma; pos&semi;&NewLine;&NewLine;int arr&lbrack;&rsqb; &equals; &lbrace;3&comma; 2&comma; 10&comma; 17&comma; 13&comma; 9&comma; 330&comma; 42&comma; 21&comma; 90&comma; 100&rcub;&semi;&NewLine;&NewLine;small &equals; arr&lbrack;0&rsqb;&semi;&NewLine;&NewLine;pos &equals; 0&semi;&NewLine;&NewLine;System&period;out&period;println&lpar;" Before sorting"&rpar;&semi;&NewLine;&NewLine;for&lpar;int j&equals;0&semi;j&lt&semi;&equals;arr&period;length-1&semi;j&plus;&plus;&rpar;&NewLine;&NewLine;&lbrace;&NewLine;&NewLine;System&period;out&period;print&lpar;" "&plus;arr&lbrack;j&rsqb;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;selectionSort&lpar;arr&comma;small&comma;pos&rpar;&semi;&NewLine;&NewLine;System&period;out&period;println&lpar;"&bsol;nAfter Sorting"&rpar;&semi;&NewLine;&NewLine;for&lpar;int j&equals;0&semi;j&lt&semi;&equals;arr&period;length-1&semi;j&plus;&plus;&rpar;&NewLine;&NewLine;&lbrace;&NewLine;&NewLine;System&period;out&period;print&lpar;" "&plus;arr&lbrack;j&rsqb;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;&rcub;<&sol;strong>&NewLine;<strong>&NewLine;&rcub;<&sol;strong>&NewLine;<&sol;code><&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>output&colon;<&sol;strong> <&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Before sorting 3 2 10 17 13 9 330 42 21 90 100 <&sol;span><&sol;strong><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">After Sorting 2 3 9 10 13 17 21 42 90 100 330<&sol;span><&sol;strong><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Time Complexity&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The time complexity of selection sort is <span style&equals;"color&colon; &num;008000&semi;"><strong>O&lpar;<i>n<&sol;i><sup>2<&sol;sup>&rpar;<&sol;strong><&sol;span> as there are two nested loops&period; One loop is for comparisons and another loop is for swapping  between the elements&period;&NewLine;

Exit mobile version