[C #] Using cainiao's thinking learning algorithm,

Source: Internet
Author: User

[C #] Using cainiao's thinking learning algorithm,
Use cainiao's thinking learning algorithm-toilet sorting, Bubble sorting, and quick sorting

[Blogger] Anti-bone child [Source] http://www.cnblogs.com/liqingwen/p/4994261.html

Toilet sorting 1. Scenario: after the final exam is over, the teacher will sort the scores of the students from high to low. Assume that there are five students in the class who scored 5, 3, 5, 2, and 8 points respectively [full score: 10 ], the sorting result is 8, 5, 3, and 2. Now, let's think about it for 10 minutes! Ii. Ideas: (1) first create an array int scores [11], and then scores [0] ~ Scores [10] has a total of 11 variables. We use the variable value 0 to indicate that no one gets this score, that is, scores [0] = 0 indicates that no one gets 0 points, and scores [10] = 0 indicates that no one gets 10 points, scores [8] = 1 indicates that one person gets 8 points. (2) The number of 1st is 5, so + 1 on the basis of scores [5] = 0, that is, scores [5] = 1 indicates that one person gets 5 points (3) the number of 2nd is 3, so + 1 is added on the basis of scores [3] = 0, that is, scores [3] = 1 indicates that one person gets 3 points (4), and 3rd numbers are 5, so on the basis of scores [5] = 1 + 1, that is, scores [5] = 2 indicates that two people get 5 points ...... (5) According to this type of push, 4th and 5th numbers are processed. The final result is shown as follows: (6) We found that scores [0] ~ The value corresponding to scores [10] is 0 ~ The number of times each score appears. Now, you only need to print the result. The printer appears several times. Now we call it "toilet sorting". This algorithm is equivalent to 11 toilets numbered from 0 ~ 10. Each time a number appears, a flag is placed in the corresponding numbered toilet. Iii. Thinking: Now there are five people with names and scores: small A 5, small 2, small 3, small 3 5, small 2 and Wang dameng 8. Please follow the scores from high to low, output their names? [Features] assume that the sorting range is 0 ~ 20000000, the new int [20000001] is required, which is a waste of space, even if only two numbers are sorted (); if the number of sorting is decimal, it will not work, such: 3.141 5926 5358 9793 2384 6264 3383 2795 0238; Bubble Sorting 1. Basic Idea: Adjust the sequence of two adjacent elements as needed. Question 2: we need to sort the five numbers of 12 35 99 18 76 in ascending order. 3. Train of Thought: (1) compare the size of 1st bits and 2nd bits first, 12 <35, because we hope that the smaller the backend, we need to adjust the order of the two. The result after the exchange is as follows: 35 12 99 18 76 (2) the size of 2nd bits and 3rd bits is now compared, 12 <99, therefore, you need to switch the location. The result after the switch is 35 99 12 18 76 (3) and then compare the size of the 3rd and 4th bits. 12 <18. The result after the switch is: 35 99 18 12 76 (4) Comparison of the last 4th and 5th The value of 12 <76. After the switch, the result is: 35 99 18 76 12 (5). After four times, we find that the smallest of the five numbers is in place, each time we return a number, we call it "one trip". (6) Now we start the second round. The goal is to return a 2nd Small number. According to the previous logic, compare the number from 1st to 2nd: 35 99 18 76 12 -- ① --> 99 35 18 76 12 -- ② --> 99 35 18 76 12 -- ③ --> 99 35 76 18 12 knows 5th characters in the first round is the smallest, therefore, 4th BITs do not need to be compared with 5th bits. This trip only requires 3 times (7) and 3rd bits: 99 35 76 18 12 --> 99 35 76 18 12 --> 99 76 35 18 12 (compared with 2 times) (8) 4th times: 99 76 35 18 12 --> 99 76 35 18 12, there are four numbers in place, so the last number does not need to be compared, It is the largest [Conclusion] If there are n numbers for sorting, you only need to normalize n-1 number, that is, the n-1 operation is required, at the beginning of each trip, two adjacent numbers are compared from 1st bits, and a small number is placed behind it. If the number has been reset, no comparison is required. [Characteristic] the core part of the bubble algorithm is a dual nested loop. It can be seen that the time complexity is O (n²), which is a very high time complexity. Quick sorting 1. Scenario: sort the 10 numbers 6 1 2 7 9 3 4 5 10 8 2. Train of Thought: first find a reference number (one for reference ), for convenience, we select the leftmost 6 and want to put the "> 6" to the right of "6" and the "<6" to the left of "6. For example: 3 1 2 5 4 6 9 7 10 8 first assume that the position where 6 is to be moved is k, the number on the left of k <6, the number on the Right> 6 (1) we will first start "probe" from the two ends of the initial series "6 1 2 7 9 3 4 5 10 8", and first find a number <6 from the left to the right, find a number greater than 6 from left to right, and then switch. We use variable I and variable j to point to the leftmost and rightmost of the sequence. At the beginning, the leftmost I = 0 points to 6, and the rightmost j = 9 points to 8 (2) the number of currently set benchmarks is the leftmost number, so the sequence first moves right to the left (j --), and stops when a number of <6 (5) is found. Then the sequence moves from left to right (I ++) until a number greater than 6 is found and then stops (7); (3) the two are exchanged. The result is: 6 1 2 5 9 3 4 7 10 8; (4) the position of j continues to move to the left (Note: every time you start from the position of j ), if discovery 4 meets the requirements, then I ++ finds that 9 meets the requirements. The exchange result is: 6 1 2 5 4 3 9 7 10 8;
(5) Currently, j points to 9, I points to 4, j -- Discovery 3 meets the requirements, and I ++ finds that I = j, this round of movement is over. Now, the number of benchmarks 6 and 3 are exchanged. The result is 3 1 2 5 4 6 9 7 10 8. Now the number on the left of 6 is <6, while the number on the right is> 6, but the game is not over yet (6) We will take out the number on the left of 6 First: 3 1 2 5 4, this time, we adjusted the number based on 3 so that the number on the left of 3 is <3, the number on the Right> 3. According to the previous simulation, the result of this operation is: 2 1 3 5 4 (7) then we sorted out 2 1 bytes and got the result: 1 2 (8) The sequence on the left side: 9 7 10 8 is the same. The final result is as follows: 1 2 3 4 5 6 7 8 9 10 [Conclusion] each round of fast sorting actually places the reference number in this round. When all the reference data is returned, the sorting is over.

[Reference] source of text and illustrations: Aha! Algorithm

Related Article

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.