嵌入式 用链表实现直接选择排序和直接插入排序示例

mysort()为直接插入排序,mysort2()为直接选择排序;

对于直接选择排序,我一开始是直接交换的结点里的数据,这个虽然是程序简单化了,但这和一个数组还有区别吗?所以我又改了交换结点,当然这就要烦的多了,一个有4种不同的情况~~~~~

  1. #include
  2. #include
  3. typedef structnode
  4. {
  5. int data;
  6. struct node*next;
  7. }NODE;
  8. NODE * mycreate()
  9. {
  10. NODE * head = NULL;
  11. returnhead;
  12. }
  13. NODE * myinsert(NODE *head,intdata)
  14. {
  15. NODE *q = head;
  16. if(head ==NULL)
  17. {
  18. head = (NODE *)malloc(sizeof(NODE));
  19. head->data = data;
  20. head->next = NULL;
  21. }
  22. else
  23. {
  24. while(q->next !=NULL)
  25. {
  26. q = q->next;
  27. }
  28. NODE *newnode = (NODE *)malloc(sizeof(NODE));
  29. newnode->data = data;
  30. q->next = newnode;
  31. newnode->next = NULL;
  32. }
  33. returnhead;
  34. }
  35. NODE * mysort(NODE *head)
  36. {
  37. NODE *newhead = mycreate();
  38. NODE *q = head;
  39. NODE * max;
  40. NODE * tmp = NULL;
  41. max = (NODE *)malloc(sizeof(NODE));
  42. max->data = head->data;
  43. NODE *maxpre = head;
  44. while(head !=NULL)
  45. {
  46. max->data = head->data;
  47. q = head;
  48. while(q->next !=NULL)
  49. {
  50. if(q->next->data >max->data)
  51. {
  52. max->data = q->next->data;
  53. tmp = q->next;
  54. maxpre = q;
  55. }
  56. q = q->next;
  57. }
  58. newhead = myinsert(newhead,max->data);
  59. if(tmp !=NULL)
  60. {
  61. maxpre->next = tmp->next;
  62. if(tmp ==head)
  63. {
  64. head = head->next;
  65. }
  66. free(tmp);
  67. tmp = NULL;
  68. }
  69. else
  70. {
  71. free(head);
  72. head = NULL;
  73. }
  74. }
  75. returnnewhead;
  76. }
  77. NODE * mysort2(NODE *head)
  78. {
  79. NODE *q = head;
  80. NODE *last = head;
  81. NODE *p = NULL;
  82. NODE *max = NULL;
  83. NODE *premax = NULL;
  84. NODE *nextmax = NULL;
  85. NODE *prelast = NULL;
  86. NODE *lastnext = NULL;
  87. int tmp;
  88. while(last->next !=NULL)
  89. {
  90. p = last;
  91. lastnext = last->next;
  92. max = p;
  93. while(p->next !=NULL)
  94. {
  95. if(p->next->data >max->data)
  96. {
  97. max = p->next;
  98. premax = p;
  99. }
  100. p = p->next;
  101. }
  102. if(last ==head)
  103. {
  104. nextmax = max->next;
  105. if(premax ==last)
  106. {
  107. last->next = nextmax;
  108. max->next = last;
  109. head = max;
  110. }
  111. else
  112. {
  113. premax->next = last;
  114. head->next = max->next;
  115. max->next = lastnext;
  116. head = max;
  117. }
  118. last = head->next;
  119. prelast = head;
  120. }
  121. else
  122. {
  123. nextmax = max->next;
  124. prelast->next = max;
  125. if(premax ==last)
  126. {
  127. max->next = last;
  128. last->next = nextmax;
  129. }
  130. else
  131. {
  132. premax->next = last;
  133. last->next = nextmax;
  134. max->next = lastnext;
  135. }
  136. last = prelast->next;
  137. prelast = last;
  138. last = last->next;
  139. }
  140. }
  141. returnhead;
  142. }
  143. void myprint(NODE*head)
  144. {
  145. if(head ==NULL)
  146. {
  147. printf("Empty !\n");
  148. }
  149. else
  150. {
  151. while(head->next !=NULL)
  152. {
  153. printf("%d\t",head->data);
  154. head = head->next;
  155. }
  156. printf("%d\n",head->data);
  157. }
  158. }
  159. intmain()
  160. {
  161. NODE *head = mycreate();
  162. head = myinsert(head,1);
  163. head = myinsert(head,2);
  164. head = myinsert(head,3);
  165. head = myinsert(head,4);
  166. head = myinsert(head,5);
  167. head = myinsert(head,6);
  168. printf("排序前:");
  169. myprint(head);
  170. head = mysort2(head);
  171. printf("排序后:");
  172. myprint(head);
  173. return 0;
  174. }
#include 
#include 

typedef struct node 
{
    int data;
    struct node *next;
}NODE;

NODE * mycreate()
{
    NODE * head = NULL;
    return head;
}

NODE * myinsert(NODE *head,int data)
{
    NODE *q = head;
    if(head == NULL)
    {
        head = (NODE *)malloc(sizeof(NODE));
        head->data = data;
        head->next = NULL;
    }
    else
    {
        while(q->next != NULL)
        {
            q = q->next;
        }
        NODE *newnode = (NODE *)malloc(sizeof(NODE));
        newnode->data = data;
        q->next = newnode;
        newnode->next = NULL;
    }
    return head;
}

NODE * mysort(NODE *head)
{
    NODE *newhead = mycreate();
    NODE *q = head;
    NODE * max;
    NODE * tmp = NULL;
    max = (NODE *)malloc(sizeof(NODE));
    max->data = head->data;
    NODE *maxpre = head;
    while(head != NULL)
    {
            max->data = head->data;
            q = head;
        while(q->next != NULL)
        {
            if(q->next->data > max->data)
            {
                max->data = q->next->data;
                tmp = q->next;
                maxpre = q;
            }
            q = q->next;
        }
        newhead = myinsert(newhead,max->data);
        if(tmp != NULL)
        {
            maxpre->next = tmp->next;
            if(tmp == head)
            {
                head = head->next;
            }
            free(tmp);
            tmp = NULL;
        }
        else
        {
            free(head);
            head = NULL;
        }
    }
    return newhead;
}

NODE * mysort2(NODE *head)
{
    NODE *q = head;
    NODE *last = head;
    NODE *p = NULL;
    NODE *max = NULL;
    NODE *premax = NULL;
    NODE *nextmax = NULL;
    NODE *prelast = NULL;
    NODE *lastnext = NULL;
    int tmp;
    while(last->next != NULL)
    {
        p = last;
        lastnext = last->next;
        max = p;
        while(p->next != NULL)
        {
            if(p->next->data > max->data)
            {
                max = p->next;
                premax = p;
            }
            p = p->next;
        }
        if(last == head)
        {
                nextmax = max->next;
                if(premax == last)
                {
                    last->next = nextmax;
                    max->next = last;
                    head = max;  
                }
                else
                {
                    premax->next = last;
                    head->next = max->next;
                    max->next = lastnext;
                    head = max;
                }
                last = head->next;
                prelast = head;
        }
        else
        {
            nextmax = max->next;
            prelast->next = max;
            if(premax == last)
            {
                max->next = last;
                last->next = nextmax;
            }
            else
            {
                premax->next = last;
                last->next = nextmax;
                max->next = lastnext;

            }
            last = prelast->next;
            prelast = last;
            last = last->next;
        }

    }
    return head;
}

void myprint(NODE *head)
{
    if(head == NULL)
    {
        printf("Empty !\n");
    }
    else
    {
        while(head->next != NULL)
        {
            printf("%d\t",head->data);
            head = head->next;
        }
        printf("%d\n",head->data);
    }
}
int main()
{
    NODE *head = mycreate();
    head = myinsert(head,1);
    head = myinsert(head,2);
    head = myinsert(head,3);
    head = myinsert(head,4);
    head = myinsert(head,5);
    head = myinsert(head,6);
    printf("排序前:");
    myprint(head);
    head = mysort2(head);
    printf("排序后:");
    myprint(head);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值