Write a C++ program to Bubble sort an array 7 6 5 4 3 in ascending orderCode -
#include<iostream>
using namespace std;
int main()
{
int n, i, arr[5],
j, temp;
cout<<"Enter the Size (max. 5): \n";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n;
i++)
cin>>arr[i];
for(i=0;
i<(n-1); i++)
{
for(j=0;
j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp =
arr[j];
arr[j]
= arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"\nThe Sorted Array is: \n";
for(i=0; i<n;
i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output -
Enter the Size (max. 5): 5
Enter 5 Numbers: 7 6 5 4 3
The Sorted Array is:
3 4 5 6 7
Note - Orange colour text represent input is entered by user
0 Comments