C 当我已经对2D数组进行气泡排序时,如何显示该数组的索引?

C 当我已经对2D数组进行气泡排序时,如何显示该数组的索引?,c,multidimensional-array,bubble-sort,C,Multidimensional Array,Bubble Sort,我的问题要求我显示已经使用冒泡排序进行排序的数组的索引。但我不知道该怎么做 int hdb[3][5] = {{5510, 5538, 5519, 5543, 5540}, {6627, 7656, 8603, 9201, 9404}, {3623, 4388, 4861, 5293, 5817}}; int j, temp; printf("The number of applications for the 3-room

我的问题要求我显示已经使用冒泡排序进行排序的数组的索引。但我不知道该怎么做

int hdb[3][5] = {{5510, 5538, 5519, 5543, 5540},
                 {6627, 7656, 8603, 9201, 9404},
                 {3623, 4388, 4861, 5293, 5817}};
int j, temp;

printf("The number of applications for the 3-room HDB flat is:\n");

for (j = 0; j < 5; j++) {
  if (hdb[0][j] > hdb[0][j + 1]) {
    temp = hdb[0][j];
    hdb[0][j] = hdb[0][j + 1];
    hdb[0][j + 1] = temp;
  }
  printf("%d in index %d\n", hdb[0][j], j + 1);
}
但我的预期产出应该是:

The number of applications for the 3-room HDB flat is:
5510 in index 1
5519 in index 3
5538 in index 2
5540 in index 5
5543 in index 4

但是您得到的输出是正确的输出。您期望的输出在(部分!)排序之前。排序后,这些索引不再有效。是否有任何方法可以获得预期的输出?也许就像对索引进行冒泡排序一样(?),对不起,我对编码很陌生,但是冒泡排序值总是以同样的方式处理第二个数组(包含索引)。或者使用包含值和初始索引的结构。您有5个元素的数组。这意味着最后一个元素的索引是4。然后使用j从0迭代到4,并比较位置j和j+1处的元素。但当j=4时,它将比较索引4和索引5处的元素。没有索引为5的元素。因此,您的数组溢出。请修复该错误。您需要解决的真正问题是什么?为什么需要对(子)数组进行排序?为什么需要打印旧的(原始)索引?请花些时间阅读,以及。另外,请花一些时间阅读,因为你的问题是一个。
The number of applications for the 3-room HDB flat is:
5510 in index 1
5519 in index 3
5538 in index 2
5540 in index 5
5543 in index 4