第十六周项目1-验证算法(3)冒泡排序

来源:互联网 发布:网络干货分享 编辑:程序博客网 时间:2024/06/05 03:35
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*   
  2.  * Copyright (c) 2016, 烟台大学计算机与控制工程学院   
  3.  * All rights reserved。   
  4.  * 文件名称 :1.cpp   
  5.  * 作    者 :杨俊杰 
  6.  * 完成日期 :2016年 12月15日   
  7.  * 版 本 号 :v1.0   
  8.  * 问题描述 : 
  9.  * 输出描述 : 
  10.  */  
  11. #include <stdio.h>  
  12. #define MaxSize 20  
  13. typedef int KeyType;    //定义关键字类型  
  14. typedef char InfoType[10];  
  15. typedef struct          //记录类型  
  16. {  
  17.     KeyType key;        //关键字项  
  18.     InfoType data;      //其他数据项,类型为InfoType  
  19. } RecType;              //排序的记录类型定义  
  20.   
  21. void BubbleSort(RecType R[],int n)  
  22. {  
  23.     int i,j,k;  
  24.     RecType tmp;  
  25.     for (i=0; i<n-1; i++)  
  26.     {  
  27.         for (j=n-1; j>i; j--)   //比较,找出本趟最小关键字的记录  
  28.             if (R[j].key<R[j-1].key)  
  29.             {  
  30.                 tmp=R[j];  //R[j]与R[j-1]进行交换,将最小关键字记录前移  
  31.                 R[j]=R[j-1];  
  32.                 R[j-1]=tmp;  
  33.             }  
  34.         printf("i=%d: ",i);  
  35.         for (k=0; k<n; k++)  
  36.             printf("%d ",R[k].key);  
  37.         printf("\n");  
  38.     }  
  39. }  
  40. int main()  
  41. {  
  42.     int i,n=10;  
  43.     RecType R[MaxSize];  
  44.     KeyType a[]= {9,8,7,6,5,4,3,2,1,0};  
  45.     for (i=0; i<n; i++)  
  46.         R[i].key=a[i];  
  47.     printf("排序前:");  
  48.     for (i=0; i<n; i++)  
  49.         printf("%d ",R[i].key);  
  50.     printf("\n");  
  51.     BubbleSort(R,n);  
  52.     printf("排序后:");  
  53.     for (i=0; i<n; i++)  
  54.         printf("%d ",R[i].key);  
  55.     printf("\n");  
  56.     return 0;  
  57. }  

运行结果:


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

[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