#include <stdio.h>
void initIntArray(int arr[], int length) {
// 初始化, 函数里和函数外是"同一个数组"
for (int i = 0; i < length; i++) {
printf("请输入第 %d 个数字\n", i + 1);
scanf("%d", &arr[i]);
}
}
void foreachIntArray(int array[], int length) {
printf("====================\n");
for (int i = 0; i < length; i++) {
printf("%d\n", array[i]);
}
}
void bubbleSortByIntArray(int array[], int length) {
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - i - 1; j++) {
if(array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
int main(int argc, const char * argv[]) {
int nums;
foreachIntArray(nums, 5);
bubbleSortByIntArray(nums, 5);
foreachIntArray(nums, 5);
return 0;
}
|
|