#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 101
//swap function bug
//void swap(int x,int y) swap change the location of the two number
void swap(int &x,int &y)//why need &
{
int t;
t=x;
x=y;
y=t;
}
void sort(int list[],int n)
{
int i,j,min;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n-1;j++)
{
if(list[j]<list[min])
{
min=j;
}
}
//please think about it
//what‘s better ?
swap(list[i],list[min]);
}
}//the main ideal is looking for the smallest number
int main(void)
{
int i,n;
int list[MAX_SIZE];//定义数组
printf("enter the number to generate:");
scanf("%d",&n);
/*The input sign n must between 1 and 100 */
if(n<1 || n>MAX_SIZE)
{
/*int fprintf(FILE *stream,char *format,[argument]) */
fprintf(stderr,"improper value of n");
exit(EXIT_FAILURE);
}
//input number
printf("input number:\n");
for (i=0;i<n;i++)
{
/*randomlys generate number*/
list[i]=rand()%1000;
//it‘s good ?
printf("%d ",list[i]);
}
printf("\ninput number end\n");
//sort function
printf("sorting...\n");
sort(list,n);
printf("sort ok\n");
/*print out sorted numbers*/
printf("output number\n");
for(i=0;i<n;i++)
{
printf("%d ",list[i]);
}
printf("\noutput end\n");
exit(0);
}