有这么一组数:9 1 5 4 8 7 4 6 6
采用选择排序是这么的:
第1次 1 9 5 4 8 7 4 6 6 首先重开始的里面选择最小的,交换位置
第2次 1 4 5 9 8 7 4 6 6 接着又从上面黑色数字里面选最小的
第3次 1 4 4 9 8 7 5 6 6 重复
第4次 1 4 4 58 7 9 6 6
第5次 1 4 4 56 7 9 8 6
第6次 1 4 4 5 6 6 9 8 7
第7次 1 4 4 56 6 7 8 9
第8次 1 4 4 5 6 6 7 8 9
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int n;
char str[100];
int i,j;
int k;
char temp;
printf("please input the string\n");
scanf("%s",str);
n=strlen(str);
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(str[j]<str[k])
{
temp=str[j];
str[j]=str[k];
str[k]=temp;
}
}
}
printf("the result is:%s",str);
printf("\n");
return 0;
}
时间复杂度:O(n^2)