c语言实现冒泡排序

来源:互联网 发布:社会化新媒体矩阵 编辑:程序博客网 时间:2024/05/16 18:33
 1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <time.h>
  5 int* getArr();
  6 void main()
  7 {
  8     int* a;
  9     int i;
 10     a = getArr();
 11     int length = 10;
 12     for (i=1;i<length;i++)
 13     {
 14         int j=0;
 15         for (j=0;j<length-i;j++)
 16         {
 17             if (a[j]>a[j+1])
 18             {
 19                 int temp;
 20                 temp = a[j];
 21                 a[j] = a[j+1];
 22                 a[j+1] = temp;
 23             }
 24         }
 25     }
 26 
 27     for (i=0;i<length;i++)
 28     {
 29         printf("%d\n", a[i]);
 30     }

 31 }

 33 int* getArr()
 34 {
 35     srand((unsigned)time(NULL));
 36     int i;
 37     int * a = (int *)malloc(10*sizeof(int));
 38     for (i=0;i<10;i++)
 39     {
 40         time_t j;
 41         j = rand()%10;
 42         a[i] = j;
 43     }
 44     return a;
 45 }

原创粉丝点击