Write a c program to perform selection sort on an array of n elements. selection sort algorithm starts by comparing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. then, again first element and third element are compared and swapped if necessary. this process goes on until first and last element of an array is compared. this completes the first step of selection sort in binary labs in binary labs
Answers
Answered by
0
#includ<iostream>
Using namespace std;
Int main()
{
Int AR[20], N, I, J, MIN, POS;
COUT<<"how many elements";
Con>>N;
Cout<<"enter the Aray element";
For(I=0;I<N;I++)
Cin>>AR[I] ;
For(I=0;I<N-1;I++)
{
MIN=AR[I] ;
POS=I;
FOR(J=I+1;J<N;J++)
IF(AR(J) <MIN)
{
MIN=AR[J] ;
PPS=J;
}
IF(POS! =I);
{
AR[POS] =AR[I] ;
AR[I] =MIN;
}
}
COUT<<"SORTED ARAY IS :";
FOR(I=0;I<N;I++)
COUT<<AR[I] <<"\t";
Return 0;
}
Using namespace std;
Int main()
{
Int AR[20], N, I, J, MIN, POS;
COUT<<"how many elements";
Con>>N;
Cout<<"enter the Aray element";
For(I=0;I<N;I++)
Cin>>AR[I] ;
For(I=0;I<N-1;I++)
{
MIN=AR[I] ;
POS=I;
FOR(J=I+1;J<N;J++)
IF(AR(J) <MIN)
{
MIN=AR[J] ;
PPS=J;
}
IF(POS! =I);
{
AR[POS] =AR[I] ;
AR[I] =MIN;
}
}
COUT<<"SORTED ARAY IS :";
FOR(I=0;I<N;I++)
COUT<<AR[I] <<"\t";
Return 0;
}
Similar questions