• 四种插入排序


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

    目录

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

  • 相关阅读:
    使用requests库解决Session对象设置超时的问题
    C#医学检验室(LIS)信息管理系统源码
    电脑软件:推荐一款非常实用的固态硬盘优化工具
    ARM Linux DIY(十一)板子名称、开机 logo、LCD 控制台、console 免登录、命令提示符、文件系统大小
    致远oa wpsassistservlet任意文件上传漏洞
    java计算机毕业设计微服务在线考试源码+系统+数据库+lw文档
    第4节-PhotoShop基础课程-Ps格式
    为何vxlan需要封装在UDP里而不是直接使用IP包封装?
    推荐系统(业务侧)小结
    DPDK helloworld示例程序
  • 原文地址:https://blog.csdn.net/qq_61735602/article/details/127714020