选择排序
// SelectSort.cpp : Defines the entry point for the console application. // #include "stdafx.h" int sort(int* p,int len ) { int index,temp,i,j; if( p == NULL || len <= 1 ) { return -1; } for( i = 0; i < len; i++ ) { index = i; for( j = i+1;j < len;j++ ) { if( p[j] < p[index] ) { index = j; } } if( index != i ) { temp = p[i]; p[i] = p[index]; p[index] = temp; } } return 0; } int main(int argc, char* argv[]) { int a[]={12,54,6,3,4,6,76,33}; int i; sort(a,sizeof(a)/sizeof(a[0])); for( i = 0 ; i < sizeof(a)/sizeof(a[0]);i++ ) { printf("%d ",a[i]); } printf("\n"); return 0; }
联系方式:heshengjun@tinywsn.com