研究算法的目的是什么?效率最大化(或时间,或空间,或维护成本,或...)只达到目的就好了.为什么不是冒泡?冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。
#include <stdio.h> #include<stdarg.h> #include <time.h> static void init_srand( long* ); static void test1( size_t,... ); static void test2( size_t,unsigned ); static void test3( unsigned,unsigned,unsigned ); static void check( int*,size_t ); void print( const int [],size_t size ); typedef int E_T; void sort( E_T *array, int l, int r ); int main( void ) { size_t i; time_t start; init_srand(NULL); start=time(NULL); test1(1,0); //测试1个数 test1(2,1,2); //测试顺序两个数 test1(2,2,1); //测试逆序两个数 test1(5,1,1,1,1,1); //测试输入重复数 test1(5,1,2,3,4,5); //测试顺序5个数 test1(5,5,4,3,2,1); //测试逆序5个数 test1(5,1,2,1,1,2); //测试重复数 test1(5,3,2,1,5,4); //测试一般序列 test1(5,-3,-2,-1,-5,-4); //测试负数 puts("ACROSS TEST1"); for (i=1;i!=10001;++i) //从1个数据到10000个数据每个数据段覆盖1次 test2(i,1); puts("ACROSS TEST2"); test3(1,1000000,10); //随机产生1~1000000元素个数,测试10次 puts("ACROSS TEST3"); printf("The test of time is %g s\n",difftime(time(NULL),start)); getchar(); return 0; } #include<stdlib.h> void init_srand( long* data ) { srand(( unsigned )time(data)); } #include<string.h> #include<assert.h> static void test1( size_t len,... ) { va_list ap; int* a=( int* )malloc(len*sizeof (*a)); size_t i; assert(a); va_start(ap,len); for (i=0;i!=len;++i) a[i]=va_arg(ap,int); va_end(ap); sort(a,0,len); print(a,len); check(a,len); //检查数据是否存在bug free(a); } static void test2( size_t len,unsigned count ) { int* buf=( int* )malloc(len*sizeof (*buf)); assert(buf); do { size_t i; for (i=0;i!=len;++i) buf[i]=rand()%100; sort(buf,0,len); print(buf,len); check(buf,len); //检查数据是否存在bug }while (--count); free(buf); } static void test3( unsigned low,unsigned height,unsigned count ) { size_t i; if (low>height) { fprintf(stderr,"TEST3 DATA RANGE ERROR\n"); exit(EXIT_FAILURE); } for (i=0;i!=count;++i) { const size_t len=rand()%(height-low+1)+low; test2(len,1); } } static void check( int* a,size_t len ) { size_t i; if (len<2) return ; for (i=0;i!=len-1;++i) assert(a[i]<=a[i+1]); } void sort( E_T *array, int l, int r ) { int minix, maxix; int R; int c; // int f; int s; s = 0; if( l >= r ) return; for( int ix = l; ix < r -1; ix++ ) { if( array[ ix ] <= array[ ix + 1 ] ) { s++; if( s <= 0 ) break; } else if( array[ ix ] > array[ix + 1 ] ) { s--; if( s >= 0 ) break; } else break; } c=0; if( s == r - l - 1 ) { //printf("jiao huan %d ci\n",c); return; } else if( s == l - r + 1) { for(r -= 1 ; l < r; l++,r-- ) { c++; E_T temp = array[ l ]; array[ l ] = array[ r ]; array[ r ] = temp; } } else { for( ; l < r; ++l, --r ) { // f = 0; for( minix = l + 1, maxix = r - 2, R = r - 1; minix <= R; ++minix, --maxix ) { if( array[ l ] > array[ minix ] ) { c++; // f++; E_T temp = array[ l ]; array[ l ] = array[ minix ]; array[ minix ] = temp; } if( array[ R ] < array[ maxix ] ) { c++; // f++; E_T temp = array[ R ]; array[ R ] = array[ maxix ]; array[ maxix ] = temp; } } /* if( f == 0 ) sort( array, l + 1, r - 1 );*/ } } //printf("jiao huan %d ci\n",c); } void print( const int a[],size_t size ) { size_t i; for (i=0;i!=size;++i) printf("%-4d",a[i]); puts(""); }
[此贴子已经被作者于2018-5-25 15:53编辑过]