关于冒泡法排序提前结束的有关问题
关于冒泡法排序提前结束的问题
最近在书上看到,用冒泡法排序,如果顺序提前已经排好,是可以提前结束的,节省时间。但不明白其中的原理,麻烦大家指点一下。在此不胜感激!
现把程序copy如下:
#include "stdafx.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int temp,outer,inner,didswap,ctr;
int nums[10];
for(ctr=0;ctr<10;ctr++)
{nums[ctr]=rand()%100;}
puts("\nhere is the list before the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
for(outer=0;outer<9;outer++)
{didswap=0;
for(inner=outer;inner<10;inner++)
{if(nums[outer]>nums[inner])
{temp=nums[outer];
nums[outer]=nums[inner];
nums[inner]=temp;
didswap=1;/*这里不太明白,下面稍微改了下程序,就不能排序了,它的原理是什么*/
}
}
if(didswap==0)/*如果再循环开始时列表已经排好序,或者如果只需扫描几遍就能完成排序,那么外层循环就可以提前结束*/
break;
}
printf("\nhere is the list after the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
return(0);
}
在vc中编译通过,是能正常排序的,但其中有点不明白,改编程序如下:
#include "stdafx.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int temp,outer,inner,didswap,ctr;
int nums[10]={0,5,3,4,56,6,7,8,89,9};/*只改了nums[10]的赋值,上面采用随机赋值,其他的都未动,但是结果是不能正常排序,仍会打印出原来的值*/
puts("\nhere is the list before the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
for(outer=0;outer<9;outer++)
{didswap=0;
for(inner=outer;inner<10;inner++)
{if(nums[outer]>nums[inner])
{temp=nums[outer];
nums[outer]=nums[inner];
nums[inner]=temp;
didswap=1;
}
}
if(didswap==0)
break;
}
printf("\nhere is the list after the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
return(0);
}
等待大家指点迷津!
------解决方案--------------------
最近在书上看到,用冒泡法排序,如果顺序提前已经排好,是可以提前结束的,节省时间。但不明白其中的原理,麻烦大家指点一下。在此不胜感激!
现把程序copy如下:
#include "stdafx.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int temp,outer,inner,didswap,ctr;
int nums[10];
for(ctr=0;ctr<10;ctr++)
{nums[ctr]=rand()%100;}
puts("\nhere is the list before the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
for(outer=0;outer<9;outer++)
{didswap=0;
for(inner=outer;inner<10;inner++)
{if(nums[outer]>nums[inner])
{temp=nums[outer];
nums[outer]=nums[inner];
nums[inner]=temp;
didswap=1;/*这里不太明白,下面稍微改了下程序,就不能排序了,它的原理是什么*/
}
}
if(didswap==0)/*如果再循环开始时列表已经排好序,或者如果只需扫描几遍就能完成排序,那么外层循环就可以提前结束*/
break;
}
printf("\nhere is the list after the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
return(0);
}
在vc中编译通过,是能正常排序的,但其中有点不明白,改编程序如下:
#include "stdafx.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int temp,outer,inner,didswap,ctr;
int nums[10]={0,5,3,4,56,6,7,8,89,9};/*只改了nums[10]的赋值,上面采用随机赋值,其他的都未动,但是结果是不能正常排序,仍会打印出原来的值*/
puts("\nhere is the list before the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
for(outer=0;outer<9;outer++)
{didswap=0;
for(inner=outer;inner<10;inner++)
{if(nums[outer]>nums[inner])
{temp=nums[outer];
nums[outer]=nums[inner];
nums[inner]=temp;
didswap=1;
}
}
if(didswap==0)
break;
}
printf("\nhere is the list after the sort:\n");
for(ctr=0;ctr<10;ctr++)
{printf("%d\n",nums[ctr]);}
return(0);
}
等待大家指点迷津!
------解决方案--------------------
- C/C++ code
void BubbleSort (int a[], int n) { int exchange,bound; int temp; int j=0; exchange = n-1; while( exchange ) { bound = exchange; exchange = 0; for( j = 0 ;j < bound;j++ ) { if(a[j] > a[j+1] ) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; exchange = j; } } } }
------解决方案--------------------
程序你对比下,应该能够看出点问题了
- C/C++ code
#include "stdio.h" #include "stdlib.h" int main(int argc, char* argv[]) { int temp,outer,inner,didswap,ctr; int nums[10]={0,5,3,4,56,6,7,8,9,89}; puts("\nhere is the list before the sort:\n"); for(ctr=0;ctr<10;ctr++) { printf("%d\n",nums[ctr]); } for(outer=0;outer<9;outer++) { didswap=0; for(inner=9;inner>=outer;inner--) { if(nums[inner]<nums[inner-1]) { temp=nums[inner]; nums[inner]=nums[inner-1]; nums[inner-1]=temp; didswap=1; } } if(didswap==0) break; } printf("\nhere is the list after the sort:\n"); for(ctr=0;ctr<10;ctr++) { printf("%d\n",nums[ctr]); } return(0); }