Data structure Java version of the base sort (iv)

Source: Internet
Author: User

Base sort:

There are two types of cardinality: The first is LSD, the lowest bit begins, and the second is that the MSD starts at the highest bit. The first LSD sorting algorithm is described here.

First, let's first understand what cardinality is. The cardinality is based on a specific sort of situation, such as our common cardinality is the decimal-10, as well as the binary-2.

Secondly, we should memorize the idea of cardinal sort: by sorting the values on each bit, we can finish sorting the whole array.

Algorithm implementation process of radix sorting: Iterate through all array elements, find out the maximum bit value of the element--------> from low to High place the bit value on the array element into the linked list--------> iterate through all linked lists, re-assign the values in the list to the array, and then the list.

For example: array int[] data = {421, 240, 35, 532, 305, 430, 124}; Sort, first of all, we're going to sort the numbers on the single digits.

The result of the first order is: 240 430 421 532 124 35 305

And then you sort the numbers on the top ten:

The result of the second order is: 305 421 124 430 532 35 240

And then sort the numbers on the hundreds:

The result of the third Order is: 35 124 240 305 421 430 532

Finally, our results are: 35 124 240 305 421 430 532

At this point, the sorting of the arrays has been completed

Attached Source:

 Public classRadixsort {@Test Public voidFun () {int[] n = {421, 240, 35, 532, 305, 430, 124};        Radixsort (n);  for(inti:n) {System.out.print (i+ " "); }    }    //implement Cardinal sort lsd-from lowest bit start row msd-from highest bit     Public voidRadixsort (int[] data) {        intMaxbin =maxbin (data); List<List<Integer>> List =NewArraylist<list<integer>>();  for(inti = 0; I < 10; i + +) {List.add (NewArraylist<integer>()); }         for(inti = 0, factor = 1; i < Maxbin; Factor *=, I + +) {             for(intj = 0; J < Data.length; J + +) {list.get (Data[j]/factor)%10). Add (Data[j]); }             for(intj = 0, k = 0; J < List.size (); J + +) {                 while(!List.get (j). IsEmpty ()) {Data[k]= List.get (j). Get (0); List.get (j). Remove (0); K++; }            }        }    }    //calculates the maximum number of digits in an array of elements     Public intMaxbin (int[] data) {        intMaxLen = 0;  for(inti = 0; i < data.length; i + +) {            intSize =integer.tostring (Data[i]). Length (); MaxLen= size > MaxLen?Size:maxlen; }        returnMaxLen; }}

Data structure Java version of the base sort (iv)

Related Article

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: 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.