当前位置:网站首页>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 :

边栏推荐
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- Data type - integer (C language)
- 21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
- 数据分片介绍
- Ebpf cilium practice (2) - underlying network observability
- How to realize the high temperature alarm of the machine room in the moving ring monitoring system
- All about PDF crack, a complete solution to meet all your PDF needs
- PLSQL的安装和配置
- Implementation of navigation bar at the bottom of applet
- iptables 之 state模块(ftp服务练习)
猜你喜欢

Splunk query CSV lookup table data dynamic query
![[hard core science popularization] working principle of dynamic loop monitoring system](/img/d4/0c0281aec5a4f444528e8cfd401598.jpg)
[hard core science popularization] working principle of dynamic loop monitoring system

21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design

Download and install orcale database11.2.0.4

A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?

归并排序和非比较排序

关于基于kangle和EP面板使用CDN

Opencv learning note 5 - gradient calculation / edge detection

Analysis of maker education in innovative education system

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"
随机推荐
Opencv learning note 3 - image smoothing / denoising
National SMS center number inquiry
POJ - 3784 running medium
Novice entry SCM must understand those things
The field value in Splunk subquery fuzzy matching CSV is*
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
Arm GIC (IV) GIC V3 register class analysis notes.
Learn how to compile basic components of rainbow from the source code
MES系统,是企业生产的必要选择
字符串操作
POJ - 3616 Milking Time(DP+LIS)
[IELTS speaking] Anna's oral learning records part2
Interface as a parameter (interface callback)
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Data type - floating point (C language)
go写一个在一定时间内运行的程序
opencv之图像分割
Data type - integer (C language)
How to understand distributed architecture and micro service architecture
如何在图片的目标中添加目标的mask