Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the cardinality sort of Java bucket sort

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to understand the cardinality sort of Java bucket sort". In the daily operation, I believe that many people have doubts about how to understand the cardinality sort of Java bucket sort. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to understand the cardinality sort of Java bucket sort". Next, please follow the editor to study!

Cardinal sorting is also a kind of bucket sorting, and it is also strongly related to the sample data, and cardinality sorting requires that the sample data is a non-negative decimal number, if there is a decimal or negative number, then the code will be greatly rewritten! This is the drawback of sorting without comparison. Generally speaking, we think that the cardinality sorting time complexity is O (N). But in fact, if the amount of data is very large, its time complexity is O (N*log10 (Max)) (the base is 10).

The process of cardinality sorting algorithm is not very difficult, but the following code is implemented in a more coquettish way, so put a screenshot first for easy viewing.

We know that the implementation process of cardinality sorting needs to prepare 10 queues, but the classic implementation process uses a count array to simulate the operation of the queue, so it saves space.

Package com.harrison.class05;import java.util.Arrays;// cardinality sorting is also a kind of bucket sorting, and it is also strongly related to the sample data / / and the cardinality sorting requires that the sample data is a non-negative decimal number / / if there is a decimal or negative number, then the code will be massively rewritten! / / this is the disadvantage of sorting without comparison / / generally speaking, we think that the complexity of cardinality sorting time is O (N) / / but in fact, if the amount of data is very large. Its time complexity is O (N*log10 (Max)) (base is 10) public class Code03_RadixSort {public static void radixSort (int [] arr) {if (arr = = null | | arr.length < 2) {return } radixSort (arr, 0, arr.length-1, maxBits (arr));} / / find the number of digits of the maximum value in the array / / for example, if the maximum value in the array is 100, then return 3 public static int maxBits (int [] arr) {int max = Integer.MIN_VALUE; for (int I = 0) I < arr.length; iTunes +) {max = Math.max (max, arr [I]);} int res = 0; while (max! = 0) {res++; max / = 10;} return res } / / this method is combined with screenshot understanding! Public static void radixSort (int [] arr, int L, int R, int digit) {final int radix = 10; int I = 0, j = 0; / / how many are there in the original array and how many spaces int [] help = new int [R-L + 1]; for (int d = 1; d

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report