1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <utility> // std::swap
int main()
{
const unsigned SIZE { 4 };
int x[SIZE][SIZE] { { 7, 3, -2, 0 },
{ -5, 9, 2, 12 },
{ 8, -1, 4, 10 },
{ -6, 11, 0, 5 } };
std::cout << "The UNsorted array:\n";
for (unsigned i { }; i < SIZE; i++)
{
for (unsigned j { }; j < SIZE; j++)
{
std::cout << x[i][j] << '\t';
}
std::cout << '\n';
}
std::cout << '\n';
for (unsigned i { }; i < SIZE; i++)
{
for (unsigned j { }; j < SIZE; j++)
{
unsigned m = i;
unsigned n = j + 1;
while (true)
{
if (n == SIZE)
{
n = 0;
m++;
if (m == SIZE) break; // Stopping condition: past the last element in array
}
if (x[i][j] > x[m][n]) std::swap(x[i][j], x[m][n]);
n++;
}
}
}
std::cout << "The sorted array:\n";
for (unsigned i { }; i < SIZE; i++)
{
for (unsigned j { }; j < SIZE; j++)
{
std::cout << x[i][j] << '\t';
}
std::cout << '\n';
}
}
|