Как сделать пузырьковую сортировку, чтобы вывести массив зигзагом?

#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
using namespace std;
int** createblock(int rows, int 
cols);
void switinitialize(int** 
blockarr, int rows, int cols, 
int number);
void blockArrout(int** blockarr, int rows, int cols);
void NextItem(int currentX, int currentY, int& nextX, int& nextY, int rows);
void bubbleSort(int** blockarr, int rows);





int main()
{
setlocale(LC_ALL, "rus");
int rowcount, switnumber;
cout << "Введите число строк матрицы" << endl;
cin >> rowcount;
int colscount = rowcount;
int** tablearr;
cout << "Выберите способ заполения. Рандом(1)/Ввод(2)" << endl;
cin >> switnumber;
tablearr = createblock(rowcount, colscount);
switinitialize(tablearr, rowcount, colscount, switnumber);
blockArrout(tablearr, rowcount, colscount);
bubbleSort(tablearr, rowcount);
blockArrout(tablearr, rowcount, colscount);

system("pause");
}
int** createblock(int rows, int cols)
{
int** blockarr;
blockarr = new int* [rows];
for (int i = 0; i < rows; i++)
{
    blockarr[i] = new int[cols];
}
return blockarr;
}

void switinitialize(int** blockarr, int rows, int cols, int number)
{
switch (number)
{
case 1:
{
    srand(time(0));
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            blockarr[i][j] = rand() % 15 + 1;
        }
    }
    break;
}
case 2:
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << "Arr[" << i << "][" << j << "] =   " << endl;
            cin >> blockarr[i][j];
        }
    }
    break;
}
case 3:
{
    break;
}
}
}
void blockArrout(int** blockarr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        cout << setw(2) << blockarr[i][j] << "   ";
    }
    cout << endl;
}
}

void NextItem(int currentX, int currentY, int& nextX, int& nextY, int rows)
{
nextX = currentX;
nextY = currentY;
int diag = 0;
do
{
    switch (diag = diag%2)
    {
    case 0:
    {
        if (nextX != rows)
            nextX++;
        else
            nextY++;
        diag++;
        break;
    }
    case 1:
    {
        nextX--;
        nextY++;
        if (nextX == 0 || nextY == rows)
            diag++;
        break;
    }
    case 2:
    {
        if (nextY != rows)
            nextY++;
        else
            nextX++;
        diag++;
        break;
    }
    case 3:

        nextX++;
        nextY--;
        if (nextY == 0 || nextX == rows)
            diag++;
        break;
    

    }
}while (nextX != rows || nextY != rows);

}

void bubbleSort(int** blockarr, int rows)
{
int col = rows * (rows + 1) / 2;

for (int i = 0; i < col; i++)
{
    int currentX = 0, currentY = 0;
    int nextX, nextY;
    int num = col - 1;

    while (num--)
    {
        NextItem(currentX, currentY, nextX, nextY, rows);
        if (blockarr[currentX][currentY] > blockarr[nextX][nextY])
        {
            swap(blockarr[currentX][currentY], blockarr[nextX][nextY]);
        }
        currentX = nextX;
        currentY = nextY;
    }
}
}

Прошу помочь с пузырьковой сортировкой, чтобы вывести двумерный квадратный массив в следующей форме. Размерность N*N введите сюда описание изображения


Ответы (0 шт):