C语言单链表的3种排序算法,插入排序,冒泡排序,选择排序

//插入排序
stu *view_sort_math(stu *head)  
{  
    struct student *first;
    struct student *t;  
    struct student *p;   
    struct student *q;   

    first = head->next;   
    head->next = NULL;

    while (first != NULL)   
    {  
        for (t = first, q = head; ((q != NULL) && (q->chinese > t->chinese)); p = q, q = q->next);   

        first = first->next;

        if (q == head)   
        {  
            head = t;  
        }  
        else
        {  
            p->next = t;  
        }  
        t->next = q;

    }  
    return head;  
}

//冒泡排序
stu *view_sort_chinese(stu *h)         
{
    stu *endpt,*u,*v,*p;     
    u = (stu *)malloc(sizeof(stu));   
    u->next=h;   
    h=u;   
    for(endpt=NULL;endpt!=h;endpt=p)
    {
        for(p=u=h;u->next->next!=endpt;u=u->next)  
        {
            if(u->next->chinese < u->next->next->chinese)
            {                            
                v=u->next->next;   
                u->next->next=v->next;   
                v->next=u->next;   
                u->next=v;   
                p=u->next->next;   
            }
        }
    }
    u=h;   
    h=h->next;       
    return h;
}

//选择排序
stu *view_sort_c(stu *shead)
{  
    struct student *pfirst;       
    struct student *ptail;         
    struct student *pminBefore;  
    struct student *pmin;         
    struct student *p;             

    pfirst = NULL;  
    while (shead != NULL)         
    {      
        for (p = shead, pmin = shead; p->next != NULL; p = p->next)
        {  
            if (p->next->math > pmin->math)
            {  
                pminBefore = p;           
                pmin       = p->next;      
            }  
        }     

        if (pfirst == NULL)     
        {  
            pfirst = pmin;      
            ptail  = pmin;      
        }  
        else                    
        {  
            ptail->next = pmin;
            ptail = pmin;       
        }  


        if (pmin == shead)        
        {  
            shead = shead->next;   
        }  
        else   
        {  
            pminBefore->next = pmin->next;   
        }  
    }  

    if (pfirst != NULL)       
    {  
        ptail->next = NULL;
    }  
    shead = pfirst;  
    print_stu_list(shead);
    return shead;
}
  • 11
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
下面是一个使用 C 语言实现单链表冒泡排序的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 单链表节点结构 struct Node { int data; struct Node* next; }; // 在链表末尾插入节点 void insert(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; if (*head_ref == NULL) { *head_ref = new_node; return; } struct Node* last = *head_ref; while (last->next != NULL) { last = last->next; } last->next = new_node; } // 交换链表中两个节点的数据 void swap(struct Node* a, struct Node* b) { int temp = a->data; a->data = b->data; b->data = temp; } // 使用冒泡排序算法对链表进行排序 void bubbleSort(struct Node* head) { int swapped, i; struct Node* ptr1; struct Node* lptr = NULL; if (head == NULL) return; do { swapped = 0; ptr1 = head; while (ptr1->next != lptr) { if (ptr1->data > ptr1->next->data) { swap(ptr1, ptr1->next); swapped = 1; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); } // 打印链表中的数据 void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } // 主函数 int main() { struct Node* head = NULL; insert(&head, 5); insert(&head, 3); insert(&head, 8); insert(&head, 1); insert(&head, 6); printf("原始链表:"); printList(head); bubbleSort(head); printf("排序后链表:"); printList(head); return 0; } ``` 这段代码中,我们首先定义了一个 `Node` 结构体作为链表节点的数据结构。然后实现了 `insert` 函数用于在链表末尾插入节点,`swap` 函数用于交换链表中两个节点的数据。 冒泡排序算法的实现是通过遍历链表多次,每次比较相邻两个节点的数据,并根据需要交换它们的位置。函数 `bubbleSort` 是实现冒泡排序的主要逻辑。 最后,我们使用 `main` 函数来测试这段代码。我们在链表中插入一些数据,并打印原始链表,然后使用 `bubbleSort` 函数对链表进行排序,最后再次打印排序后的链表。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值