Tuesday, 22 January 2013

Selection Sort


By on 03:31

Selection Sorting is the sorting algorithm or a type of sorting which is used to sort individual data items in ascending or descending order.Complexity of Selection Sort is O(n)2.It is the most simple sorting method as compared to bubble,insertion,heap etc.This technique is used in c ++,java,c# etc.


Selection Sorting Example and Algorithm:-

=5  7  4  3  2  6  1(Before Sort)
=4  7  5  3  2  6  1(Swap 4 & 3)
=3  7  5  4  2  6  1(Swap 3 & 2)
=2  7  5  4  3  6  1(Swap 2 & 1)
=1  7  5  4  3  6  2(Swap 7 & 5)
=1  5  7  4  3  6  2(Swap 5 & 4)
=1  4  7  5  3  6  2(Swap 4 & 3)
=1  3  7  5  4  6  2(Swap 3 & 2)
=1  2  7  5  4  6  2(Swap 7 & 5)
=1  2  5  7  4  6  3(Swap 5 & 4)
=1  2  4  7  5  6  3(Swap 4 & 3)
=1  2  3  7  5  6  4(Swap 7 & 5)
=1  2  3  5  7  6  4(Swap 5 & 4)
=1  2  3  4  7  6  5(Swap 7 & 6)
=1  2  3  4  6  7  5(Swap 6 & 5)
=1  2  3  4  5  7  6(Swap 7 & 6)
=1  2  3  4  5  6  7(After Sort)

Example of Selection Sort(In Program):-


#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,t,n;
cout<<"Enter size";
cin>>n;
cout<<"Enter array element";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout<<"Array as";
for(i=0;i,n;i++)
cout<<a[i];
getch();
}


That's all about Selection Sorting in data structure and computer science.


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...