选择排序
#include<stdio.h>
#define n 10void main( void )
{
//input
int data[n];
int nElems = 0;
int i = 0;
int j = 0;
int temp = 0;
printf( "Please input the number( n<=10 ) of the data:" );
scanf( "%d", &nElems );
if ( nElems > n ) {
printf( "Input error!");
} else {
printf( "Please input the number:");
for ( i = 0; i <nElems; i ++ ) {
scanf( "%d", &data[i] );
}
printf( "the data you input:");
for ( i = 0; i < nElems; i ++ ) {
printf( "%d\t", data[i] );
}
printf( "\n" );
//sort
for ( i = 0; i < nElems-1; i ++ ) {
for ( j = i+1; j < nElems; j ++ ) {
if ( data[i] > data[j] ) {
//exchange
temp = data[j];
data[j] = data[i];
data[i] = temp;
}
}
}
//output
printf( "after sorted:");
for ( i = 0; i < nElems; i ++ ) {
printf( "%d\t", data[i] );
}
printf( "\n" );
}
}
和冒泡排序一样,都是for的描述过程
0,确定变化量
1,确定for之间的关系,嵌套或者平行