zoukankan      html  css  js  c++  java
  • 基数排序

    算法描述

    多关键字排序:

    又比如123,主位是1,次卫是3。

    123,46,791。 按照次位优先     次位:791,123,46

                                             次次位:123,46,791

                                          次次次位:46,123,791

    具体实现

    建立桶元素结点,用链表实现。

    建立桶头尾结点结构体。

    构造GetDigit ( int X, int D )函数 用来得到X的D位的数字

    构造LSDRadixSort( ElementType A[], int N )

     {

          初始化每个桶为空链表

          将原始序列逆序存入初始链表List

          排序

          {  

                   对数据的每一位循环处理

                   {

                     获得当前元素的当前位数字 

                     从List中摘除

                     插入B[Di]号桶尾

                   }

           }

          收集(将每个桶的元素顺序收集入      将List倒入A[]并释放

     1 #define MaxDigit 4
     2 #define Radix 10
     3 typedef struct Node *PtrToNode;
     4 struct Node
     5 {
     6     int key;
     7     PtrToNode Next;    
     8 };
     9 //定义桶头节点
    10 struct HeadNode
    11 {
    12     PtrToNode head;
    13     PtrToNode tail;
    14 };
    15 typedef struct HeadNode Bucket[Radix];
    16 /*
    17    X是所求的数,X是几位数 D就是几
    18 */
    19 int GetDigital(int X,int D)
    20 {
    21     int i,d;                 //d为返回值,是X在某位上的具体数
    22     for(i=1;i<=D;i++)
    23     {
    24         d=X%Radix;             //Radix =10;
    25         X/=Radix;             //Radix =10;
    26     }
    27     return d;
    28 }
    29 void LSDRadixSort(ElementType A[],int N)
    30 {
    31     Bucket B;
    32     int i ,D,Di;
    33     PtrToNode P,temp,list=NULL;
    34 
    35     /*初始化桶*/
    36     for(i=0;i<Radix;i++)
    37    
    38         B[i].head=B[i].tail=NULL;
    39     
    40    /*将数组逆序导入链表list*/
    41     for(i=0;i<N;i++)
    42     {
    43         temp=(PtrToNode)malloc(sizeof( struct Node));  //新建temp
    44         temp->key=A[i];     
    45         temp->Next=list;    // 第一次temp为list list为null
    46         list=temp;          //更新list 为temp
    47     }
    48     
    49     /*从最低位开始入桶*/
    50 
    51     for(D=1;D<=MaxDigit;D++)
    52     {    
    53         P=list;
    54         while(P)
    55         {
    56             Di=GetDigital(P->key,D);
    57             /*获取位数*/
    58             temp=P;
    59             P=P->Next;
    60             temp->Next=NULL;
    61             /*从list中摘除*/
    62             if(B[Di].head==NULL)
    63                 B[Di].head=B[Di].tail=temp;
    64             else
    65             {
    66                 B[Di].tail->Next=temp;
    67                 B[Di].tail=temp;
    68             }
    69         }
    70     
    71     /*收集*/
    72         list=NULL;
    73         for(Di=Radix-1;Di>=0;Di--)
    74         {
    75             if(B[Di].head)
    76             {
    77                 B[Di].tail->Next=list;
    78                 list=B[Di].head;
    79                 B[Di].head=B[Di].tail=NULL;//清空桶
    80             }
    81         }
    82     }
    83     /* 将list倒入A[]并释放空间 */
    84     for(i=0;i<N;i++)
    85     {
    86         temp=list;
    87         list=list->Next;
    88         A[i]=temp->key;
    89     //    printf(" i: %d  a: %d   ",i,A[i]);
    90         free(temp);
    91     }
    92 }

     错误分析:1.将82行的{  放在了70行。

  • 相关阅读:
    POJ 2187 Beauty Contest(凸包+旋转卡壳)
    POJ 3845 Fractal(计算几何の旋转缩放)
    POJ 1755 Triathlon(线性规划の半平面交)
    POJ 2540 Hotter Colder(半平面交)
    POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
    POJ 3348 Cows(凸包+多边形面积)
    POJ 1228 Grandpa's Estate(凸包唯一性判断)
    POJ 2826 An Easy Problem?!(线段交点+简单计算)
    如何在ARC代码中混编非ARC代码
    给view 添加事件
  • 原文地址:https://www.cnblogs.com/zle1992/p/5867960.html
Copyright © 2011-2022 走看看