成都创新互联网站制作重庆分公司

C-单链表-快速排序,冒泡排序

struct ST_QUEUE
{
    int data; 
    struct ST_QUEUE* pNext; // 指针域
};
typedef struct ST_QUEUE Queue; 
void swapNode(Queue *p1, Queue *p2)
{
    Queue* tmp = (Queue*)malloc(sizeof(Queue));
    tmp->data = p1->data;
    p1->data = p2->data;
    p2->data = tmp->data;
}
Queue* getEnd(Queue* que)
{
    if (que == NULL || que->pNext == NULL) return que;
    while (que->pNext != NULL)
    {
        que = que->pNext;
    }
    return que;
}

// 快速排序
void quicksort(Queue* pHead, Queue* pEnd)
{
    if (NULL == pHead || NULL == pEnd || pHead == pEnd) { return; }
    Queue* p1 = pHead;
    Queue* p2 = p1->pNext;
    int pivot = pHead->data;
    while (p2 != pEnd->pNext && p2 != NULL)
    {
        if (p2->data > pivot)
        {
            p1 = p1->pNext;
            swapNode(p1, p2);
        }
        p2 = p2->pNext;
    }
    swapNode(pHead, p1);
    quicksort(pHead, p1);
    quicksort(p1->pNext, pEnd);
}
// 冒泡排序
void bubblesort1(Queue* que)
{
    if (que == NULL || que->pNext == NULL) return que;
    // 冒泡排序
    for (Queue* p1 = que; p1 != NULL; p1 = p1->pNext)
    {
        for (Queue* p2 = p1->pNext; p2 != NULL; p2 = p2->pNext)
        {
            if (p1->data < p2->data)
            { 
                swapNode(p1, p2);
            }
        }
    }
}
// 递归冒泡排序
void bubblesort2(Queue* head){
    if (head == NULL )            
    {
        return;
    }
    Queue* p = head->pNext;              
    while (p != NULL){
        if (p->data > head->data){    
            swapNode(head, p);
        }
        p = p->pNext;
    }
    bubblesort2(head->pNext);
}

新闻标题:C-单链表-快速排序,冒泡排序
网址分享:http://cxhlcq.com/article/jpphos.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部