当前位置:   article > 正文

第十五周项目四 验证算法——直接插入排序_实验十五 插入排序与选择排序

实验十五 插入排序与选择排序

问题及代码:

  1. /*
  2. *烟台大学计算机与控制工程学院
  3. *作 者:孙丽玮
  4. *完成日期:2016年12月9日
  5. *问题描述:用序列{57,40,38,11,13,34,48,75,6,19,9,7}作为测试数据,运行并本周视频中所讲过的算法对应程序,观察运行结果并深刻领会算法的思路和实现方法。
  6. (1)直接插入排序;(2)希尔排序;(3)冒泡排序;(4)快速排序;(5)直接选择排序;(6)堆排序;(7)归并排序;(8)基数排序。
  7. */

1、直接插入排序

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

运行结果:


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

  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 InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序
  11. {
  12. int i,j,k;
  13. RecType tmp;
  14. for (i=1; i<n; i++)
  15. {
  16. tmp=R[i];
  17. j=i-1; //从右向左在有序区R[0..i-1]中找R[i]的插入位置
  18. while (j>=0 && tmp.key<R[j].key)
  19. {
  20. R[j+1]=R[j]; //将关键字大于R[i].key的记录后移
  21. j--;
  22. }
  23. R[j+1]=tmp; //在j+1处插入R[i]
  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=12;
  33. RecType R[MaxSize];
  34. KeyType a[]={57,40,38,11,13,34,48,75,6,19,9,7};
  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. InsertSort(R,n);
  42. printf("排序后:");
  43. for (i=0; i<n; i++)
  44. printf("%d ",R[i].key);
  45. printf("\n");
  46. return 0;
  47. }

运行结果:


3、折半插入排序

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

运行结果:


总结:

直接插入排序:每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序。

折半插入排序:在直接插入排序的基础上,不用按顺序一次寻找插入的,可以采用折半查找的方法来加快寻找插入点的速度。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/788152
推荐阅读
相关标签
  

闽ICP备14008679号