Write a program to sort an array consists
of 10 integer numbers in ascending order,
print the array before sorting and after
sorting.
#include <iostream>
using namespace std;
void sort(int arr[],int n){
int temp;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int n;
cout<<"\nEnter number of elements in the array: ";
cin>>n;
cout<<"\nEnter elements of the array: \n";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"\nElenemts of the array BEFORE sorting:\n";
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
sort(arr,n);
cout<<"\nElements of the array AFTER sorting:\n";
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Comments
Leave a comment