网站建设步骤视频教程,wordpress 链接重定向,建设网银登录官网,asp企业营销型网站建设在排序算法中#xff0c;冒泡排序是一个很经典的算法#xff0c;最初的冒泡排序一直要运行n-1次#xff0c;但是其中有些事不必要的操作#xff0c;例 如#xff0c;当没有两个数据发生交换时#xff0c;就可以结束运行。 本文介绍的一种方法是对上述条件的改进#xff… 在排序算法中冒泡排序是一个很经典的算法最初的冒泡排序一直要运行n-1次但是其中有些事不必要的操作例 如当没有两个数据发生交换时就可以结束运行。 本文介绍的一种方法是对上述条件的改进即不仅对尾数据进行条件判断同时还对头数据进行条件判断当头数据不发生交换时需要完成一些改进下面给出实现的源代码 #include cstdlib
#include iostreamusing namespace std;void exchange(int a,int b)
{
int temp;
tempa;
ab;
btemp;
}
void Bubble_Sort(int data[],int n)
{
int numpairsn-1,first1;
int j,k;
bool didSwitchtrue,firstSwitchtrue;
while(didSwitch)
{
didSwitchfalse;
for(jfirst-1;j!numpairs;j) //ÕâÊÇðÅݵÄÒ»ÌËÅÅÐò
{
if(data[j]data[j1]firstSwitch)
firstj; //±£´æµÚÒ»´Î½»»»µÄλÖÃ
if(data[j]data[j1])
{
exchange(data[j],data[j1]);
didSwitchtrue;
firstSwitchfalse;
kj; //±£´æ×îºóÒ»´Î½»»»µÄλÖ㬿ÉÄÜ»áÓжà¸ö
}
}
numpairsk;
if(first1)
first1;
}
}
int main(int argc, char *argv[])
{int a[10]{7,12,11,6,5,8,10,19,14,21};Bubble_Sort(a,10);for(int i0;i!10;i)couta[i] ;coutendl; system(PAUSE);return EXIT_SUCCESS;
}转载于:https://www.cnblogs.com/pepcod/archive/2013/01/28/2937386.html