一、概念
二、代码
#include <stdio.h>
int main()
{
#pragma 1.已知一个无序的数组,里面有5个元素,要求对数组进行排序
int nums[5] = {99,23,33,44,21};
int length = sizeof(nums) / sizeof(nums[0]);
for (int i = 0 ; i < length; i++) {
printf("nums[%i] = %i\n",i,nums[i]);
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < length -1 -i; j++) {
// printf("*");
printf("%i == %i\n",j,j+1);
if (nums[j] > nums[j+1]) {
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
// printf("\n");
}
printf("------");
for (int i = 0 ; i < length; i++) {
printf("nums[%i] = %i\n",i,nums[i]);
}
return 0;
}
网友评论