array[][] sort

 avatar
Alexmegawin
c_cpp
2 years ago
977 B
3
Indexable
Never
#include <iostream>
#include <time.h>
using namespace std;

int main() {
	srand(time(NULL));
	int **array;
	int n, m;
	int num, num2;
	cin >> n >> m;
	array = new int*[n];
	for (int i = 0; i < n; i++) {
		array[i] = new int[m];
	}

	for (int j = 0; j < n; j++) {
		for (int k = 0; k < m; k++) {
			array[j][k] = rand() % 101;
		}
	}

	for (int h = 0; h <= (n * m); h++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m - 1; k++) {
				if (array[j][k] > array[j][k + 1]) {
					num = array[j][k];
					array[j][k] = array[j][k + 1];
					array[j][k + 1] = num;
				}
			}
		}
		for (int k = 0; k < m; k++) {
			for (int j = 0; j < n - 1; j++) {
				if (array[j][k] > array[j+1][k]) {
					num2 = array[j][k];
					array[j][k] = array[j+1][k];
					array[j+1][k] = num2;
				}
			}
		}
	}

	for (int j = 0; j < n; j++) {
		for (int k = 0; k < m; k++) {
			cout << array[j][k] << "	";
		}
		cout << endl;
	}
		return 0;
}