In C++ language when data is provided by user that is not sorted. You can sort data that is provided by user in array using this Program:
#include<conio>
#include<iostream>
main()
{
//use of array
int arr[100],i,j,temp,n;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"provide data"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(j=i;j>=0;j--)
{
if(arr[j]<arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
else
{
break;
}
}
}
for(int a=0;a<i;a++)
{
cout<<arr[a]<<endl;
}
getch();
}
#include<conio>
#include<iostream>
main()
{
//use of array
int arr[100],i,j,temp,n;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"provide data"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(j=i;j>=0;j--)
{
if(arr[j]<arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
else
{
break;
}
}
}
for(int a=0;a<i;a++)
{
cout<<arr[a]<<endl;
}
getch();
}
Post a Comment