您的位置:首页 > 理论基础 > 数据结构算法

数据结构基础 算法实现

2015-09-05 11:59 519 查看
本程序就是把数据结构基础上的代码运行起来,用转载比较合适。

#define MAX_SIZE 128

void sort(int a[], int n)

{

 for(int i=0;i<n-1;i++)

 {

  int min = i;

  for(int j=i+1;j<n;j++)

  {

   if(a[j]<a[min])

    min = j;

  }

  int temp = a[i];

  a[i] = a[min];

  a[min] = temp;

 }

}

int main()

{

 //当前未排序的整数中,找出最小一个,把它放在当前有序表的后一个位置

 int n;

 int a[MAX_SIZE];

 printf("Enter the number of numbers to generate(the max size is %d): ",MAX_SIZE);

 scanf("%d",&n);//引号内不能有空格

 if(n<1 || n>MAX_SIZE)

 {

  fprintf(stderr,"Improper value of n\n");

  exit(EXIT_FAILURE);

 }

 for(int i =0;i<n;i++)

 {

  a[i] = rand()%1000;

  printf("%d  ", a[i]);

 }

 sort(a,n);

 printf("\n Sorted array: \n");

 for(i =0;i<n;i++)

  printf("%d  ", a[i]);

 printf("\n");

 return -1;

}

 

 

运行不过的话加头文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: