A selection sort is one in which successive elements are selected in order and placed into their proper sorted positions. The elements of the input may have to be preprocessed to make the ordered selection possible.
Selection sort consists entirely of a selection phase in which the largest of the remaining elements,large, is repeatedly placed at its proper position i, at the end of the array.
Below is the working procedure code.
void SelectionSort(int x[], int n)
{
int i,indx,j,large;
for(i=n-1;i>0;i--)
{
large = x[0];
indx = 0;
for(j=1;j<=i;j++)
if(x[j]>large)
{
large = x[j];
indx = j;
}
x[indx] = x[i];
x[i] = large;
}
The above procedure will sort the array in ascending order.
Consider the elements of array are
25 57 48 37 12
Here we will see the state of array after each iteration.
iteration 0 25 57 48 37 12
iteration 1 25 12 48 37 57
iteration 2 25 12 37 48 57
iteration 3 25 12 37 48 57
iteration 4 12 25 37 48 57
Average time complexity of selection sort is O(n2).
Selection sort consists entirely of a selection phase in which the largest of the remaining elements,large, is repeatedly placed at its proper position i, at the end of the array.
Below is the working procedure code.
void SelectionSort(int x[], int n)
{
int i,indx,j,large;
for(i=n-1;i>0;i--)
{
large = x[0];
indx = 0;
for(j=1;j<=i;j++)
if(x[j]>large)
{
large = x[j];
indx = j;
}
x[indx] = x[i];
x[i] = large;
}
The above procedure will sort the array in ascending order.
Consider the elements of array are
25 57 48 37 12
Here we will see the state of array after each iteration.
iteration 0 25 57 48 37 12
iteration 1 25 12 48 37 57
iteration 2 25 12 37 48 57
iteration 3 25 12 37 48 57
iteration 4 12 25 37 48 57
Average time complexity of selection sort is O(n2).
No comments:
Post a Comment