• 基数排序


    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();
    }

  • 相关阅读:
    Collection集合总结
    JAVA泛型通配符T,E,K,V区别,T以及Class,Class的区别【转】
    Runnable和Thread
    jdk自带的函数式接口(配合lambda使用)
    java8-接口的静态方法(static)和默认方法(default)
    Java 8
    java8-Stream流【转】
    java8-lambda表达式
    Oracle:ORA-01461报错
    ASP.NET Core 入门笔记2,建立项目
  • 原文地址:https://www.cnblogs.com/chengxuyuandashu/p/3573212.html
Copyright © 2020-2023  润新知