第十五周项目1-(2)验证直接插入排序算法

来源:互联网 发布:怎样查店铺直通车数据 编辑:程序博客网 时间:2024/06/05 04:03

问题及代码:

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /*    
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院    
  3. * All rights reserved.    
  4. * 文件名称:项目1-(2).cpp    
  5. * 作    者:朱建豪   
  6. * 完成日期:2016年12月27日    
  7. * 版 本 号:v1.0     
  8. *问题描述:验证直接插入排序,完成测试。     
  9. *输入描述:无    
  10. *程序输出:测试数据    
  11. */        


1.直接插入排序

代码:

[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 InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序      
  12. {      
  13.     int i,j;      
  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.     }      
  26. }      
  27.       
  28. int main()      
  29. {      
  30.     int i,n=10;      
  31.     RecType R[MaxSize];      
  32.     KeyType a[]= {9,8,7,6,5,4,3,2,1,0};      
  33.     for (i=0; i<n; i++)      
  34.         R[i].key=a[i];      
  35.     printf("排序前:");      
  36.     for (i=0; i<n; i++)      
  37.         printf("%d ",R[i].key);      
  38.     printf("\n");      
  39.     InsertSort(R,n);      
  40.     printf("排序后:");      
  41.     for (i=0; i<n; i++)      
  42.         printf("%d ",R[i].key);      
  43.     printf("\n");      
  44.     return 0;      
  45. }      

运行结果:


2.显示直接插入排序过程

代码:

[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 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. }    

运行结果:


3.折半插入排序

代码:

[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 InsertSort1(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序    
  12. {    
  13.     int i,j,low,high,mid;    
  14.     RecType tmp;    
  15.     for (i=1; i<n; i++)    
  16.     {    
  17.         tmp=R[i];    
  18.         low=0;    
  19.         high=i-1;    
  20.         while (low<=high)    
  21.         {    
  22.             mid=(low+high)/2;    
  23.             if (tmp.key<R[mid].key)    
  24.                 high=mid-1;    
  25.             else    
  26.                 low=mid+1;    
  27.         }    
  28.         for (j=i-1; j>=high+1; j--)    
  29.             R[j+1]=R[j];    
  30.         R[high+1]=tmp;    
  31.     }    
  32. }    
  33. int main()    
  34. {    
  35.     int i,n=10;    
  36.     RecType R[MaxSize];    
  37.     KeyType a[]= {9,8,7,6,5,4,3,2,1,0};    
  38.     for (i=0; i<n; i++)    
  39.         R[i].key=a[i];    
  40.     printf("排序前:");    
  41.     for (i=0; i<n; i++)    
  42.         printf("%d ",R[i].key);    
  43.     printf("\n");    
  44.     InsertSort1(R,n);    
  45.     printf("排序后:");    
  46.     for (i=0; i<n; i++)    
  47.         printf("%d ",R[i].key);    
  48.     printf("\n");    
  49.     return 0;    
  50. }    

运行结果:

0 0