int a[] = {3, 5, 6, 1, 2, 4};
int count = sizeof(a) / sizeof(*(a));
int b[count];
for (int i = 0; i < count; i++) {
*(b + i) = i;
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count; j++) {
if (*(a + *(b + j)) > *(a + *(b + j + 1))) {
int temp = 0;
temp = *(b + j);
*(b + j) = *(b + j + 1);
*(b + j + 1) = temp;
}
}
}
for (int i = 0; i < count; i++) {
printf("%d ", *(a + *(b + i)));
}
printf("\n");
for (int i = 0; i < count; i++) {
printf("%d ", *(a + i));
}
感觉不是那么困难,只要对冒泡的方式足够了解,那么这个程序就可以很快的写出来了!
冒泡就把数组里面的元素重新排序,索引是吧元素的下表进行排序。