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

    1)判断数据在各位的大小,排列数据;

    2)根据1的结果,判断数据在十分位的大小,排列数据。如果数据在这个位置的余数相同,那么数据之间的顺序根据上一轮的排列顺序确定;

    3)依次类推,继续判断数据在百分位、千分位......上面的数据重新排序,直到所有的数据在某一分位上数据都为0。

    #include <stdio.h>
    #include <assert.h>
    #include <malloc.h>
    #include <string.h>

    int pre_process_data(int array[], int length, int weight)
    {
        int index;
        int value = 1;
        assert(NULL != array && 0 != length);
        for(index = 0; index < weight; index ++)
            value *= 10;
        for(index = 0; index < length; index ++)
        {
            array[index] = (array[index]%value)/(value/10);
        }
        for(index = 0; index < length; index ++)
        {
            if(0 != array[index])
                return 1;
        }
        return 0;
    }

    void sort_for_basic_value(int array[], int pData[], int length, int swap[])
    {
        int value;
        int index;
        int total = 0;
        for(value = -9; value < 10; value ++)
        {
            for(index = 0; index < length; index ++)
            {
                if(-10 != pData[index] && pData[index] == value)
                {
                    swap[total++] = array[index];
                    pData[index] = -10;
                }
            }
        }
        memmove(array, swap, sizeof(int) * length);
    }

    void basic_sort(int array[], int length)
    {
        int weight = 1;
        int *pData = malloc(sizeof(int) * length);
        memmove(pData, array, sizeof(int) * length);
        int *swap = malloc(sizeof(int) * length);
        while(1)
        {
            if(!pre_process_data(pData, length, weight))
                break;
            else
            {
                weight ++;
            }
            sort_for_basic_value(array, pData, length, swap);
            memmove(pData, array, sizeof(int) * length);
        }
    }

    void test()
    {
        int i;
        int array[10] = {1,3,14,5,8,6,7,9,0,-2};
        basic_sort(array, 10);
        for(i = 0; i < 10; i ++)
        {
            printf("%d, ", array[i]);
        }
        printf(" ");
    }

    int main()
    {
        test();
    }

  • 相关阅读:
    SQLSERVER查询所有数据库名,表名,和字段名
    SQL通过拆分某字段中的内容来实现与对应表连接查询
    [SPOJ]CIRU 圆并
    有关反演和GCD
    docker部署 jenkins
    mongoDB学习记录(二)
    docker动态修改容器限制
    ORACLE数据库误操作DELETE并且提交数据库之后如何恢复被删除的数据
    用8个命令调试Kubernetes集群
    db2服务器linux的cache过高原因
  • 原文地址:https://www.cnblogs.com/chengxuyuandashu/p/3573212.html
Copyright © 2011-2022 走看看