#include<iostream>
#include<iomanip>
using namespace std;
const int N=6;
void bubble_sort(int unsorted[],int n )//排序
{
int flag=0,m=0;
for (int i = 0; i <n-1; i++)
{
for (int j = 0; j < n-1-i; j++)
{
flag=0;
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;
}
void display(int x[],int n)
{
for (int i = 0; i <n; i++)
cout<<setw(5)<<x[i];
}
int main( )
{
int x[N] ;
for(int i=0;i<N;i++)
cin>>x[i];
cout<<"排序前的資料:"<<endl;
display(x,N);
cout<<endl;
bubble_sort(x,N);
cout<<"排序后的序列:"<<endl;
display(x,N);
cout<<endl;
return 0;
}
那個flag咋起作用的啊?
[code=c][code=c][/code][/code][/code][code=c]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283787.html
標籤:C++ 語言
上一篇:C/C++網路編程里撰寫的服務端/客戶端怎么才能在非局域網下進行連接呢?需要哪方面的知識呢?
下一篇:大佬們 幫幫孩子吧

[code=c]