[算法练习]选择排序的C语言实现

来源:互联网 发布:淘宝直播端口 编辑:程序博客网 时间:2024/05/01 17:18


#include <stdio.h>int length;void selection_sort(int A[]){int i,j,min_index;int min,temp;for(i=0;i<length-1;i++){/*each loop select the minimum number and  move it to the first of the array,then repeat the step above to the array that does not include the min number*/min=A[i];min_index=i;for(j=i+1;j<length;j++){if(A[j]<min){min=A[j];min_index=j;}}if(min_index!=i){temp=A[i];A[i]=A[min_index];A[min_index]=temp;}}}int main(){int a[]={3,1,5,12,6,8,11,7,4,15};length=sizeof(a)/sizeof(a[0]);//get length of a[]int i;printf("before:\t");for(i=0;i<length;i++){printf("%d ",a[i]);}selection_sort(a);printf("\nafter:\t");for(i=0;i<length;i++){printf("%d ",a[i]);}getchar();}






0 0
原创粉丝点击