Friday, 7 July 2017

C++ program of selection sort

//Program starts

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int n=5,a[n],b,min=0;
for(int i=0;i<n;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>a[i];
}
for(int j=0;j<n;j++)
{
min=j;
for(int i=j+1;i<=n-1;i++)
{

if(a[i]<a[min])
{
min=i;
}
}

b=a[min];
a[min]=a[j];
a[j]=b;
}
cout<<"\nData in sorted form \n \n";
for(int i=0;i<n;i++)
{
cout<<"Data at ["<<i<<"] = "<<a[i]<<endl;
}
}
//Program ends

OUTPUT:

Share:

Related Posts:

0 comments:

Post a Comment