溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C-單鏈表-快速排序,冒泡排序

發(fā)布時(shí)間:2020-07-13 16:41:35 來源:網(wǎng)絡(luò) 閱讀:4380 作者:DRT_GOTHANG 欄目:編程語言
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);
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI