您的位置:首页 > 编程语言

编程实现选择排序

2017-08-06 00:00 190 查看
每次循环遍历最小元素,将其与a[i] 交换

/*选择排序*/
#include<stdio.h>

void selectsort(int *a,int n)
{
int i;
int j;
int temp = 0;
int flag = 0;
for (i=0; i<n-1; i++)
{
temp = a[i];
flag = i;
for (j=i+1; j<n; j++)
{
if (a[j]<temp)
{
temp = a[j];
flag = j;
}
}
if (flag!=i)
{
a[flag] = a[i];
a[i] = temp;
}
}
}

int main()
{
int i = 0;
int a[] = {9,7,3,1,6,5,4,8,2,0};
int len = sizeof(a)/sizeof(a[0]);
selectsort(a,len);
for (i=0; i<len; i++)
{
printf("%d",a[i]);
}
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: