程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 排序算法的數組實現 -- 合並排序(五)

排序算法的數組實現 -- 合並排序(五)

編輯:C++入門知識

[cpp ]
void Insert_sort_Swap_Node(EleLink *old_froMax_Node, EleLink *old_Max_Node,EleLink *froMax_Node, EleLink *Max_Node) 

    EleLink *tmp = Max_Node->pnext; 
 
    if(old_Max_Node->pnext == Max_Node) 
    { 
        old_froMax_Node->pnext = Max_Node; 
        Max_Node->pnext = old_Max_Node; 
        old_Max_Node->pnext = tmp; 
    } 
    else 
    { 
        old_froMax_Node->pnext = Max_Node; 
        Max_Node->pnext = old_Max_Node->pnext; 
 
        froMax_Node->pnext = old_Max_Node; 
        old_Max_Node->pnext = tmp; 
    } 

 
void Link_Insert_Sort(EleLink *pNode) 

    if(!pNode || !pNode->pnext) 
        return; 
 
    EleLink *p = pNode->pnext; 
    int ncount = 0; 
    while (p) 
    { 
        ncount++; 
        p = p->pnext; 
    } 
 
    p = pNode; 
    EleLink *fro_Node, *froMax_Node; 
    EleLink *cur_Node, *Max_Node; 
    for (int i = 0; i < ncount - 1; i++) 
    { 
        froMax_Node = p; 
        Max_Node = p->pnext; 
 
        fro_Node = p->pnext; 
        cur_Node = fro_Node->pnext; 
 
        while(cur_Node) 
        { 
            if(Max_Node->Value > cur_Node->Value) 
            { 
                froMax_Node = fro_Node; 
                Max_Node = cur_Node; 
            } 
 
            fro_Node = cur_Node; 
            cur_Node = cur_Node->pnext; 
        } 
 
        if(Max_Node != p->pnext) 
            Insert_sort_Swap_Node(p, p->pnext,froMax_Node, Max_Node); 
 
        p = p->pnext; 
 
    } 

 
void Bucket_Sort(int *a, int size, EleLink** bucket, int ncnt) 

    for(int i = 0; i < size; i++) 
    { 
        int temp = a[i] / 10; 
        EleLink *p_Ele = new EleLink; 
        p_Ele->pnext = NULL; 
        p_Ele->Value = a[i]; 
 
        EleLink *ptr = bucket[temp]; 
        EleLink *p_tmp = ptr->pnext; 
        ptr->pnext = p_Ele; 
        p_Ele->pnext = p_tmp; 
    } 
 
    int m = 0; 
    for (int j = 0; j < ncnt; j++) 
    { 
        Link_Insert_Sort(bucket[j]); 
 
        EleLink *ptr = bucket[j]->pnext; 
 
        while(ptr) 
        { 
            a[m++] = ptr->Value; 
            ptr = ptr->pnext; 
        } 
    } 
     
    assert(m == size); 

作者:wchm_seu

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved