void maoPao1(int arr[],int len){
int temp;
// 双重循环
for (int i = 0; i < len; 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 maoPao2(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;
}
}
}
}
int main(int argc, const char * argv[]) {
int a[10] = {1,234,23,45,12,34,19,5,654,300};
for (int i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
printf("\n");
// 进行排序
maoPao1(a, 10);
for (int i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
printf("\n");
maoPao2(a, 10);
for (int i = 0; i < 10; i++) {
printf("%d\t",a[i]);
}
printf("\n");
return 0;
} |
|