链表上的冒泡选择排序算法

Posted loefairy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链表上的冒泡选择排序算法相关的知识,希望对你有一定的参考价值。

做如下定义

typedef struct LNode
{
    int num;
    char name[20];
    struct LNode *next;
} LNode, *LinkList;

冒泡排序算法

void BubbleSort(LinkList &L)
{
    LinkList i = NULL, j = NULL;
    for (i = L; i->next->next != NULL; i = i->next)
    {
        for (j =i ; j->next->next != NULL; j = j->next)
        {
            if (j->next->num > j->next->next->num)
            {
                LinkList a = NULL, b = NULL,c = NULL;
                a = j->next;
                b = j->next->next;
                c = j->next->next->next;
                j->next = b;
                j->next->next = a;
                j->next->next->next = c;
            }
        }
    }
}

选择排序算法

void SelectSort(LinkList &L)
{
    LinkList i=NULL, j=NULL, min_n=NULL;
    int k = 1;
    for (i = L; i->next!=NULL; i=i->next)
    {
        min_n = i;
        for (j = i ->next; j->next!=NULL; j=j->next)
        {
            if (min_n->next->num > j->next->num)
            {
                min_n = j;
            }
        }
        if (min_n->next->num != i->next->num)
        {
            if (i->next != min_n)
            {
                LinkList a = NULL, b = NULL, c = NULL, d = NULL;
                a = i->next;b = i->next->next;
                c = min_n->next;d = min_n->next->next;

                i->next = c;i->next->next = b;
                min_n->next = a;min_n->next->next = d;
            }
            else
            {
                LinkList a = NULL, b = NULL, c = NULL;
                a = i->next;b = i->next->next;
                c = i->next->next->next;

                i->next = b;i->next->next = a;
                i->next->next->next = c;
            }

        }
    }
}

测试代码

#define _CRT_SECURE_NO_WARNINGS
//此行系VS2017,读者请合情取舍
#include<iostream>
#include<cstdlib>
#include<string.h> 
using namespace std;
typedef struct LNode
{
    int num;
    char name[20];
    struct LNode *next;
} LNode, *LinkList;

void Show(LinkList L)
{
    LinkList p = L->next;
    while (p != NULL)
    {
        cout << p->num << "	  " ;
        p = p->next;
    }
    cout << "
";
    p = L->next;
    while (p != NULL)
    {
        cout << p->name << "	";
        p = p->next;
    }
    cout << "
";
}

void BubbleSort(LinkList &L)
{
    LinkList i = NULL, j = NULL;
    int k = 1;
    for (i = L; i->next->next != NULL; i = i->next)
    {
        for (j =i ; j->next->next != NULL; j = j->next)
        {
            if (j->next->num > j->next->next->num)
            {
                LinkList a = NULL, b = NULL,c = NULL;
                a = j->next;
                b = j->next->next;
                //a、b保存要交换的两个节点
                c = j->next->next->next;
                //c保存j->next->next最后完成链表的连接
                j->next = b;
                j->next->next = a;
                j->next->next->next = c;
            }
        }
        //cout << "第" << k++ << "趟
";
        //Show(L);
    }
}
void SelectSort(LinkList &L)
{
    LinkList i=NULL, j=NULL, min_n=NULL;
    int k = 1;
    //置min_next->next指向含最小值的结点
    for (i = L; i->next!=NULL; i=i->next)
    {
        min_n = i;
        for (j = i ->next; j->next!=NULL; j=j->next)
        {//查找最小值,并标记
            if (min_n->next->num > j->next->num)
            {
                min_n = j;
            }
        }
        if (min_n->next->num != i->next->num)
        {//这儿的交换分成了两种情况,1.不相邻交换 2.相邻交换
            //(相邻交换和冒泡算法的交换相同)
            //这部分写的繁了我相信聪明的读者可以优化它
            if (i->next != min_n)
            {
                LinkList a = NULL, b = NULL, c = NULL, d = NULL;
                a = i->next;
                b = i->next->next;
                c = min_n->next;
                d = min_n->next->next;

                i->next = c;
                i->next->next = b;
                min_n->next = a;
                min_n->next->next = d;
            }
            else
            {
                LinkList a = NULL, b = NULL, c = NULL;
                a = i->next;
                b = i->next->next;
                c = i->next->next->next;

                i->next = b;
                i->next->next = a;
                i->next->next->next = c;
            }

        }
        //cout << "第" << k++ << "趟
";
        //Show(L);
    }
}
void Init(LinkList &L)
{
    LinkList l1, l2, l3, l4,l5;

    L = (LinkList)malloc(sizeof(LNode));
    l1 = (LinkList)malloc(sizeof(LNode));
    l2 = (LinkList)malloc(sizeof(LNode));
    l3 = (LinkList)malloc(sizeof(LNode));
    l4 = (LinkList)malloc(sizeof(LNode));
    l5 = (LinkList)malloc(sizeof(LNode));

    l1->num = 10; strcpy(l1->name, "10s"); l1->next = NULL;
    l2->num = 9; strcpy(l2->name, "9s"); l2->next = NULL;
    l3->num = 11; strcpy(l3->name, "11s"); l3->next = NULL;
    l4->num = 8; strcpy(l4->name, "8s"); l4->next = NULL;
    l5->num = 12; strcpy(l5->name, "12s"); l5->next = NULL;

    L->next = l1;   l1->next = l2; l2->next = l3;
    l3->next = l4; l4->next = l5;
}
int main()
{
    LinkList L;

    //Init(L);
    //cout << "链表
";
    //Show(L);
    //BubbleSort(L);
    //cout << "冒泡
";
    //Show(L);

    Init(L);
    cout << "链表
";
    Show(L);
    SelectSort(L);
    cout << "选择
";
    Show(L);


    cout << "
End!";
    return 0;
}

以上是关于链表上的冒泡选择排序算法的主要内容,如果未能解决你的问题,请参考以下文章

双链表上的冒泡排序

常用排序算法之冒泡排序选择排序

链表排序(冒泡选择插入快排归并希尔堆排序)

链表选择排序的C语言算法实现

编程-链表之希尔排序堆排序归并排序快速排序

经典算法学习——链表实现冒泡排序