Write a program that implements the values in sorted manner of an unsorted array in a link.
Answers
Program in C++:
Bubble sorting:
#include<iostream>
using namespace std;
int main()
{
int N;
cout<<"Enter number of elements in the array : ";
cin>>N;
int A[N];
cout<<"\nEnter values in the array : \n";
for(int i = 0; i < N; i++)
{
cin>>A[i];
}
cout<<"\nUnsorted array : ";
for(int i = 0; i < N; i++)
{
cout<<A[i]<<"\t";
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N-i-1; j++)
{
if(A[j] > A[j+1])
{
int temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
cout<<"\n\nSorted array : ";
for(int i = 0; i < N; i++)
{
cout<<A[i]<<"\t";
}
return 0;
}
Output:
Enter number of elements in the array : 10
Enter values in the array :
9
2
7
4
5
10
6
3
1
8
Unsorted array : 9 2 7 4 5 10 6 3 1 8
Sorted array : 1 2 3 4 5 6 7 8 9 10