void BubbleSort(int A[],int n) {
int i,j;
for(i=0; i<n-1; i++) {
bool flag=false;//表示本趟冒泡是否发生交换的标志
for(j=n-1; j>i; j--) {//一趟冒泡的过程
if(A[j-1]>A[j]) {//若为逆序
swap(A[j-1],A[j]);交换
flag=true;
}
}
if(flag==false)//本趟遍历后没有发生交换,说明表已经有序
return;
}
}
原文:https://www.cnblogs.com/tianyudizhua/p/13414461.html