Wednesday, 20 May 2020

sorting an array

// sorting of array
#include<iostream>
using namespace std;
//#include<conio.h>
//void main()
int main()
{
//clrscr();
int a[100],n;
cout<<"enter the size\n";
cin>>n;
cout<<"enter the elements\n";
for(int i=0;i<=n-1;i++)
{
cin>>a[i];
}
for(int i=0;i<=n-1;i++)
{
int small=a[i];
int pos=i;
for(int j=i+1;j<=n-1;j++)
if(a[j]<small)
{
small=a[j];
pos=j;
}
int temp;
temp=a[i];
a[i]=small;
a[pos]=temp;
}
for(int k=0;k<=n-1;k++)
{
  cout<<a[k]<<"\t";
}

//getch();
return 0;
}

No comments:

Post a Comment