选择排序(欢迎测试,如有任何bug请告知,谢谢!)
欢迎测试,如有任何问题请告知,先行谢过。程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ET; void sort( ET *array, int l, int r ); int main( void ) { ET a[ 10 ]; srand( ( unsigned )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( ET *array, int l, int r ) { int minix, maxix; ET min, max; int i; int t; while( l < r ) { for( minix = l, maxix = l, i = l + 1; i < r; ++i ) { minix = array[ minix ] < array[ i ] ? minix : i; maxix = array[ maxix ] > array[ i ] ? maxix : i; } if( minix == r - 1 || maxix == l ) { if( minix == r - 1 ) { t = maxix; max = array[ r - 1 ]; array[ r - 1 ] = array[ maxix ]; array[ maxix ] = max; min = array[ t ]; array[ t ] = array[ l ]; array[ l ] = min; } else { t = minix; min = array[ l ]; array[ l ] = array[ minix ]; array[ minix ] = min; max = array[ t ]; array[ t ] = array[ r - 1 ]; array[ r - 1 ] = max; } } else { min = array[ l ]; array[ l ] = array[ minix ]; array[ minix ] = min; max = array[ r - 1 ]; array[ r - 1 ] = array[ maxix ]; array[ maxix ] = max; } ++l; --r; } }