基本思想:
遍历数组中的所有数字,找到最小的数字。
# include<stdio.h>
int sort(int x[],int n)
{
int i,min_index=0;
for(i=1;i<n;i++)
if(x[i]<x[min_index]) min_index = i;
}
将其放置于未被排序的数组部分里最前面部分。
# include<stdio.h>
int sort(int x[],int n)
{
int i,j=0,temp,min_index=0;
for(i=1;i<n;i++)
if(x[i]<x[min_index]) min_index = i;
temp = x[j];
x[j] = x[min_index];
x[min_index] = temp;
}
重复n-1次,以实现对所有元素的排序。
# include<stdio.h>
int sort(int x[],int n)
{
int i,j,temp,min_index;
for(j=0;j<n-1;j++)
{
min_index=j;
for(i=j+1;i<n;i++)
if(x[i]<x[min_index]) min_index = i;
temp = x[j];
x[j] = x[min_index];
x[min_index] = temp;
}
}