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