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

    计数排序

    /**
     *@param array is the array to sort
     *@param length is the length of the array
     *@param range is the range of the whole numbers in array
     */
    void CountingSort(int* array, int length, int range)
    {//this algorithm needs two auxiliary, one for counting the total number of the digits, one for placing sorted digits
        int* auxiliary_array = new int[length];//for placing sorted digits
        int* counting_array = new int[range];//fpr counting the total number of the digits
    
        //initilization
        for (int index = 0; index < length; ++index) {
            auxiliary_array[index] = 0;
        }
    
        for (int index = 0; index < range; ++index) {
            counting_array[index] = 0;
        }
    
        //the next three loops is called counting sort
        for (int index = 0; index < length; ++index) {
            ++counting_array[ array[index] ];
        }
    
        for (int index = 1; index < range; ++index) {
            counting_array[index] += counting_array[index - 1];
        }
    
        for (int index = length - 1; index >= 0; --index) {
            auxiliary_array[ counting_array[ array[index] ] - 1 ] = array[index];
            --counting_array[ array[index] ];
        }
    
        for(int index = 0; index < length; ++index) {
            array[index] = auxiliary_array[index];
        }
    
        delete[] auxiliary_array;
        delete[] counting_array;
    }
  • 相关阅读:
    constraint更新表列约束默认值
    sql语句 关于日期时间、类型转换的东西
    SQL数据库完全复制
    SQLServer语句 汇总
    SQL Server Profiler使用方法
    SQL语句-批量插入表(表数据插表)
    VS 快捷键
    外部引用 jQuery 库
    mongodb笔记
    Ubuntu16.04安装live-server
  • 原文地址:https://www.cnblogs.com/wolves-dave/p/3389433.html
Copyright © 2011-2022 走看看