Java implementation cardinality sorting

Source: Internet
Author: User

Detailed explanation See "2015 version data structure high-score note" 8.6--cardinal sort.

The Java code is as follows:

Package Linetimesort;import Java.util.linkedlist;import java.util.objects;/** * Cardinal sort idea is multi-keyword sort * @author yuncong * */ public class Radixsort {/** * Sorts an integer array based on the lowest bit precedence, and the integers in the array are D-digits; * with 3-bit integers as an example, the steps to sort based on the lowest bit precedence are: Create 10 queues for temporary storage of integers; * First order by single digit According to the single digit number of each integer into the corresponding queue, such as 120 in the No. 0 queue, * 124 in the 4th queue, when all the integers are placed in the corresponding queue, sequentially from the 0th queue to the 9th queue to collect integers, * all the collected integers sorted by single-digit size from small to large; Then sort by 10 digits, in the same way each integer is placed in the corresponding queue 10 digits, for the same 10-digit integer, * single-digit small integer first put in the queue, because the previous step by the single-digit order of the number of small digits in front, next, * from 0th to 9th queue to collect integers, The overall first collection of 10-digit small number, local to the specific queue, * that is, 10 digits of the same number, according to the queue FIFO characteristics, single-digit small number is first collected, so that the collection * all the integers are in accordance with the size of the 10-bit and single-digit combination from small to large; * Last sorted by hundred In the same way each integer is placed in the corresponding queue by the number of hundred, for the same number of hundred integers, * 10-digit and single-digit combination after the small integer is put in the queue first, because the previous step of the ordering resulted in 10-digit and one-digit combination of small * integer in front, followed by, from the 0th queue to the 9th , first collect the number of small * number of hundred, local to the specific queue, that is, the same number of hundreds of those numbers, according to the queue of first-out characteristics, 10-digit and * single-digit combination after the small integer is collected first, so, all the collected integers are arranged from small to large. * A sentence summary of the sequencing process, the whole from small to large, local is also small to large. * @param integers array of integers * @param d The number of bits of an integer in the array to be sorted */public void sort (integer[] integers, int d) {/** * Create queue set merge initialization, queues in the collection are used to save Put the corresponding elements, such as by the lowest order, 234 should be * placed in Queues.get (4) This queue */linkedlist<linkedlist<integer>> queues = new linkedlist<> (); for (int i = 0; i <; i++) {linkedlist<integer> queue = new Linkedli St<> (); Queues.add (queue);} for (int i = d-1; I >= 0; i--) {//According to sort key, put elements into corresponding queue for (int j = 0; J < Integers.length; J + +) {Integer key = Getv Aluebyindex (Integers[j], i); Queues.get (Key). Add (Integers[j]);} /** * Take the element back; * For loop convenience set */int index = 0;for (linkedlist<integer> linkedlist:queues) {for (Integer integer: LinkedList) {Integers[index] = integer;index++;} Empty all queues in queues, prepare for two exploits linkedlist.clear ();}} /** * Get index + 1 digits in integer * @param integer * @param index * @return */public integer getvaluebyindex (integer integer, int index) {objects.requirenonnull (integer); String istring = integer.tostring (), if (Index < 0 | | index >= istring.length ()) {throw new indexoutofboundsexception ();} String value = istring.substring (index, index + 1); return integer.valueof (value);} public static void Main (string[] args) {integer[] integers = new Integer[]{654, 122, 987, 123, 345, 234};new Radixsort (). Sort (integers, 3); for (int i = 0; i < integers.length; i++) {System. Out.println (Integers[i]);}}


Java implementation cardinality sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.