第十五周 项目一(3)冒泡排序

来源:互联网 发布:mac怎么两个窗口 编辑:程序博客网 时间:2024/06/18 15:02

冒泡排序.

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #define MaxSize 20  
  3. typedef int KeyType;    //定义关键字类型  
  4. typedef char InfoType[10];  
  5. typedef struct          //记录类型  
  6. {  
  7.     KeyType key;        //关键字项  
  8.     InfoType data;      //其他数据项,类型为InfoType  
  9. } RecType;              //排序的记录类型定义  
  10.   
  11. void BubbleSort(RecType R[],int n)  
  12. {  
  13.     int i,j,k;  
  14.     RecType tmp;  
  15.     for (i=0; i<n-1; i++)  
  16.     {  
  17.         for (j=n-1; j>i; j--)   //比较,找出本趟最小关键字的记录  
  18.             if (R[j].key<R[j-1].key)  
  19.             {  
  20.                 tmp=R[j];  //R[j]与R[j-1]进行交换,将最小关键字记录前移  
  21.                 R[j]=R[j-1];  
  22.                 R[j-1]=tmp;  
  23.             }  
  24.         printf("i=%d: ",i);  
  25.         for (k=0; k<n; k++)  
  26.             printf("%d ",R[k].key);  
  27.         printf("\n");  
  28.     }  
  29. }  
  30. int main()  
  31. {  
  32.     int i,n=10;  
  33.     RecType R[MaxSize];  
  34.     KeyType a[]= {9,8,7,6,5,4,3,2,1,0};  
  35.     for (i=0; i<n; i++)  
  36.         R[i].key=a[i];  
  37.     printf("排序前:");  
  38.     for (i=0; i<n; i++)  
  39.         printf("%d ",R[i].key);  
  40.     printf("\n");  
  41.     BubbleSort(R,n);  
  42.     printf("排序后:");  
  43.     for (i=0; i<n; i++)  
  44.         printf("%d ",R[i].key);  
  45.     printf("\n");  
  46.     return 0;  
  47. }  

改进的算法(一趟冒泡没有交换,即时结束排序过程)

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #define MaxSize 20  
  3. typedef int KeyType;    //定义关键字类型  
  4. typedef char InfoType[10];  
  5. typedef struct          //记录类型  
  6. {  
  7.     KeyType key;        //关键字项  
  8.     InfoType data;      //其他数据项,类型为InfoType  
  9. } RecType;              //排序的记录类型定义  
  10. void BubbleSort1(RecType R[],int n)  
  11. {  
  12.     int i,j,k,exchange;  
  13.     RecType tmp;  
  14.     for (i=0; i<n-1; i++)  
  15.     {  
  16.         exchange=0;  
  17.         for (j=n-1; j>i; j--)   //比较,找出最小关键字的记录  
  18.             if (R[j].key<R[j-1].key)  
  19.             {  
  20.                 tmp=R[j];  //R[j]与R[j-1]进行交换,将最小关键字记录前移  
  21.                 R[j]=R[j-1];  
  22.                 R[j-1]=tmp;  
  23.                 exchange=1;  
  24.             }  
  25.   
  26.         printf("i=%d: ",i);  
  27.         for (k=0; k<n; k++)  
  28.             printf("%d ",R[k].key);  
  29.         printf("\n");  
  30.   
  31.         if (exchange==0)    //中途结束算法  
  32.             return;  
  33.     }  
  34. }  
  35. int main()  
  36. {  
  37.     int i,n=10;  
  38.     RecType R[MaxSize];  
  39.     KeyType a[]= {0,1,7,2,5,4,3,6,8,9};  
  40.     for (i=0; i<n; i++)  
  41.         R[i].key=a[i];  
  42.     printf("排序前:");  
  43.     for (i=0; i<n; i++)  
  44.         printf("%d ",R[i].key);  
  45.     printf("\n");  
  46.     BubbleSort1(R,n);  
  47.     printf("排序后:");  
  48.     for (i=0; i<n; i++)  
  49.         printf("%d ",R[i].key);  
  50.     printf("\n");  
  51.     return 0;  
  52. }  
0 0
原创粉丝点击