当前位置:网站首页>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 :
边栏推荐
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
- IELTS review progress and method use [daily revision]
- MES系统,是企业生产的必要选择
- 【微信小程序:缓存操作】
- [Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
- Basic data types and string types are converted to each other
- In go language, function is a type
- uniapp 微信小程序监测网络
- Data type - integer (C language)
- [Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
猜你喜欢
Novice entry SCM must understand those things
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Compilation and linking of programs
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
Implementation of navigation bar at the bottom of applet
DeiT学习笔记
SSM integration
Learn how to compile basic components of rainbow from the source code
IP地址的类别
One click installation of highly available Nacos clusters in rainbow
随机推荐
快速集成认证服务-HarmonyOS平台
数据库存储---表分区
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
下载和安装orcale database11.2.0.4
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Data type - floating point (C language)
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Arm GIC (IV) GIC V3 register class analysis notes.
Interface as a parameter (interface callback)
Rsync remote synchronization
Input of mathematical formula of obsidan
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
Golang 编译约束/条件编译 ( // +build <tags> )
Installation and configuration of PLSQL
MES system is a necessary choice for enterprise production
Rapid integration of authentication services - harmonyos platform
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
uniapp 微信小程序监测网络
数据分析方法论与前人经验总结2【笔记干货】