代码实现
#include<stdio.h>
void PrintArrary(char desc[],int a[],int len){
puts(desc);
for(int i = 0;i<len;i++){
printf("%d ",a[i]);
}
printf("\n");
}
int odd_even_sort(int a[],int len){
int flag = 0,temp;
while(flag == 0) {
flag = 1;
for(int i = 1;i<len-1;i+=2){
if(a[i]>a[i+1]){
flag = 0;
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
for(int i = 0;i<len-1;i+=2){
if(a[i]>a[i+1]){
flag = 0;
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
return 1;
}
int main(){
int b[8] = {8,1,4,5,6,9,2,3};
PrintArrary("排序前",b,8);
odd_even_sort(b,8) ;
PrintArrary("排序后",b,8);
}
运行结果