I have a two dimensional array that needs to be sorted diagonally, for example,
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16
I managed to create a new one dimensional array that contains the elements of the two dimensional array and bubble sort it. I am stuck on how to fill the two dimensional array diagonally with the sorted one dimensional array. Any help?
The row and column of the original array are not necessarily equal but in the diagonally sorted array they should be I think.
//Function that swaps two values (used in bubble sort)
void swap(int &a, int &b){
int hold = a;
a = b;
b = hold;
}
//Function that bubble sorts a one dimensional array
void bubbleSort(int a[], int r){
for (int pass = 1; pass < r; pass++){
for (int i = 0; i < r - 1; i++){
if (a[i + 1] < a[i]){
//Swap a[i] and a[i + 1]
swap(a[i + 1], a[i]);
}
}
}
}
//Function that diagonally sorts a two dimensional array
void diagonalSort(int array[][column], int row){
//Step 1: Store the two dimensional array in a one dimensional array
int flatSize = row*column, *flat = new int[flatSize], a = 0;
for (int b = 0; b < row; b++){
for (int c = 0; c < column; c++){
flat[a] = array[b][c];
a++;
}
}
//Step 2: Bubble sort the one dimensional array that was created
bubbleSort(flat, flatSize);
//Step 3: ??
}