void bubble_sort(int arr[],int count) { //count 是数组的长度 for(int i =0;i<count;i++) { //可以这样理解 如果最大的数在第一个位置,则将它移动到最后需要比较 count-1次 //第一次循环完毕后,最末尾的值是必定是最大的, //第二次循环完毕后,倒数第二个的值必定是第二大的 for(int j =0;j<count-i-1;j++) { if(arr[j]>arr[j+1]) { int temp = arr[j+1]; arr[j+1] = arr[j]; arr[j] = temp; } } } } void bubble_sort(int arr[],int count) { for(int i=count-1,ischanged=1;i>=1&&change;i--) { //如果这一遍遍历没有交换过元素的位置,则下一遍肯定也不会,中断在外层循环。 ischanged =0; for(int j=0;j<i;j++) { if(arr[j]>arr[j+1]) { int temp = arr[j+1]; arr[j+1] = arr[j]; arr[j] = temp; ischanged = 1; } } }