//简单选择排序源码 void SelectSort( int a[], int n ) { int i, j; for( i = 0; i < n - 1; i++ ) { for( j = i + 1;j < n; j++ ) if ( a[ i ] > a[ j ] ) { int tmp = a[ j ]; a[ j ] = a[ i ]; a[ i ] = tmp; } } } void SelectSort_2(int r[],int n) { int i ,j , min ,max, tmp; for (i=0 ;i < n/2; i++) { min = i; max = i ; for( j = i + 1; j <= n - i; j++ ) { if ( r[ j ] > r[ max ] ) { max = j; } if ( r[ j ] < r[ min ] ) { min = j; } } tmp = r[ i ]; r[ i ] = r[ min ]; r[ min ] = tmp; if ( max != i ) //此处需注意,如第二次交换的是已经进行过交换的下标要注意!!! { tmp = r[ n - i]; r[ n - i ] = r[max]; r[ max ] = tmp; } else { tmp = r[ n - i]; r[ n - i ] = r[min]; r[ min ] = tmp; } for (int k = 0; k <= n; k++ ) printf( "%d\t", r[ k ] ); printf( "\n"); } }
本文出自 “马二丫” 博客,请务必保留此出处http://8734514.blog.51cto.com/8724514/1567701
原文地址:http://8734514.blog.51cto.com/8724514/1567701