每日算法-冒泡排序

#include<stdio.h>

int main(){
    int a[10] = { 1,2,3,4,5,7,8,9,10,6};
    int flag = 1,temp,i,j,k=0;
    for (i = 1; i <= 10; i++){
        flag = 1;
        for (j = 0; j <10-i; j++){
            if (a[j]>a[j+1]){
                temp = a[j];a[j] = a[j+1];a[j+1] = temp;
                flag = 0;
            }
            k++;
        }
        if (flag) break;
    }
    for (int i = 0; i < 10; i++){
        printf("%d ", a[i]);
    }
    printf("\n %d ", k);
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注