数据结构基础 算法实现

来源:互联网 发布:九浅一深网络剧百度云 编辑:程序博客网 时间:2024/06/02 11:26

本程序就是把数据结构基础上的代码运行起来,用转载比较合适。

#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;
}

 

 

运行不过的话加头文件。

0 0
原创粉丝点击