IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

八、计数排序

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

永久链接: https://tech.souyunku.com/?p=9384

计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。

1. 动图演示

动图演示

2. JavaScript 代码实现

function countingSort(arr, maxValue) {
    var bucket = new Array(maxValue+1),
        sortedIndex = 0;
        arrLen = arr.length,
        bucketLen = maxValue + 1;

    for (var i = 0; i < arrLen; i++) {
        if (!bucket[arr[i]]) {
            bucket[arr[i]] = 0;
        }
        bucket[arr[i]]++;
    }

    for (var j = 0; j < bucketLen; j++) {
        while(bucket[j] > 0) {
            arr[sortedIndex++] = j;
            bucket[j]--;
        }
    }

    return arr;
}

3. Python 代码实现

def countingSort(arr, maxValue):
    bucketLen = maxValue+1
    bucket = [0]*bucketLen
    sortedIndex =0
    arrLen = len(arr)
    for i in range(arrLen):
        if not bucket[arr[i]]:
            bucket[arr[i]]=0
        bucket[arr[i]]+=1
    for j in range(bucketLen):
        while bucket[j]>0:
            arr[sortedIndex] = j
            sortedIndex+=1
            bucket[j]-=1
    return arr

4. Go 代码实现

func countingSort(arr []int, maxValue int) []int {
    bucketLen := maxValue + 1
    bucket := make([]int, bucketLen) // 初始为0的数组

    sortedIndex := 0
    length := len(arr)

    for i := 0; i < length; i++ {
        bucket[arr[i]] += 1
    }

    for j := 0; j < bucketLen; j++ {
        for bucket[j] > 0 {
            arr[sortedIndex] = j
            sortedIndex += 1
            bucket[j] -= 1
        }
    }

    return arr
}

5. java 代码实现

public static void main(String[] args) throws Exception {  
    int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 6, 1, 0 };  

    System.out.println("Before sort:");  
    ArrayUtils.printArray(array);  

    countingSort(array, 9);  

    System.out.println("After sort:");  
    ArrayUtils.printArray(array);  
}  

public static void countingSort(int[] array, int range) throws Exception {  
            if (range <= 0) {  
                throw new Exception("range can't be negative or zero.");  
            }  

            if (array.length <= 1) {  
                return;  
            }  

            int[] countArray = new int[range + 1];  
            for (int i = 0; i < array.length; i++) {  
                int value = array[i];  
                if (value < 0 || value > range) {  
                    throw new Exception("array element overflow range.");  
                }  
                countArray[value] += 1;  
            }  

            for (int i = 1; i < countArray.length; i++) {  
                countArray[i] += countArray[i - 1];  
            }  

            int[] temp = new int[array.length];  
            for (int i = array.length - 1; i >= 0; i--) {  
                int value = array[i];  
                int position = countArray[value] - 1;  

                temp[position] = value;  
                countArray[value] -= 1;  
            }  

            for (int i = 0; i < array.length; i++) {  
                array[i] = temp[i];  
            }  
        }  
赞(89) 打赏



未经允许不得转载:搜云库技术团队 » 八、计数排序

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367