当前位置:网站首页>Count sort (diagram)
Count sort (diagram)
2022-07-07 08:37:00 【S dream chasing boy】
One 、 What is counting sort
Counting sort is applicable to those with Define the scope Array of , For example, given an array , And know that all the value ranges are [n,m]. You can use one at this time m-n+1 An array of lengths , The array to be sorted can be scattered on this array , The value of the array is the number of current values , After another traversal expansion , The resulting array is ordered .
Two 、 Implementation steps
(1) Get the maximum value of the elements in the array 、 minimum value ; To determine the length of the array of counts ;
(2) Ergodic primitive group , Count the number of occurrences of each element with the count array ;
(3) Traversal count array , Re store the original array , The output original array is the sorted array ;

3、 ... and 、 Code implementation
public class CountingSort {
public static void main(String[] args) {
int[] arr = { -2, 3, 5, 1, 7, 3, 11, 4 };
sort(arr);
}
public static void sort(int[] arr) {
int Max = arr[0];
int Min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > Max) {
Max = arr[i];
}
if (arr[i] < Min) {
Min = arr[i];
}
}
int len = Max - Min + 1;
int[] count = new int[len];
for (int i = 0; i < arr.length; i++) {
count[(arr[i] - Min)]++;
}
int h = 0;
for (int m = 0; m < count.length; m++) {
for (int j = 0; j < count[m]; j++) {
arr[h] = m + Min;
h++;
}
}
for (int c = 0; c < arr.length; c++) {
System.out.print(arr[c]+" ");
}
}
}
Running results :

边栏推荐
- Analysis of maker education in innovative education system
- Snyk dependency security vulnerability scanning tool
- About using CDN based on Kangle and EP panel
- Arm GIC (IV) GIC V3 register class analysis notes.
- Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
- Openvscode cloud ide joins rainbow integrated development system
- Obsidan之数学公式的输入
- Opencv learning notes 1 -- several methods of reading images
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- POJ - 3784 Running Median(对顶堆)
猜你喜欢

Novice entry SCM must understand those things

Merge sort and non comparison sort

National standard gb28181 protocol video platform easygbs adds streaming timeout configuration

Exercise arrangement 2.10, 11

2-3 lookup tree

Give full play to the wide practicality of maker education space

PVTV2--Pyramid Vision TransformerV2学习笔记

Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur

AVL balanced binary search tree

Opencv learning notes 1 -- several methods of reading images
随机推荐
AVL平衡二叉搜索树
SSM 整合
opencv之图像分割
路由信息协议——RIP
Several ways of lambda used in functions in kotlin (higher-order functions)
PVTV2--Pyramid Vision TransformerV2学习笔记
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
Go write a program that runs within a certain period of time
[machine learning] watermelon book data set_ data sharing
uniapp 微信小程序监测网络
Opencv learning notes 1 -- several methods of reading images
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
Golang 编译约束/条件编译 ( // +build <tags> )
Practice of implementing cloud native Devops based on rainbow library app
Using helm to install rainbow in various kubernetes
Golan idea IntelliJ cannot input Chinese characters
Interpreting the practical application of maker thinking and mathematics curriculum
基本数据类型和string类型互相转化
Virtual address space
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全