#include <stdio.h>
//冒泡排序
void maoPao(int arr[],int len){
int temp;
for (int i = 0;i < len-1 ; i++) {
for (int j = 0;j<len-1-i; j++) {
if (arr[j]>arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//选择排序
void xuanZe(int arr[],int len){
int temp;
//
for (int i = 0; i < len - 1; i++) {
//循环执行下面的小数放前面,大数放后面
for (int j = i+1; j < len-1; j++) {
if (arr[i] > arr[j]) {
//把小数放前面,大数放后面
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
int main(){
//定义数组
int ages[5] = {1,54,5,23,68};
//定义数组的长度 其实已知了
int length = sizeof(ages)/sizeof(int);
//maoPao(ages,length);
xuanZe(ages,length);
for (int i = 0; i < length; i++) {
printf("%d\n",ages[i]);
}
return 0;
} |
|