[C #] with rookie thinking learning algorithm--toilet sort, bubble sort and quick sort

Source: Internet
Author: User

using the Novice's thinking learning algorithm--toilet sort, bubble sort and quick sort

"Bo Master" anti-bone Aberdeen "source" http://www.cnblogs.com/liqingwen/p/4994261.html

Toilet Sortfirst, the scene : The final exam is over, the teacher to the students score from high to low sort. Suppose the class has 5 students, respectively, 5 points, 3 points, 5 points, 2 points and 8 points "full score: 10 points", the result of sorting is 8 5 5 3 2, now, let's think about 10 minutes!  Second, the idea :(1) first create an array int scores[11], there is scores[0]~scores[10] a total of 11 variables. We use a variable value of 0 to indicate that no one gets the score, that is, scores [0]=0 says no one has 0 points, scores [10]=0 says no one has 10 points, and scores [8]=1] says one person gets 8 points. (2) The 1th number is 5, so on the basis of scores[5]=0 +1, that is, scores[5]=1 that 1 people get 5 points(3) The 2nd number is 3, so on the basis of scores[3]=0 +1, that is, scores[3]=1 that 1 people get 3 points(4) The 3rd number is 5, so on the basis of Scores[5]=1 +1, that is, scores[5]=2 that 2 people get 5 points...(5) and so on, dealing with the 4th and 5th numbers, the final result diagram is as follows:(6) We find that the corresponding value in SCORES[0]~SCORES[10] is the number of occurrences of each fraction in the 0~10 score. Now, just print the results and appear several times on the printer.   We call it the "toilet sort", which is equivalent to having 11 toilets, numbered from 0~10. For each number, place a flag in the corresponding numbered toilet.      Three, think : Now there are 5 people's name and score: Small a 5, small 23, small 35, Chick 2 and King Sledgehammer 8, please follow the score from high to low, output their name?   "Features" If you need to sort the range 0~20000000, you need new int[20000001], which is a waste of space , even if only 2 numbers are ordered (1,19999999);if the number of sorts is not a decimal , such as: 3.141 5926 5358 9793 2384 6264 3383 2795 0238; Bubble Sortfirst, the basic idea : each comparison of adjacent two elements, on-demand adjustment of the orderSecond, the title : Request 12 35 99 18 76 These 5 numbers from the big to the small sortThird, the idea :(1) Compare the size of the 1th and 2nd digits first, 12<35, because the smaller the hope, so to adjust the order of the two, the result after the exchange:(2) Now compare 2nd and 3rd bit size, 12<99, so need to exchange position, the result of Exchange is: (3) Then compare the size of the 3rd and 4th digits, 12<18, after the exchange results are: (4) Finally comparing the size of the 4th and 5th digits, 12<76, after the exchange results are: (5) after 4 times We find that the smallest of the 5 numbers is in place, each one of which we call "a trip";(6) Now we start the second trip, the goal is to return the 2nd small number, according to the previous logic, or from the 1th number and 2nd number to start comparing:35 99 18 76 12--①--> -18 76 12--②-->, 76 12--③-->, 76 Aboutin the first trip compared to know that the 5th is the smallest, so the 4th bit does not compare with 5th, this trip only 3 times compared(7) 3rd trip: three----- ------- -18 12 (compare 2 )(8) 4th trip: Six-------- 35 18 12, 4 numbers are in place, so the last number is not to be compared, it is the largest "Summary" if there are n number of orders, just the number of n-1, that is, to carry out the n-1, and each trip starts from the 1th place adjacent to the two number of comparisons, the small number is placed in the back, has been returned without comparison.     The core part of the "feature" bubbling algorithm is the double nested loop, which shows that the time complexity is O (N²), which is a very high time complexity.    Quick SortFirst, scene : 6 1 2 7 9 3 4 5 10 8 This 10 number is sorted Second, the idea :first find a base number (a reference to the number), for convenience, we choose the leftmost 6, hoping to put >6 to the right of 6, <6 to 6 left. such as: 3 1 2 5 4 6 9 7, 8first assume that you need to move 6 to the position of K,k to the left of the number <6, the number on the right >6(1) We start "probing" from both ends of the initial sequence "6 1 2 7 9 3 4, 5 10 8", first find a <6 number from the right to the left , then find a >6 number from left to right , then swap. We use the variable i and the variable J to point to the leftmost and rightmost of the sequence. At first, the leftmost i=0 points to 6, and the rightmost j=9 points to 8. (2) The base number is now set to the leftmost number, so the sequence first right to the left (j--), when found a <6 number (5) to stop. Then the sequence moves from left to right (i++) until a number of >6 is found and stops (7);(3) Exchange of the two, results: 6 1 2 5 9 3 4 7, 8; (4) The position of J continues to move to the left ( friendly tip: Each time must start from the position of J ), found that 4 meet the requirements, then i++ found that 9 meet the requirements, the results of the Exchange: 6 1 2 5 4 3 9 7 8;
(5) At present, J points to a value of 9,i pointed to the value of 4,j--found 3 meet the requirements, then i++ found I=j, indicating that this round of movement is over. The base number 6 and 3 are now exchanged, resulting in3 1 2 5 4 6 9 7, 8;now 6 The number on the left is <6, and the number on the right is >6, but the game is not over yet.    (6) We take the number of the left of 6 out first: 3 1 2 5 4, this time with 3 as the base number to adjust, so that 3 left the number of <3, the right number of >3, according to the previous simulation, this time results: 2 1 3 5 4(7) Then 2 1 key out to reorganize, the results obtained: 1 2(8) The remaining sequence on the right: 9 7 10 8 It's the same way, the final result: 1 2 3 4 5 6 7 8 9 "Summary" each round of the quick sort is actually a return of the base number of this round, and when all the numbers are set, the order is over.    

"References" source of text and illustrations "Aha!" Algorithm

[C #] with rookie thinking learning algorithm--toilet sort, bubble sort and quick sort

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.