alanwang[GDOU] 用選擇排序法對10個整數排序

Alan丶Wang發表於2020-12-05
# include<stdio.h>
int main()
{
 int i,j,X,temp,a[10];
 printf("please enter 10 numbers\n");
 for (i=0;i<=9;i++)
  {
  scanf("%d",&a[i]); 
  }
 printf("\n");
 printf("The orginal numbers:\n");
 for (i=0;i<=9;i++) 
 printf("%2d",a[i]); 
 printf("\n"); 
 for (i=0;i<=9;i++)
  {X=i;
   for(j=i+1;j<=10;j++)
   if (a[X]>a[j]) X=j;
   temp=a[i]; 
   a[i]=a[X];
   a[X]=temp;
  }
 printf("\n The sorted numbers:\n"); 
 for(i=0;i<10;i++)
  printf("%2d",a[i]);
  printf("\n");
 return 0;
}

相關文章