//implementing selection sort
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],ptr,i,k,loc,min,temp;
clrscr();
cout<<"Enter the elements in array";
for(i=1;i<=10;i++)
{
cin>>a[i];
}
for(i=1;i<=10;i++)
{
min=a[i];
loc=i;
ptr=i+1;
while(ptr<=10)
{
if(min>a[ptr])
{
min=a[ptr];
loc=ptr;
}
ptr++;
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"Sorted array is\n";
for(i=1;i<=10;i++)
cout<<"\na["<<i<<"]="<<a[i];
getch();
}
********output**********
No comments:
Post a Comment