准备好好学习算法了,大家一起努力
这是选择排序代码,自己手动编写的,加油
#include <stdio.h>
#define N 9
int SelectionSort(int sort[]);
int swap(int *a,int *b);
int main()
{
int i;
int sort[N] = {9,8,7,6,5,4,3,2,1};
for (i = 0; i< N; i++)
{
printf("%d ",sort[i]);
}
printf("\n");
SelectionSort(sort);
for (i = 0; i< N; i++)
{
printf("%d ",sort[i]);
}
printf("\n");
return 0;
}
int SelectionSort(int sort[])
{
int i,j,h;
int min;
for (i = 0; i < N -1 ; i++)
{
min = i;
for (j = i + 1; j < N ; j++ )
{
if (sort[min] > sort[j])
{
min = j;
}
}
if (i!= min)
{
swap(&sort[i],&sort[min]);
}
}
}
int swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}