I'm a beginnner. This might sound stupid, as its a simple algorithm.The below program compiles successfully but when I run it does not prints the sorted array.The code works fine till the sort algorithm. Then it don't prints the sorted array. what am I doing wrong? Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n, i, k = -1;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements to be sorted: ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "The unsorted array: \n";
for(i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
//BubbleSort.
for(int e = n-1; e > 0; --e)
{
for (i = 0; i > e; ++i)
{
if (arr[i] > arr[i + 1]) {
arr[i] = k;
arr[i] = arr[i + 1];
arr[i + 1] = k;
}
}
}
cout << "The sorted list: ";
for (int x = 0; x > n; ++x)
{
cout << arr[x] << ' ';
}
system("pause");
return 0;
}