Simple selection Sort source 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 ) //here to note, such as the second exchange is already in the exchange of subscript to pay attention!!! &NBSP;&NBSP;{&NBSP;&NBSP;TMP&NBSP;=&NBSP;R[&NBSP;N&NBSP;-&NBSP;I];&NBSP;&NBSP;R[&NBSP;N&NBSP;-&NBSP;I&NBSP;] = r[max]; r[ max ] = tmp; } else &NBSP;&NBSP;{&NBSP;&NBSP;TMP&NBSP;=&NBSP;R[&NBSP;N&NBSP;-&NBSP;I];&NBSP;&NBSP;R[&NBSP;N&NBSP;-&NBSP;I&NBSP;] = r[min]; r[ min ] = tmp; } for (int k = 0; k <= n; k++ ) printf ( "%d\t", r[ k ] ); printf ( "\ n"); } }
This article is from the "Umaji Ya" blog, please be sure to keep this source http://8734514.blog.51cto.com/8724514/1567701
Improvements to simple sorting