当前位置:网站首页>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 :
边栏推荐
- In go language, function is a type
- Golang compilation constraint / conditional compilation (/ / +build < tags>)
- JEditableTable的使用技巧
- Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
- Ebpf cilium practice (2) - underlying network observability
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
- Interface as a parameter (interface callback)
- Componentspace2022, assertions, protocols, bindings, and configuration files
- Data type - integer (C language)
- Opencv learning notes 1 -- several methods of reading images
猜你喜欢
The single value view in Splunk uses to replace numeric values with text
为什么要选择云原生数据库
About using CDN based on Kangle and EP panel
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
How to integrate app linking services in harmonyos applications
Open3D ISS关键点
详解华为应用市场2022年逐步减少32位包体上架应用和策略
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
2-3查找樹
随机推荐
Laravel8 uses passport login and JWT (generate token)
Splunk query CSV lookup table data dynamic query
Merge sort and non comparison sort
[IELTS speaking] Anna's oral learning records part2
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
[kuangbin] topic 15 digit DP
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
mysql分区讲解及操作语句
Several ways of lambda used in functions in kotlin (higher-order functions)
Pvtv2--pyramid vision transformer V2 learning notes
数据库存储---表分区
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
Open3D ISS关键点
基本数据类型和string类型互相转化
Ebpf cilium practice (2) - underlying network observability
GOLand idea intellij 无法输入汉字
Analysis of maker education in innovative education system
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Opencv learning note 5 - gradient calculation / edge detection
Using helm to install rainbow in various kubernetes