2. The selection sort is a combination of searching
and sorting.
In selection sort, sorting is done after selecting a
particular smallest or largest element from an
array and shifted it to a particular location in an
array.
During each pass, the unsorted element with the
smallest (or largest) value is moved to its proper
position in the array.
Let's look at our same table of elements using a
selection sort for ascending order:
3. Array 40 50 60 10 30 20
1-pass 10 50 60 40 30 20
2-pass 10 20 60 40 30 50
3-pass 10 20 30 40 60 50
4-pass 10 20 30 40 60 50
5-pass 10 20 30 40 50 60
The number of times the sort passes through the
array is one less than the number of items in the
array.
In the selection sort, the inner loop finds the next
smallest (or largest) value and the outer loop
places that value into its proper location.
Lets watch a video which will give you a basic idea
of a selection sort.
Please co-operate.
4. Lets look at coding of it in c++:
#include<iostream>
using namespace std;
void selectionsort(int *array,int length)
{
int i,j,min,minat,temp;
for(i=0;i<(length-1);i++)
{
minat=i;
min=array[i];
for(j=0;j<length;j++)
{
if(min>array[j])
{
minat=j;
min=array[j];
}
}
temp=array[i];
array[i]=array[minat];
array[minat]=temp;
}
}