zoukankan      html  css  js  c++  java
  • 基数排序,也叫关键码排序

    一、实现思想

      1、关键码排序,实际上是给数据贴上关键码,然后对关键码进行排序

      2、关键码就是一些数字。比如关键码是两位数时,我可以先对个位数进行排序,然后再对十位数进行排序,最后得到的就是有序的序列。

      3、这里补充一点数据结构安排上的细节,因为这样的数据结构安排,用起来确实有点妙。

        待排序的序列是用链表来存储,在按某个位数进行排序时,实际上是按照该位数大小,放到一个可以拓展的数组(没错,就是指针数组),最后再将数组上的小链表连接起来

    二、实现图

     

     按位数分配的时候,严谨一点,其实是有两个指针数组,一个指向序号的第一个节点,一个指向序号的最后一个节点(当然了,序号只有一个节点时,指向的是同一个节点)

    为什么要两个这样的指针数组,因为我们需要把对应序号上的许多个小链表连接起来。指向链表尾部,方便和下一个链表的首部,连接起来。

    三、实现代码

     1 #include <stdio.h>
     2 typedef struct Node
     3 {
     4     char name;
     5     int flag;
     6     Node *next;
     7 } Node, *pNode;
     8 
     9 pNode RadixSort(pNode first, int d)
    10 {
    11     pNode front[10], rear[10], tail;
    12     int k;                      //用来记录关键码的个位/十位/百位 等等
    13     int base = 1;               //用来控制,k
    14     for (int i = 0; i < d; i++) //进行d趟基数排序
    15     {
    16         for (int j = 0; j < 10; j++)
    17         {
    18             front[j] = NULL;
    19             rear[j] = NULL;
    20         }
    21 
    22         while (first != NULL) //遍历链表,将节点分配给链表数组
    23         {
    24             k = (first->flag / base) % base;
    25             if (front[k] == NULL)
    26             {
    27                 front[k] = first;
    28                 rear[k] = first;
    29             }
    30             else
    31                 rear[k] = rear[k]->next = first;
    32             first = first->next;
    33         }
    34         for (int j = 0; j < 10; j++) //通过链表数组,将节点重新组合成链表
    35         {
    36             if (front[j] == NULL)
    37                 continue;
    38             if (first == NULL) //这对if else用得太好了,tail->next是一定要有的,但是第一次又不需要
    39                 first = front[j];
    40             else
    41                 tail->next = front[j];
    42             tail = rear[j];
    43         }
    44         tail->next = NULL; //为单链表加上一个尾标志
    45         base = base * 10;
    46     }
    47     return first;
    48 }
    49 int main(void)
    50 {
    51     int flag[] = {61, 98, 12, 15, 20, 24, 31, 23, 35};
    52     char name[] = {'H', 'I', 'A', 'B', 'C', 'E', 'f', 'D', 'G'};
    53     pNode mypNode = NULL;
    54     pNode front = NULL;
    55     pNode back = NULL;
    56     //构造一个链表,这不是重点,重点在RadixSort函数上
    57     for (int i = 0; i < 9; i++)
    58     {
    59         pNode temp = new Node; //这里必须要new一个节点出来
    60         temp->name = name[i];
    61         temp->flag = flag[i];
    62         temp->next = NULL;
    63         if (0 == i)
    64         {
    65             mypNode = temp;
    66             front = temp;
    67         }
    68         else
    69         {
    70             back = temp;
    71             front->next = back;
    72             front = front->next;
    73         }
    74     }
    75     front = NULL;
    76     back = NULL;
    77     front = mypNode;
    78     while (front != NULL)
    79     {
    80         printf("%c %d       ", front->name, front->flag);
    81         front = front->next;
    82     }
    83     printf("
    ");
    84     mypNode = RadixSort(mypNode, 2);
    85     front = mypNode;
    86     while (front != NULL)
    87     {
    88         printf("%c %d       ", front->name, front->flag);
    89         front = front->next;
    90     }
    91     return 0;
    92 }
    93 /* 
    94 输出:
    95 ————————————————————————————————————————————————————————————————
    96 H 61       I 98       A 12       B 15       C 20       E 24       f 31       D 23       G 35       
    97 A 12       B 15       C 20       E 24       D 23       f 31       G 35       H 61       I 98 
    98 ————————————————————————————————————————————————————————————————
    99  */

    ps:我这里显示了行号,你应该会列选择,然后删除吧

  • 相关阅读:
    Unit of Work
    Layered Supertype
    Domain Model
    ASP.Net设计模式读书笔记
    VS2010无法使用nuget安装第三方包的问题
    数据库对象命名
    sql50题
    RESTFul API
    EasyUI日历控件
    ASP.NET MVC 防止前端点击劫持
  • 原文地址:https://www.cnblogs.com/coderon/p/13540269.html
Copyright © 2011-2022 走看看