Learning algorithms--toilet sorting, bubbling sorting, and quick sorting

Source: Internet
Author: User

Catalogue
    • Toilet sort (sickening sort)
    • Bubble sort (algorithm to ask for interview)
    • Quick sort (witness the love Journey of Adam and Eve)

toilet sort (sickening sort)first, 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. Figure: Here are 11 toilets 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; Here you use C # to give a simple algorithm process.
1Staticvoid Main (String[] args)2{3var scores =NewInt[] {5,3,5,2,8};4var newscores =Newint[9];56for (int i =0; I < scores. Length; i++)7{8var score =Scores[i];9 newscores[score]++;10}1112for (int i = newscores.length-1; I >=0; i--)13{14var num =Newscores[i];15 for (int j = 1; J <= Num; J++) 16  {17 console.write ($" {i} " ); 18 }19 }20 21  Console.WriteLine (); 22  Console.read ();  }24}        

bubble sort (algorithm to ask for interview)First, the basic idea: each comparison of adjacent two elements, on-demand adjustment of the order Second, the title: Request 12 35 99 18 76 These 5 numbers from the big to the small sort Third, 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--①--> 99 35 18 76 12--②--> 99 35 18 76 12--③-->in 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--------99 76 35 18 12 (compare 2 times)(8) 4th trip: 99 76 35 18 12, 4 numbers are in place, so the last number does not need 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 (N2), which is a very high time complexity.    Here you use C # to give a simple algorithm process.
1Staticvoid Main (String[] args)2{3var nums =NewInt[] {12,35,99,18,76};4Output (Nums);56for (Int J =0; J < Nums. Length-1; J + +)7{8for (int i =0; I < Nums. Length-1; i++)9{10if (Nums[i] < Nums[i +1])11{12var temp =Nums[i];Nums[i] = nums[i +1];Nums[i +1] =Temp15}16}1718Output (Nums);19}2021stConsole.read ();22}2324///<summary>25///Console output26///</summary>27///<param name= "Nums" ></param>28static void Output (int< Span style= "COLOR: #000000" >[] nums) 29  {30 foreach (var num in Nums) 31  {32 console.write ($ "{num}  "); 33 }34 35  Console.WriteLine ();                  

Quick Sort (witness the love Journey of Adam and Eve)First, 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. Diagram: Adam I and Eve J(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 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. Now the base number 6 and 3 are exchanged, the result: 3 1 2 5 4 6 9 7 ten 8; now 6 The left is <6, and the number on the right is >6, but the game is not over yet.

    

Figure: Adam and Eve finally have a intersection(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 That's the way to go, the final result: 1 2 3 4 5 6 7 8 9 10 (specific)"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.    

Learning algorithms--toilet sorting, bubbling sorting, and quick sorting

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.