首页 > 解决方案 > 当我已经对它进行冒泡排序时,如何显示我的二维数组的索引?

问题描述

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

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 2
5538 in index 3
5540 in index 4
5543 in index 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

标签: cmultidimensional-arraybubble-sort

解决方案


推荐阅读