//从第一个记录开始,n-1次比较,与后面最小的记录交换;第二个,n-2次比较...
#include <stdio.h>
#define MAX 11
typedef struct
{
int key;
char name;
}elemtype;
void selectsort(elemtype x[],int n);
main()
{
elemtype x[MAX];
int n,i;
printf("\nPlease input the number of data,n=");
scanf("%d",&n);
printf("\nplease input the data to be sorted:");
for(i=0;i<n;i++)
scanf("%d",&x[i].key);
printf("The original data is:\t");
for(i=0;i<n;i++)
printf("%d\t",x[i].key);
printf("\n\n");
selectsort(x,n);
printf("\n\n");
printf("The fianl result is:\t");
for(i=0;i<n;i++)
printf("%d\t",x[i].key);
}
void selectsort(elemtype x[],int n)
{
int i,j,small,m,k;
elemtype temp;
for(i=0;i<n-1;i++)
{
small=i;
for(j=i+1;j<n;j++)
if(x[j].key<x[small].key)
small=j;
if(small!=i)
{
temp=x[i];
x[i]=x[small];
x[small]=temp;
}
m=i+1;
printf("\nNo.%d select sort result\t",m);
for(k=0;k<n;k++)
printf("%d\t",x[k].key);
}
}