Saturday 1 August 2015

Selection Sort Program



#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,temp,min;
cout<<"\nEnter the no. of elements you want:";
cin>>n;
int *a=new int[n];  //dynamically initialize array
cout<<"\nEnter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
 min=i;
 for(int j=i+1;j<n;j++)
 {
  If(a[min]>a[j])
   min=j;
 }
 temp=a[min];
 a[min]=a[i];
 a[i]=temp;
}
cout<<"\nSorted list of given integers is(in ascending order) :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}



No comments:

Post a Comment