The world in which we live is all sorted. When the line will be ranked according to the height, the ranking of the exam needs to be sorted according to the score, online shopping will be sorted according to the price, e-mail messages in chronological order ... In short a lot of things need to sort, can say sort is everywhere. Now let's give a concrete example to introduce the sorting algorithm.
The first appearance of our hero Xiao Hum, above this lovely baby is a. The final exam is over. The teacher wants to sort the scores of the students according to their grades from high to low. Xiao hum of the class only 5 students, the 5 students scored 5 points, 3 points, 5 points, 2 points and 8 points, ah test is really miserable (full mark is 10 points). The scores are then sorted from large to small, sorted by 8 5 5 3 2. Do you have any good way to write a program that allows the computer to randomly read 5 numbers and then output these 5 numbers from large to small? Just think about it for at least 15 minutes before you look down (*^__^*).
We can solve this problem by using only one one-dimensional array. Make sure you really think about it and look down.
First we need to apply for an array of size 11 int a[11]. OK now you've got 11 variables, numbered from a[0]~a[10]. At the beginning, we initialized the a[0]~a[10] to 0, indicating that none of these scores were ever met. For example, a[0] equals 0, which means that no one has had 0 points at present, and the same a[1] equals 0 means that no one has had 1 points ... a[10] equals 0 means that no one has had 10 points at the moment.
Here we start to deal with each person's score, the first person's score is 5 points, we will be relative to a[5] value on the original basis of 1, the value of a[5] from 0 to 1, indicating that 5 points occurred once.
The second person's score is 3 points, we put the value of the relative should be a[3] on the original basis of the increase of 1, the value of a[3] from 0 to 1, indicating that 3 points occurred once.
Watch out! The third person's score is also "5 points", so the value of a[5] needs to be increased by 1 on this basis, and the value of a[5] will be changed from 1 to 2. Indicates that 5 points have appeared two times.
To deal with the scores of the fourth and fifth people in the way you just did. The final result is the following figure.