Jul 16, 2021
Selection Sort
Finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
Best,Worst and Average Case Time Complexity: O(n²).
Auxiliary Space: O(1)
Example
public class MyClass
{
public static void main(String args[])
{
int[] values = {5,4,3,1,2};
int n = values.length;
for(int i=0;i<n-1;i++)
{
int minindexx = i;
for(int j = i+1;j<n;j++)
if(values[j]<values[minindexx])
minindexx = j;
int temp = values[minindexx];
values[minindexx] = values[i];
values[i] = temp;
}
System.out.println("Sorted:"+Arrays.toString(values));
}
}