100 哥随机数 。从小到大排序
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define NUM 100
void bbule(int *arr,int size){for (int i = 0; i < size-1; i++)// 运行O(n)次{/* code */for (int j = 0; j < size-1-i; j++)// 要减i哟{/* code */if (arr[j] > arr[j+1])// 这个大于 就是 从小到大 排列; 如果测试小于呢?{/* code */arr[j] ^=arr[j+1];arr[j+1] ^=arr[j];arr[j] ^=arr[j+1];}}}}
void printfArry(int *arr,int size){for (int i = 0; i < size; i++){/* code */printf("%d ",arr[i]);}putchar('\n');}int main(){// int arr[6]= {1,8,6,4,9,7};int arr[NUM]= {0};srand( (unsigned)time( NULL ) ); //初始化随机数for (int i = 0; i < NUM; i++){/* code */arr[i] =rand();}// rand(); //这是数啊 srand( (unsigned)time(NULL))
printfArry(arr,NUM);
bbule(arr,NUM);
printfArry(arr,NUM);return 0;
}
结果展示: