代码:
#include<stdio.h>
int data[] = { 3,1,2,4,7,6,8};
void printData(int data[],int length);
void chooseSort(int data[], int length);
void swap(int *a, int *b);
#define N 7
int main(void)
{
printData(data,N);
chooseSort(data,N);
printData(data,N);
return 0;
}
void printData(int data[],int length)
{
int i;
for(i = 0;i<length;++i)
{
printf("%d\t",data[i]);
}
printf("\n");
}
void chooseSort(int data[], int length)
{
int i;
int j;
int t;
for(i=0;i<length-1;++i)
{
t = i;
for(j=i+1;j<length;++j)
{
if(data[j]<data[t])
{
t = j;
}
}
if(t != i)
{
swap(&data[i],&data[t]);
}
}
}
void swap( int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
运行结果:
3 1 2 4 7 6 8
1 2 3 4 6 7 8