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

  • 相关阅读:
    VOC2012数据集
    flask 图像接口测试
    PyQt5 学习指南
    超分辨中的注意力机制:Attention in Super Resolution
    OpenCV:图像增亮与直方图均衡
    SOA 架构与微服务架构的区别
    Docker 部署 vue 项目
    element ui 中的 resetFields() 报错'resetFields' of undefined
    Mybatis分页方法
    Serializable的作用
  • 原文地址:https://www.cnblogs.com/woodzcl/p/8068275.html
Copyright © 2011-2022 走看看