단일 링크 정렬 (거품 정렬) (C 언어)

5171 단어 데이터 구조
최적화 판:
void SortList(PSListNode pHead)
{
    if (NULL == pHead)
    {
        return;
    }
    else
    {
        int flag = 0;
        PSListNode pTailNode = NULL;
        //                   ,           ,      
        while (pTailNode != pHead)
        {
            PSListNode pPreNode = pHead;
            //                 
            while (pPreNode->pNextNode != pTailNode)
            {
                PSListNode pCurNode = pPreNode->pNextNode;
                if (pPreNode->data > pCurNode->data)
                {
                    DataType dTemp = pPreNode->data;
                    pPreNode->data = pCurNode->data;
                    pCurNode->data = dTemp;
                    flag = 1;
                }
                pPreNode = pPreNode->pNextNode;
            }
            //      ,               ,      ,            
            if (0 == flag)
            {
                break;
            }
            pTailNode = pPreNode;
        }
    }
}

최종 최적화 판:
void SortList(PSListNode pHead)
{
    if (NULL == pHead)
    {
        return;
    }
    else
    {
        PSListNode pTailNode = NULL;
        PSListNode pFlagNode = NULL;
        //  pFlagNode                      ,     pFlagNode pHead  ,
        //                     ,              ,        ,pFlagNode
        //   pHead,         ,pFlagNide  pHead,         ,         
        while (pFlagNode != pHead)
        {
            pTailNode = pFlagNode;
            pFlagNode = pHead;
            PSListNode pPreNode = pHead;
            while (pPreNode->pNextNode != pTailNode)
            {
                PSListNode pCurNode = pPreNode->pNextNode;
                if (pPreNode->data > pCurNode->data)
                {
                    DataType dTemp = pPreNode->data;
                    pPreNode->data = pCurNode->data;
                    pCurNode->data = dTemp;
                    //             
                    pFlagNode = pPreNode->pNextNode;
                }
                pPreNode = pPreNode->pNextNode;
            }
        }
    }
}

좋은 웹페이지 즐겨찾기