#include #include typedef enum { false, true } bool; #define N 8 void bubble_sort(int a[],int n); //一般实现 void bubble_sort(int a[],int n)//n为数组a的元素个数 { int i,j; //一定进行N-1轮比较 for(i=0; i a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1]=temp; } } } } //优化实现 void bubble_sort_better(int a[],int n)//n为数组a的元素个数 { int i,j; bool isSorted; int temp; //最多进行N-1轮比较 for(i=0; i a[j+1]) { isSorted = false; temp = a[j]; a[j] = a[j+1]; a[j+1]=temp; } } if(isSorted) break; //如果没有发生交换,说明数组已经排序好了 } } int main() { int i; int num[N] = {89, 38, 11, 78, 96, 44, 19, 25}; bubble_sort(num, N); //或者使用bubble_sort_better(num, N); for(i=0; i