标签:code %x define int span 循环 signed 排序数组 dom
//冒泡排序,输入一个数组,冒泡排序 #include <stdio.h> #include<time.h> #include<stdlib.h> # define MAXSIZE 10 //要排序数组的长度 #define random(x) (rand()%x) //宏定义随机数函数 //三个参数分别为原数组,数组大小,排序后的数组 maopao_sort(int* old,int maxsize, int* new); main() { srand((unsigned)time(NULL)); int old[MAXSIZE]; int new[MAXSIZE]; //生成随机数组 for (int i = 0; i < MAXSIZE; i++) old[i] = random(100)-50; //随机生成(-50,50)之间的数 maopao_sort(old,MAXSIZE,new); //输出原数组 printf("old : "); for (int i = 0; i < MAXSIZE; i++) printf("%d ", old[i]); printf("\n"); //输出排序后的数组 printf("new : "); for (int i = 0; i < MAXSIZE; i++) printf("%d ", new[i]); } maopao_sort(int* old, int maxsize,int* ar) { int i, j; int temp; for (int i = 0; i < MAXSIZE; i++) ar[i] = old[i]; for(j = 0; j < MAXSIZE-1; j++) for (i = 0; i < MAXSIZE-1 - j; i++) //第一轮循环把最大的放在最后位置,第二轮把次大的放在倒数第二个位置 if (ar[i] > ar[i + 1]) { temp = ar[i]; ar[i] = ar[i + 1]; ar[i + 1] = temp; } }
标签:code %x define int span 循环 signed 排序数组 dom
原文地址:https://www.cnblogs.com/abel2020/p/13047304.html