当前位置:网站首页>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 :
边栏推荐
- National SMS center number inquiry
- The field value in Splunk subquery fuzzy matching CSV is*
- 南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
- 下载和安装orcale database11.2.0.4
- Implement your own dataset using bisenet
- Openvscode cloud ide joins rainbow integrated development system
- GOLand idea intellij 无法输入汉字
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- 使用AGC重签名服务前后渠道号信息异常分析
- [untitled]
猜你喜欢
XCiT学习笔记
Analyzing the influence of robot science and technology development concept on Social Research
Explore creativity in steam art design
Interpreting the practical application of maker thinking and mathematics curriculum
Compilation and linking of programs
PLSQL的安装和配置
The truth of robot education in hands-on practice
Openvscode cloud ide joins rainbow integrated development system
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
[untitled]
随机推荐
Obsidan之数学公式的输入
Analysis of maker education in innovative education system
登山小分队(dfs)
如何在快应用中实现滑动操作组件
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
opencv 将16位图像数据转为8位、8转16
Download and install orcale database11.2.0.4
Pvtv2--pyramid vision transformer V2 learning notes
iptables 之 state模块(ftp服务练习)
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
Thirteen forms of lambda in kotlin
You should use Google related products with caution
Several ways of lambda used in functions in kotlin (higher-order functions)
JS的操作
Open3d ISS key points
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
POJ - 3616 Milking Time(DP+LIS)
【微信小程序:缓存操作】
下载和安装orcale database11.2.0.4