• 四种插入排序


    每个标题下分别给出主函数和测试用例,总的排序代码放在最后,表排序有详细注释,方便理解。

    目录

    第1关直接插入排序

    第2关二分插入排序

    第3关表插入排序

    第4关shell排序

    插入排序代码


    第1关直接插入排序

    1. int main(void)
    2. {
    3. int cnt ;
    4. SortObject *p ;
    5. p = (SortObject*)malloc(sizeof(SortObject));
    6. linkObject *head ;
    7. head = (linkObject*)malloc(sizeof(linkObject));
    8. head->next = NULL;
    9. scanf("%d",&cnt);
    10. p->NUM = cnt ;
    11. p->data = (DataType*)malloc(sizeof(DataType)*cnt);
    12. printf("the result of listSort:\n");
    13. for(int i=0;i
    14. {
    15. scanf("%d",&p->data[i]);
    16. }
    17. insertSort(p);
    18. }

    测试输入:

    8 49 38 65 97 76 113 27 49

    —— 预期输出 ——

    the result of listSort:

    38 49 65 97 76 113 27 49

    38 49 65 97 76 113 27 49

    38 49 65 97 76 113 27 49

    38 49 65 76 97 113 27 49

    38 49 65 76 97 113 27 49

    27 38 49 65 76 97 113 49

    27 38 49 49 65 76 97 113

    第2关二分插入排序

    1. int main(void)
    2. {
    3. int cnt ;
    4. SortObject *p ;
    5. p = (SortObject*)malloc(sizeof(SortObject));
    6. linkObject *head ;
    7. head = (linkObject*)malloc(sizeof(linkObject));
    8. head->next = NULL;
    9. scanf("%d",&cnt);
    10. p->NUM = cnt ;
    11. p->data = (DataType*)malloc(sizeof(DataType)*cnt);
    12. //printf("the result of binInsertSort:\n");
    13. for(int i=0;i
    14. {
    15. scanf("%d",&p->data[i]);
    16. }
    17. binInsertSort(p);
    18. }

    测试输入:

    8 38 49 65 97 76 113 27 49

    —— 预期输出 ——

    the result of binInsertSort:

    38 49 65 97 76 113 27 49

    38 49 65 97 76 113 27 49

    38 49 65 97 76 113 27 49

    38 49 65 76 97 113 27 49

    38 49 65 76 97 113 27 49

    27 38 49 65 76 97 113 49

    27 38 49 49 65 76 97 113

    第3关表插入排序

    1. int main(void)
    2. {
    3. int data ;
    4. int cnt ;
    5. linkObject *head,*p ;
    6. head = (linkObject*)malloc(sizeof(linkObject));
    7. head->next = NULL;
    8. scanf("%d",&cnt);
    9. for(int i=0;i
    10. {
    11. scanf("%d",&data);
    12. p = (linkObject*)malloc(sizeof(linkObject));
    13. p->next = head->next;
    14. p->info = data;
    15. head->next = p ;
    16. }
    17. printf("the result of linkSort:\n");
    18. listSort(head);
    19. }

    第4关shell排序

    1. int main(void)
    2. {
    3. int cnt ;
    4. SortObject *p ;
    5. p = (SortObject*)malloc(sizeof(SortObject));
    6. linkObject *head ;
    7. head = (linkObject*)malloc(sizeof(linkObject));
    8. head->next = NULL;
    9. scanf("%d",&cnt);
    10. p->NUM = cnt ;
    11. p->data = (DataType*)malloc(sizeof(DataType)*cnt);
    12. for(int i=0;i
    13. {
    14. scanf("%d",&p->data[i]);
    15. }
    16. shellSort(p,cnt/2);
    17. }

    测试输入:

    8 49 38 65 97 13 76 27 49

    —— 预期输出 ——

    13 38 27 49 49 76 65 97

    13 38 27 49 49 76 65 97

    13 27 38 49 49 65 76 97

    插入排序代码

    1. #include
    2. #include
    3. /*数据结构定义*/
    4. typedef int DataType;
    5. typedef struct
    6. {
    7. DataType *data; //用于存放待排序关键字的起始地址
    8. int NUM; //待排序关键字的个数
    9. } SortObject;
    10. typedef struct node //用于表插入排序的数据结构
    11. {
    12. DataType info;
    13. struct node *next;
    14. } linkObject;
    15. //输出顺序表
    16. void print(SortObject *p)
    17. {
    18. for(int i=0;iNUM;i++)
    19. printf("%d ",p->data[i]);
    20. printf("\n");
    21. }
    22. //输出链表
    23. void printLink(linkObject *Head)
    24. {
    25. linkObject *p = Head->next ;
    26. while(p)
    27. {
    28. printf("%d ",p->info);
    29. p = p->next;
    30. }
    31. printf("\n");
    32. }
    33. /*第一关
    34. 此处请填写代码实现递增序进行直接插入排序,
    35. 要求每趟排序后 调用print函数,输出关键字的排列情况*/
    36. void insertSort( SortObject *Rec )
    37. {
    38. /*----begin------*/
    39. DataType temp;
    40. DataType *data = Rec->data;
    41. int i,j;
    42. for(i=1;iNUM;i++){
    43. temp = data[i];
    44. for(j = i-1 ; j >= 0 && temp
    45. data[j+1] = data[j];
    46. }
    47. data[j+1] = temp;
    48. print(Rec);
    49. }
    50. /*-----end------*/
    51. }
    52. /*第二关
    53. 此处请填写代码实现递增序进行二分插入排序,
    54. 实质是在已经有序的表中采用二分法查找插入位置
    55. 要求每趟排序后 调用print函数,输出关键字的排列情况*/
    56. void binInsertSort( SortObject *Rec )
    57. {
    58. printf("the result of binInsertSort:\n");
    59. /*----begin------*/
    60. int i,j,left,mid,right;
    61. DataType *data = Rec->data;
    62. for(i = 1;iNUM;i++){
    63. DataType temp = data[i];
    64. left = 0,right = i-1;
    65. while(left<=right){
    66. mid = (left+right)>>1;
    67. if(temp>data[mid])left = mid+1;
    68. else right = mid - 1;
    69. }
    70. for(j = i-1;j>=left;j--)
    71. data[j+1] = data[j];
    72. data[left] = temp;
    73. print(Rec);
    74. }
    75. /*-----end------*/
    76. }
    77. /* 第四关
    78. 此处请填写代码实现递增序进行shell排序,
    79. 要求每趟排序后 调用print函数,输出关键字的排列情况
    80. */
    81. void shellSort( SortObject *Rec,int d )
    82. {
    83. int i,j,inc;
    84. DataType* temp;
    85. DataType* data = Rec->data;
    86. for(inc = d ; inc>0;inc/=2){
    87. for(i = inc;iNUM;i++){
    88. temp = data[i];
    89. //这边都是inc范围的移动
    90. for(j = i - inc;j>=0&&data[j]>temp;j-=inc)
    91. data[j+inc] = data[j];
    92. data[j+inc] = temp;
    93. }
    94. print(Rec);
    95. }
    96. }
    97. /*第三关
    98. 此处请填写代码实现递增序进行表插入排序,
    99. 返回值是关键字比较次数
    100. Head是表头结点,不存放数据,info是待插入数据
    101. 要求每趟排序后 调用printLink函数,输出关键字的排列情况
    102. */
    103. void listSort(linkObject *plist )
    104. {
    105. /*----begin------*/
    106. linkObject *head,*p,*q,*pre,*now;
    107. head = plist;
    108. pre = head->next;
    109. if(pre==NULL)return;
    110. now = pre ->next;
    111. if(now==NULL)return;
    112. printLink(plist);
    113. while(now!=NULL){
    114. (void)(q = head),p = q->next;
    115. while(p!=now&&p->infoinfo){q = q->next,p = q->next;}
    116. if(p==now){pre = pre->next;now = pre->next; continue;}
    117. pre->next = now->next;
    118. q->next = now;
    119. now->next = p;
    120. now = pre->next;
    121. printLink(plist);
    122. }
    123. /*-----end------*/
    124. }

  • 相关阅读:
    本地wsl的Ubuntu安装docker,不使用docker桌面版
    基于强化学习的空战辅助决策环境搭建(2D)
    值得推荐的小型 C 语言开源项目:Triggerhappy
    医疗服务全面升级,这个方法绝了!
    计算机毕设(附源码)JAVA-SSM基于的小型房屋租赁平台
    flutter 混合开发 module 依赖
    如何选择适合自己练手的 Java 源码项目?
    Docker和debezium是什么关系,如何部署?
    JAVA:实现Pow函数功能算法(附完整源码)
    Cadence23学习笔记(三)
  • 原文地址:https://blog.csdn.net/qq_61735602/article/details/127714020