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

    这个最低位优先的基数排序,非常适合移植到硬件设备中,所以,我们提供一个C源码

    ——————————————————————————————————————

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

     

    #define maxSize 100

    #define maxValue 20000

    typedef struct

    {

        int data;

        int link;

    }SLNode;

    typedef struct

    {

        SLNode elem[maxSize];

        int n;

    }StaticLinkList;

     

    void createSList(StaticLinkList *SL, int arr[], int n)

    {

        for (int i=0; i<n; i++)

        {

            SL->elem[i+1].data = arr[i];

            SL->elem[i+1].link = i+2;

        }

        SL->elem[0].data = maxValue;

        SL->elem[0].link = 1;

        SL->elem[n].link = 0;

        SL->n = n;

    }

     

    #define rd 10

    #define d 3

     

    int getDigit(int x, int k)

    {

        if (k<1||k>d)

        {

            return -1;

        }

        for(int i=1; i<=d-k; i++)

        {

            x /= 10;

        }

        return x%10;

    }

     

    void SLinkRadixSort(StaticLinkList * SL)

    {

        int front[rd], rear[rd];

        int i, j, k, last, s, t;

     

        for (i=d; i>=1; i--)

        {

            for (j=0; j<rd; j++)

            {

                front[j] = 0;

            }

     

            for (s=SL->elem[0].link; s!=0; s=SL->elem[s].link)

            {

                k = getDigit(SL->elem[s].data, i);

                if (0==front[k])

                {

                    front[k] = s;

                }

                else

                {

                    SL->elem[rear[k]].link = s;

                }

                rear[k] = s;

            }

     

            for (j=0; j<rd&&0==front[j]; j++);

     

            SL->elem[0].link = front[j];

            last = rear[j];

     

            for (t=j+1; t<rd; t++)

            {

                if (0!=front[t])

                {

                    SL->elem[last].link = front[t];

                    last = rear[t];

                }

            }

            SL->elem[last].link = 0;

        }

    }

     

    void main()

    {

        int arr[] = {332, 633, 589, 232, 664, 179, 457, 825, 405, 361};

        int len = sizeof(arr)/sizeof(int);

     

        StaticLinkList SL;

        createSList(&SL, arr, len);

        SLinkRadixSort(&SL);

     

        for (int i=SL.elem[0].link; 0!=i; i = SL.elem[i].link)

        {

            printf("%d ", SL.elem[i].data);

        }

     

        printf(" ");

    }

    //result

    # ./sort
    179 232 332 361 405 457 589 633 664 825

  • 相关阅读:
    取得GridView被隐藏列的值方法集合
    【转】ASP.NET 数据分页第一篇 探讨分页原理及 SQL Server 2005 的 ROW_NUMBER 函数
    浏览器的工作原理:新式网络浏览器幕后揭秘
    无法装载文件或者汇编的AjaxControlToolkit
    【转】ASP.NET 数据分页第二篇 范例下载
    在用户控件中FindControl控件
    【转】ASP.NET 数据分页第三篇 结合 Custom Control 处理 GridView 的 UI 呈现
    Ajax Toolkit AutoComplete 几种用法
    到底如何区分什么是架构、框架、模式和平台 ?
    因果推理综述——《A Survey on Causal Inference》一文的总结和梳理
  • 原文地址:https://www.cnblogs.com/woodzcl/p/8068275.html
Copyright © 2011-2022 走看看