Write a C++ Program to sort array of integers. Get the user input and display it in the sorted order.
#include <iostream>
using namespace std;
int main()
{
cout << "Enter size of array: ";
int size = 0;
int temp = 0;
cin >> size;
int* a = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter " << i << " element of array: ";
cin >> a[i];
}
cout << "Initial array: ";
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
}
cout << endl << "Sorting..." << endl;
for (int i = 0; i < size; i++)
{
for (int j = 1; j < size; j++)
{
if (a[j - 1] > a[j])
{
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}
cout << "Sorted array: ";
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
Comments
Leave a comment