回复 36楼 九转星河
跳出循环我以前写冒泡的时候就考虑了,只是没实现,因为一个简单的例子就能狠狠的打脸。{0,2,3,6,7,8,1,4,5,9}
09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
void sort( E_T *array, int l, int r ) { int minix, maxix; int R; int c; c=0; for( ; l < r; ++l, --r ) { int mask = 1; for( minix = l + 1, maxix = r - 2, R = r - 1; minix <= R; ++minix, --maxix ) { if( array[ l ] > array[ minix ] ) { mask = 0; c++; E_T temp = array[ l ]; array[ l ] = array[ minix ]; array[ minix ] = temp; } if( array[ R ] < array[ maxix ] ) { mask = 0; c++; E_T temp = array[ R ]; array[ R ] = array[ maxix ]; array[ maxix ] = temp; } } if(mask) break; } printf("jiao huan %d ci",c); }
[此贴子已经被作者于2018-5-24 11:33编辑过]
#include <stdio.h> #include <time.h> #include <stdlib.h> typedef int E_T; void sort( E_T *array, int l, int r ); int main( void ) { E_T a[ 10 ] = {9,9,2,3,4,5,6,7,0,0}; /* srand( ( unsigned long )time( NULL ) ); for( int i = 0; i < 10; ++i ) { a[ i ] = rand() % 1000; printf( "%d ",a[ i ] ); }*/ printf( "\n" ); sort( a, 0, 10 ); for( int i = 0; i < 10; ++i ) printf( "%d ", a[ i ] ); return 0; } 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++; 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 = 1; E_T temp = array[ l ]; array[ l ] = array[ minix ]; array[ minix ] = temp; } if( array[ R ] < array[ maxix ] ) { c++; f = 1; 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); }
[此贴子已经被作者于2018-5-24 21:24编辑过]