Sunday, July 4, 2010

PROGRAM TO ILLUSTRATE SELECTION SORT(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
class sort
{
public:
int a[50],n,i,j;
void selectionsort();
};
void sort::selectionsort()
{
int min,temp;
cout<<"\nenter the maximum no of elements\n";
cin>>n;
cout<<"enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{temp=a[min];
a[min]=a[i];
a[i]=temp;
}}
cout<<"array after sorting is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void main()
{
clrscr();
sort ob;
ob.selectionsort();
getch();
}

OUTPUT:
enter the maximum no of elements
5e
Enter the elements
54
15
23
2
10
array after sorting is
2 10 15 32 54

0 comments:

Post a Comment