C語言經典算法選擇排序
#include "stdio.h"
int main()
{
int a[10]={
3,1,2,8,9,5,7,4,0,6};
for (int i = 0; i < 9; i++)
{
for (int j = i+1; j < 10; j++)
{
if (a[i]>a[j])
{
int b = a[i];
a[i] = a[j];
a[j] = b;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("%d",a[i]);
}
return 0;
}
C語言經典算法函數實現選擇排序
#include "stdio.h"
void xuanzhe(int a[])
{
for (int i = 0; i < 9; i++)
{
for (int j = i+1; j < 10; j++)
{
if (a[i]>a[j])
{
int b = a[i];
a[i]=a[j];
a[j]=b;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("%d",a[i]);
}
}
int main()
{
int a[10];
for (int i = 0; i < 10; i++)
{
scanf("%d",&a[i]);
}
xuanzhe(a);
return 0;
}
Python實現選擇排序
list1=[3,2,4,1,9,7,0,6,5,8]
for a in range(10):
for b in range(a+1,10):
if list1[a]>list1[b]:
c = list1[a]
list1[a] = list1[b]
list1[b] =c
for a in list1:
print(a)