SELECTION SORT
Write a C++ program to accept a single dimensional
array and sort given array elements by Selection Sort.
#include<iostream.h>
#include<conio.h>
void selsort(int[],int);
void main()
{
clrscr();
int a[50],item,n,index;
cout<<"Enter array
size ";
cin>>n;
cout<<"\nEnter
array elements :";
for(int i=0;i<n;i++)
cin>>a[i];
selsort(a,n);
cout<<"\n\nThe
sorted array is as shown below \n";
for(i=0;i<n;i++)
cout<<a[i]<<"
";
cout<<endl;
getch();
}
void selsort(int a[],int
size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{
small=a[i];
for(int j=i+1;j<size;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
cout<<"\nArray
after pass - "<<i+1<<" - is:";
for(j=0;j<size;j++)
cout<<a[j]<<"
";
}
}
No comments:
Post a Comment