链表的排序算法

介绍:交换节点法的冒泡排序及优化

1.数组实现

#include <cstdio>
typedef long long ll;
using namespace std;

void out(ll a[], ll n)
{
    for(int i = 0; i < n; i++)
        printf("%lld ", a[i]);
    printf("\n");

}
void bubble_sort(ll *a, ll n)
{
    int pos = n-1;//每一趟的范围
    while(pos)
    {
        int last = 0;//重置交换的最后位置
        for(int i = 0; i < pos; i++)//优化的地方,从每次-1提高到最后交换的位置
        {
            if(a[i] > a[i+1])
            {
                int t = a[i];
                a[i] = a[i+1];
                a[i+1] = t;
                last = i;
            }
        }
        pos = last;//记录最后一次交换的位置,后面的由传递性可知是有序的
        printf("%d\n", last);
    }
     out(a, n);
}
void bad_bubble_sort(ll *a, ll n)
{
    for(int i = 0; i < n; i++)
    {
        bool flag = false;
        for(int j = i; j < n-1; j++)
        {
            if(a[j] > a[j+1])
            {
                int t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
                flag = true;
            }
        }
        if(!flag) break;
    }
     out(a, n);
}
int main()
{
    ll n, a[100];
    while(~scanf("%lld", &n))
    {
        for(int i = 0; i < n; i++)
            scanf("%lld", &a[i]);
        bubble_sort(a, n);
        bad_bubble_sort(a, n);
    }
}
/*
10
3 2 1 4 4 4 4 4 4 4 4
*/

2.链表实现

#include <cstdio>
#include <cstdlib>
#include <conio.h>
using namespace std;
typedef struct _Node
{
    int num;
    _Node *next;
}Node;
Node *CreateLink()
{
    Node *head, *tail;
    head = (Node*)malloc(sizeof(Node));
    tail = NULL;
    head->next = NULL;
    printf("输入数据的组数\n");
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        Node *p = (Node*)malloc(sizeof(Node));
        scanf("%d", &p->num);
        p->next = NULL;
        if(tail == NULL)
            head->next = p;
        else
            tail->next = p;
        tail = p;
    }

    return head;
}
void print(Node *head)
{
    Node *p;
    for(p = head->next; p; p = p->next)
        printf("%d\n", p->num);
}
/*
void Bubble_sort1(Node *head)//无优化的Bubble_sort
{
    int cnt = 0;
    Node *pre, *p, *tail;
    tail = NULL;
    while(head->next != tail)//当头结点的指针域不等于最后交换的元素时
    {
        cnt++;
        pre = head;//前驱指针指向头节点(2个交换元素的前一个元素)
        p = head->next;//目标指针指向首元元素,即第一个放数据的元素
        while(p->next != tail)
        {
            if(p->num > p->next->num)//比较目标元素和它的下一个元素
            {
                //交换步骤画图有助理解
                pre->next = p->next;//1连3
                p->next = pre->next->next;//2连4
                pre->next->next = p;//2连3
            }
            else
                p = p->next;//如果交换了就不用执行这一步,因为p就被交换到下一位了(画图)
            pre = pre->next;//前驱指针后移, 别漏了
        }
        tail = p;//每次向左缩进一位
    }
    printf("Bubble_sort1执行了%d趟\n", cnt);
}
*/
void Bubble_sort(Node *head)//优化的Bubble_sort
{
    int cnt = 0;
    Node *pre, *p, *pos;//pos表示每一趟的范围
    pos = NULL;
    while(head->next != pos)//当头结点的指针域不等于最后交换的元素时
    {
        cnt++;
        pre = head;//前驱指针指向头节点(2个交换元素的前一个元素)
        p = head->next;//目标指针指向首元元素,即第一个放数据的元素
        Node *last = head->next;//重置交换的最后位置为首元元素
        while(p->next != pos)
        {
            if(p->num > p->next->num)//比较目标元素和它的下一个元素
            {
                //交换步骤画图有助理解
                pre->next = p->next;//1连3
                p->next = pre->next->next;//2连4
                pre->next->next = p;//2连3
                last = p;
            }
            else
                p = p->next;//如果交换了就不用执行这一步,因为p就被交换到下一位了(画图)
            pre = pre->next;//前驱指针后移, 别漏了
        }
        pos = last;//每次向左缩进一位
    }
    printf("Bubble_sort执行了%d趟\n", cnt);
}
void Clear(Node *head)
{
    Node *p,*q;
    for(p = head; p; p = q)
    {
        q = p->next;
        free(p);
    }
}
int main()
{
    while(1)
    {
        Node *head;
        head = CreateLink();
        printf("排序前\n");
        print(head);
        //Bubble_sort1(head);
        Bubble_sort(head);
        printf("排序后\n");
        print(head);
        Clear(head);
        printf("按任意键刷新屏幕\n");
        getch();
        system("cls");
    }

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表排序可以使用多种算法实现,下面是一种基于插入排序链表排序算法的C语言实现: ```c #include <stdio.h> #include <stdlib.h> /* 定义链表节点结构体 */ typedef struct ListNode { int val; struct ListNode *next; } ListNode; /* 插入排序算法实现 */ ListNode* insertionSortList(ListNode* head) { if (!head || !head->next) return head; // 链表为空或只有一个节点,直接返回 ListNode *dummy = (ListNode *)malloc(sizeof(ListNode)); // 创建哑节点 dummy->next = head; ListNode *p = head->next, *q = head; // p指向要插入的节点,q指向p的前一个节点 while (p) { ListNode *r = dummy; // r指向已排序部分的哑节点 while (r != q && r->next->val <= p->val) r = r->next; // 在已排序部分中找到插入位置 if (r != q) { q->next = p->next; p->next = r->next; r->next = p; p = q->next; } else { p = p->next; q = q->next; } } head = dummy->next; free(dummy); // 释放哑节点 return head; } /* 创建链表 */ ListNode* createList(int arr[], int n) { if (n == 0) return NULL; ListNode *head = (ListNode *)malloc(sizeof(ListNode)); head->val = arr[0]; head->next = NULL; ListNode *p = head; for (int i = 1; i < n; i++) { ListNode *q = (ListNode *)malloc(sizeof(ListNode)); q->val = arr[i]; q->next = NULL; p->next = q; p = p->next; } return head; } /* 打印链表 */ void printList(ListNode *head) { while (head) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int arr[] = {3, 1, 4, 2, 5}; int n = sizeof(arr) / sizeof(int); ListNode *head = createList(arr, n); printf("原链表:"); printList(head); head = insertionSortList(head); printf("排序后:"); printList(head); return 0; } ``` 该程序中,insertionSortList函数实现了链表的插入排序算法,createList函数用来创建链表,printList函数用来打印链表。在main函数中,先创建一个链表,然后打印原链表,对链表进行排序,最后再打印排序后的链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值