直接选择排序:
基本思想:把一个无序集中的最小值选择出来放到有序集中
C语言代码:
#include<stdio.h> #define MAX 225 int R[MAX]; //直接选择排序 void Select_Sort(int n) { int i,j,k; for(i=1;i<n;i++) { k=i; for(j=i+1;j<=n;j++) { if(R[j]<R[k]) k=j; } if(k!=i) { R[0]=R[i]; R[i]=R[k]; R[k]=R[0]; } } } int main() { int i,n; printf("直接选择示例: "); printf("Please input the n above 1 and below %d ",MAX); scanf("%d",&n); if(n<1||n>MAX) { printf("Please input right n!"); return 0; } printf("Please input the array under n one by one: "); for(i=1;i<=n;i++) { scanf("%d",&R[i]); } printf("The array you input is: "); for(i=1;i<=n;i++) { printf("%d ",R[i]); } Select_Sort(n); printf("The array after Quick_Sort is: "); for(i=1;i<=n;i++) { printf("%d ",R[i]); } return 0; }