A program to initialize a 1-D array and sort it in ascending order using Selection Sort method.

 

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[10],i,j,max,loc,temp;

cout<<"Enter elements of the array:"<<endl;
for(i=0;i<10;i++)
{
cout<<"Enter element no."<<i+1<<": ";
cin>>a[i];
}

for(i=0;i<9;i++)
{
max=a[i];
loc=i;
for(j=i+1;j<10;j++)
{
if(a[j]<max)
{
max=a[j];
loc=j;
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
}

cout<<"\nElements of the sorted array:"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";

gotoxy(25,24); cout<<"Press any key to continue......";
getch();
}

 

< Back