64,683
社区成员
发帖
与我相关
我的任务
分享
[code=c][code=c][code=c]
[/code][/code][/code]void bubble_sort(int unsorted[],int n )//排序
{
int flag=0,m=0;
for (int i = 0; i <n-1; i++)
{
flag=0;
for (int j = 0; j < n-1-i; j++)
{
if (unsorted[j] > unsorted[j+1])
{
int temp = unsorted[j+1];
unsorted[j+1] = unsorted[j];
unsorted[j]=temp;
flag=1;
}
}
m++;
if(!flag) break;
}
cout<<"实际进行了"<<m<<"轮的相邻两个元素的比较交换"<<endl;
cout<<"增加flag标志位可减少"<<n-1-m<<"轮循环"<<endl;
}