The cardinality of the queue implementation for data structure Jobs (Java edition)

Source: Internet
Author: User

Topic:

The queue is used to sort a sequence of data (in the order of cardinality), where data for the data series (1th and 2nd) and how the queue is stored (described in article 3rd) are as follows:

1) When the data series is an integer type of data, the number of bits per data in the data series does not require equal width, for example:

1, 21, 12, 322, 44, 123, 2312, 765, 56

2) When the data series is a string type of data, each string in the data series is equal width, for example:

"ABC", "Bde", "fad", "abd", "bef", "FDD", "Abe"

3) requires that the storage representation of the queue be rebuilt so that it can map n queue order to an array of Listarray, each of which is represented as a looping queue in memory "This is an option"

Cardinal Sort Idea:

take an integer as an example: first build 10 barrels, numbered 0-9. Then detect the integers to be sorted,

Starting with the number of digits, the highest number of digits that exist in the data. All the integers to be sorted are placed in the bucket of the corresponding number, based on the value of each digit number.

than , for example, integers 73 22 93 43 55 14 28 65 39 81.

First trip:

Second trip:

If the number you want to sort has a higher number of digits, you must sort multiple times. But that would take time. Therefore, a radix sort is not recommended for a sequence of numbers with a large difference in number of digits. The time complexity of the cardinality sort is O (n). Space complexity is O (n+bucket). Buckets are the number of buckets. Therefore, the spatial complexity is approximately equal to O (n). When the number after each bucket is known, the spatial structure of the stored cardinality can be transformed into a simple one-dimensional array.

Queue: FIFO feature. Push and pop queue up separately.

Specific implementation code:

1 //Programming in Java2 ImportJava.util.*;3 4  Public classRadixsort {5 6     Private Static FinalHashmap<character, integer> alphatonum =NewHashMap ();7 8     Static {9 alphatonum.clear ();TenInitalpha2num (' A ', ' Z ', alphatonum); OneInitalpha2num (' A ', ' Z ', alphatonum); A  -     } -  the     Private Static Final voidInitalphatonum (CharC1,CharC2, Hashmap<character, integer>map) { -          for(Charc = C1; C <= C2; C++) - Map.put (c, Map.size ()); -     } +  -      Public Static voidMain (string[] args) { +Radixsort sortinstance =NewRadixsort (); A         int[] data = {1, 4, 3, 3, 21, 12, 322, 44, 123, 2312, 765, 56, 8978, 10000, 14, 28, 65, 39, 81, 33, 100, 567 }; atString[] data2 = {"abc", "Bde", "fad", "abd", "Bef", "FDD", "Abe" }; - sortinstance.sort (data); - Sortinstance.sort (data2); -  - print (data); - print (data2); in     } -  to      Public Static voidprint (string[] data) { +          for(inti = 0; i < data.length; i++) -System.out.print (Data[i] + ""); the System.out.println (); *     } $ Panax Notoginseng      Public Static voidPrintint[] data) { -          for(inti = 0; i < data.length; i++) theSystem.out.print (Data[i] + ""); + System.out.println (); A     } the  +      Public voidSortint[] a) { -         intN = 10; $Arraylist<queue<integer>>Qarray; $Qarray =NewArrayList (); -          for(inti = 0; i < N; i++) -Qarray.add (New<Integer> LinkedList ());//Open Space the         intMax =Integer.min_value; -          for(inti = 0; i < a.length; i++)WuyiMax =Integer.max (Max, a[i]); the         int[] length =New int[N];//record the original length of the Qarray to prevent duplicate operations on the element -         //Radix sort Wu          for(inti = 0; i < a.length; i++) -Qarray.get (A[i]%). Offer (A[i]);//1 Step About          for(intj = 10; J <= Max; J *= 10) { $              for(inti = 0; i < N; i++) -Length[i] = Qarray.get (i). Size ();//2 Step Update length -              for(inti = 0; i < N; i++) { -queue<integer> q =Qarray.get (i); A                  while(length[i]--> 0) { +                     intnum =Q.poll (); theQarray.get ((num/j)% 10). Offer (num); -                 } $             } the         } the          for(inti = 0, aindex = 0; i < N; i++)//finally the              while(Qarray.get (i). Size () > 0) thea[aindex++] =Qarray.get (i). Poll (); -     } in  the      Public voidsort (string[] s) { the         intN =alphatonum.size (); AboutArraylist<queue<string>>Qarray; theQarray =NewArrayList (); the          for(inti = 0; i < N; i++) theQarray.add (New<String> LinkedList ());//Open Space +         intStrLen = S[0].length (); -         int[] length =New int[N];//forbid duplicate operation for (int i = 0; i < s.length; i++) theQarray.get (Alphaindex (S[i].charat (s[i].length)-1)) . Offer (S[i]);Bayi          for(intj = strLen-1; J > 0; j--) { the              for(inti = 0; i < N; i++) theLength[i] =Qarray.get (i). Size (); -              for(inti = 0; i < N; i++) { -queue<string> q =Qarray.get (i); the                  while(length[i]--> 0) { theString str =Q.poll (); theQarray.get (Alphaindex (Str.charat (j-1)) . Offer (str); the                 } -             } the         } the          for(inti = 0, aindex = 0; i < N; i++)//finally the              while(Qarray.get (i). Size () > 0)94s[aindex++] =Qarray.get (i). Poll (); the     } the  the     Private intAlphaindex (Charc) {98         returnAlphatonum.get (c); About     } -}

PS: ABBA's code

The cardinality of the queue implementation for data structure Jobs (Java edition)

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.