I have made the class having 2d array (4 x 4) and maximum value in 2d array like below:
class B {
public:
int shape[4][4] = { 0 };
int maxh = 0;
B() {};
void record(int module[4][4]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
shape[i][j] = module[i][j];
if (shape[i][j] > maxh) { maxh = shape[i][j]; }
}
}
}
};
If there is a class 'B' array,
B b_arr = new B[30000];
how do I sort the class object array by the maximum value?
I have tried to sort array like below code, but I have a stack overflow error.
int partition(B arr[], int p, int r) {
int i = p - 1;
for (int j = p; j < r; j++) {
int cri = arr[r].maxh;
if (arr[j].maxh < cri) {
i++;
B tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
B tmp = arr[i + 1];
arr[i + 1] = arr[r];
arr[r] = tmp;
return i + 1;
}
void quickSort(B arr[], int p, int r) {
if (p < r) {
int q = partition(arr, p, r);
quickSort(arr, p, q - 1);
quickSort(arr, q + 1, r);
}
}