Pages

Wednesday, February 16, 2011

SELECTION SORT

#include<iostream.h>
#include<conio.h>
class selection_sort
{
int a[50];
int i,j,k,n,min,temp,l,pos;
public:
void getarray();
void display();
};
void selection_sort::getarray()
{
cout<<"\nEnter the size of the array(max 50)"; cin>>n;
cout<<"\n Enter the array"; for(i=1;i<=n;i++) cin>>a[i];
}
void selection_sort::display()
{
for(k=1;k<=n;k++) { min=a[k]; for(l=k+1;l<=n;l++) { if(min>a[l])
{
min=a[l];
pos=l;
}
}
temp=min;
a[pos] =a[k];
a[k]=temp;



}
for(int j=1;j<=n;j++)
cout<<a[j]<<"\t";
}
void main()
{
clrscr();
selection_sort obj;
cout<<"\t ///////////////////SELECTION SORT///////////////////";
obj.getarray();
cout<<" sorted array"<<"\n";
obj.display();
getch();
}



This time we take a look at how Selection Sort works

No comments:

Post a Comment