第16周-项目1—直接插入排序

来源:互联网 发布:qt tcp客户端编程 编辑:程序博客网 时间:2024/06/04 19:01

问题及代码:

  1. /**Copyright (c) 2016, 烟台大学计算机与控制工程学院 
  2. * All rights reserved. 
  3. * 文件名称:main.cpp 
  4. * 作者:张冰 
  5. * 完成日期:2016年12月15日 
  6. * 版本号:code ::Block 12.11 
  7. * 问题描述:验证直接插入排序算法 
  8. * 输入描述:无 
  9. * 程序输出:输出排序前和排序后的序列 
  10.  
  11. */  
  12.   
  13. #include <stdio.h>  
  14. #define MaxSize 20  
  15. typedef int KeyType;    //定义关键字类型  
  16. typedef char InfoType[10];  
  17. typedef struct          //记录类型  
  18. {  
  19.     KeyType key;        //关键字项  
  20.     InfoType data;      //其他数据项,类型为InfoType  
  21. } RecType;              //排序的记录类型定义  
  22.   
  23. void InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序  
  24. {  
  25.     int i,j;  
  26.     RecType tmp;  
  27.     for (i=1; i<n; i++)  
  28.     {  
  29.         tmp=R[i];  
  30.         j=i-1;            //从右向左在有序区R[0..i-1]中找R[i]的插入位置  
  31.         while (j>=0 && tmp.key<R[j].key)  
  32.         {  
  33.             R[j+1]=R[j]; //将关键字大于R[i].key的记录后移  
  34.             j--;  
  35.         }  
  36.         R[j+1]=tmp;      //在j+1处插入R[i]  
  37.     }  
  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.     InsertSort(R,n);  
  52.     printf("排序后:");  
  53.     for (i=0; i<n; i++)  
  54.         printf("%d ",R[i].key);  
  55.     printf("\n");  
  56.     return 0;  
  57. }  
运行结果:


  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 InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序  
  12. {  
  13.     int i,j,k;  
  14.     RecType tmp;  
  15.     for (i=1; i<n; i++)  
  16.     {  
  17.         tmp=R[i];  
  18.         j=i-1;            //从右向左在有序区R[0..i-1]中找R[i]的插入位置  
  19.         while (j>=0 && tmp.key<R[j].key)  
  20.         {  
  21.             R[j+1]=R[j]; //将关键字大于R[i].key的记录后移  
  22.             j--;  
  23.         }  
  24.         R[j+1]=tmp;      //在j+1处插入R[i]  
  25.         printf("i=%d: ",i);  
  26.         for (k=0; k<n; k++)  
  27.             printf("%d ",R[k].key);  
  28.         printf("\n");  
  29.     }  
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     int i,n=10;  
  35.     RecType R[MaxSize];  
  36.     KeyType a[]= {9,8,7,6,5,4,3,2,1,0};  
  37.     for (i=0; i<n; i++)  
  38.         R[i].key=a[i];  
  39.     printf("排序前:");  
  40.     for (i=0; i<n; i++)  
  41.         printf("%d ",R[i].key);  
  42.     printf("\n");  
  43.     InsertSort(R,n);  
  44.     printf("排序后:");  
  45.     for (i=0; i<n; i++)  
  46.         printf("%d ",R[i].key);  
  47.     printf("\n");  
  48.     return 0;  
  49. }  
运行结果:


知识点总结:

直接排序比较简单明了

0 0