Using queues for array arrangement, cardinality sorting

Source: Internet
Author: User

Using queues for array arrangement, cardinality sorting
/** Use queue array, cardinality sort * For 0~99 numbers, the cardinality sort scans the array set two times. * First sort by digit on digit, * second time by number on 10 digits **/functionQueue () { This. DataStore = [];//an array of storage queues, initialized to null     This. enqueue = Enqueue;//add an element to the tail of the queue     This. dequeue = dequeue;//remove elements from the first team     This. Thefront = Thefront;//read the elements of the first team     This. back = back;//the element that takes the tail of the team     This. tostrings = tostrings;//Show all elements in a queue     This. empty = Empty;//determine if the queue is empty}functionEnqueue (Element) { This. Datastore.push (Element);}functiondequeue () {return  This. Datastore.shift ();}functionThefront () {return  This. datastore[0];}functionBack () {return  This. datastore[ This. datastore.length-1];}functiontostrings () {return  This. DataStore;}functionempty () {if( This. Datastore.length = = 0){       return true; }Else{        return false; }}/*Base Sort * Nums: Array to be sorted * Queues: Array, inside element is queue * N: Queue format, here is 10 * Digit: Pass 1, then sort by digit number first; Pass in 10, sort by number on 10 bit **/functionDistribute (nums,queues,n,digit) { for(vari = 0;i < n;i++){        if(Digit = = 1) {Queues[nums[i]%10].enqueue (Nums[i]); }Else if(Digit = = 10) {Queues[math.floor (Nums[i]/10)].enqueue (Nums[i]); }Else{        }    }}functionCollect (queues,nums) {vari = 0;  for(varj = 0;j < 10;j++){         while(!Queues[j].empty ()) {Nums[i++] =Queues[j].dequeue (); }    }}/*Test Program*/varQueues = []; for(vari = 0;i < 10;i++) {Queues[i]=NewQueue ();}varNums = []; for(vari = 0;i < 10;i++) {Nums[i]= Math.floor (Math.random () *101);} Console.log ("The beginning of the nums:" +nums);//24,72,90,84,49,69,8,30,50,0Distribute (nums,queues,10,1); collect (Queues,nums);d Istribute (Nums,queues,10,10); collect (queues,nums); Console.log ("Nums after sorting:" +nums);//0,8,24,30,49,50,69,72,84,90

Using queues for array arrangement, 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.