Sorting of the age of the employees in buckets

Source: Internet
Author: User

I don't know what the problem is today, I'm in a tangle of what, the topic requires a company all employees of the age order, can only apply O (n) size of the auxiliary space. Then see the solution of the problem in the general idea is to use the auxiliary space to record the number of occurrences of each age, and then the next code to read a half-day.

1 int index=0; 2  for (int i=0;i<=oldestage;i++) {3      for (int j=0;j<timesofage[i];j++) {4          age[index]=i; 5          index++; 6     }          7 }            

The first thing you see after a question is sort by the age of the company's employees, such as {59,25,23,46,32,15,65,26,22,33,20,28,59,23,24,23,23,32,59,59}, and then sort by the number of times the age appears, After putting the age of each employee into a 1-100 bucket, the buckets are ranked down in order from highest to lowest. So I've never understood the meaning of this code.

And then on the net to see the results of the bucket sequencing, the original final result is not so. It is equivalent to the original chaotic input of a group of numbers into the bucket, each bucket is the same size of the number, and then according to the number of barrels, from small to large in order to release the number of barrels inside (the bucket has a few numbers on how many of the same number), in order to come out of the sequence of the array .

After you understand the meaning, you can understand the above code. The first is that Oldestofage is the largest age range, and then Timesofages is the number of occurrences of each age, that is, as barrels. Next, Index=0 begins, for each number beginning, accesses its corresponding occurrence in timesofages, how many times it is written to the current index in the age array. Index is the representation of the position, for example, {59,25,23,46,32,15,65,26,22,33,20,28,59,23,24,23,23,32,59,59} in the above array, when index=0,i=15, At this time the timesofages[15]=1, then age[0]=15; then index++,i=16, continue down. For 23 occurrences, the array will also make a different index equals 23, as long as the reporter age is not stored in the number of times, but the corresponding age should be in the index is which.

  

1  Public classTest {2      Public Static voidSortages (int[] Ages)throwsexception{3         if(ages==NULL){4             return;5         }6         intoldestage=99;7         int[] timesofages=New int[Oldestage+1];8          for(inti=0;i<oldestage;i++){9Timesofages[i]=0;Ten         } One          for(inti:ages) { A             if(I<=0 | | i>oldestage) { -                 Throw NewException ("Age out of range"); -             } thetimesofages[i]++; -         } -          -         intIndex=0; +          for(inti=0;i<=oldestage;i++){ -              for(intj=0;j<timesofages[i];j++){ +ages[index++]=i; A             } at         } -          -}

The intention to really understand the topic is the entrance key.

Sorting of the age of the employees in buckets

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.