This article brings you code examples about Java implementation of counting sorting (CountingSort). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Counting sorting is a special type of bucket sorting.
When n data are to be sorted, if the range is not large, we can take the maximum value K and disperse the data in K buckets,
Each The data in the buckets are all the same (this saves the time of sorting in the buckets), and then they are taken out in sequence.
Of course, counting and sorting is simple to say, but it is difficult to understand in some places when writing.
For example, we now have 8 numbers: 2, 5, 3, 0, 2, 3, 0, 3. To sort them, we can first get its maximum value of 5, and then determine the range. 0-5,
We apply for a memory space of 0-5 to calculate the number of each number corresponding to each position.
The picture below shows the data of the memory space 0-5. We can see that 0 appears twice, 1 appears 0 times, 2 appears twice, 3 appears three times, and 4 appears. 0 times, 5 appeared once.
At the same time, we can also summarize some rules. For example, we now see the position c[2], 2 appears twice, and there are a total of 2 c[0] c[1] in front of 2. element, so c[2] corresponds to the positions of the two 2s in the original array, which are 2 and 3. We can draw the rule that the position of c[2] = c[0] c[1], and the following c[3] Position = c[2] c[1], we sum them sequentially like this:
Then we scan the original array 2, 5, 3, 0, 2, 3, 0, 3. Every time we encounter a number, Substitute this number into the index of the c array to get the real index of the current element after sorting.
My Java implementation is as follows:
package com.structure.sort; /** * @author zhangxingrui * @create 2019-01-30 13:45 **/ public class CountingSort { public static void main(String[] args) { int[] numbers = {3, 9, 2, 1, 8, 7, 6, 10, 9}; // 假设数组中存储的都是非负整数 countingSort(numbers); for (int number : numbers) { System.out.println(number); } } /** * @Author: xingrui * @Description: 计数排序 * @Date: 13:57 2019/1/30 */ private static void countingSort(int[] numbers){ int n = numbers.length; int maxNumber = numbers[0]; for(int i = 1; i < n; ++i){ if(numbers[i] > maxNumber) maxNumber = numbers[i]; } int[] r = new int[n]; int[] c = new int[maxNumber + 1]; for(int i = 0; i < n; ++i){ c[numbers[i]]++; } for(int i = 1; i <= maxNumber; ++i){ c[i] = c[i-1] + c[i]; } for (int i = n - 1; i >= 0; --i){ int index = c[numbers[i]]; r[index - 1] = numbers[i]; c[index]--; } for(int i = 0; i < n; ++i){ numbers[i] = r[i]; } } }
The key code:
for (int i = n - 1; i >= 0; --i){ int index = c[numbers[i]]; r[index - 1] = numbers[i]; c[index]--;5 }
Retrieve the sorted index from the c array.
The above is the detailed content of Code example of Java implementation of counting sort (CountingSort). For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software