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;
    }
  • 相关阅读:
    ASP.Net MVC-Web API使用Entity Framework时遇到Loop Reference
    springboot-32-使用mvc测试
    docker-dockerfile使用
    java-jmx使用
    docker-使用ali云加速
    docker-基础命令
    centos7-使用nginx做ftp站
    maven-插件-不同的开发环境指定
    maven
    mysql-定时对表分区
  • 原文地址:https://www.cnblogs.com/wolves-dave/p/3389433.html
Copyright © 2011-2022 走看看