Bubble sorting and a small and small array problem, Bubble Sorting array freshman

Source: Internet
Author: User

Bubble sorting and a small and small array problem, Bubble Sorting array freshman
Bubble Sorting

Bubble Sort is a simple Sorting Algorithm in the computer science field.

The operation of the Bubble Sorting Algorithm is as follows: (from the back to the front)

 

L compares two adjacent elements in order to eliminate the reverse order (the reverse order is a mathematical concept and appears in pairs, for example, 50 or 30 is a pair of the reverse order, the so-called elimination of the reverse order is a big put behind, small front)

L in this case, after a round comparison, the biggest couple is at the end!

L then, continue the new round of comparison. Note that the maximum value after the previous round is no longer involved in the comparison. In this way, the number of participating in the comparison is one less than that in the previous round, until only two values are compared!

 

Therefore, it is a dual loop. The outer layer controls the number of rounds and the inner layer controls the number of comparisons for each round.

If the array has n elements, we need to compare n-1 rounds, that is, the number of external loops!

 

Add another knowledge point:

List-assign values in the array to some variables, from small to large. In turn, assign values from large to small.

 

 

Next, let's talk about the Bubble Sorting I wrote and my own understanding of it:

// Sort the array in ascending order by bubbling. function maopao ($ arr) {// double-layer loop, with outer control. $ I indicates the number of cycles, the number of rounds equals to the number of arrays minus 1, and $ I <$ len is equivalent to the number of Arrays-1, for example, 8. When $ I = 7 is the final comparison, compliant with 8-1 = 7; for ($ I = 1, $ len = count ($ arr); $ I <$ len; $ I ++) {// The inner layer controls the number of comparisons for each round. $ k = subscript. After each round is compared, the last one is no longer involved in the comparison. The subscript starts from 0 for ($ k = 0; $ k <$ len-$ I; $ k ++) {// compare two adjacent numbers. If the preceding value is greater than the latter, change the next position and use the intermediate number, if it is not larger than it, you do not need to change if ($ arr [$ k]> $ arr [$ k + 1]). {// change location $ tem = $ arr [$ k]; $ arr [$ k] = $ arr [$ k + 1]; $ arr [$ k + 1] = $ tem ;}}return $ arr ;}$ arr1 = array (, 64 ); // echo count ($ arr1); echo '<pre>'; print_r (maopao ($ arr1 ));

Result: The array is sorted from small to large.

If the algorithm is to be implemented from large to small, it is also the same algorithm, both of which are exchanged through the comparison of intermediate numbers.

 

Next let's talk about a problem I encountered during the interview: Take an array and let the array be the largest, the second smallest, the third second, and the fourth smallest... In this way, sort.

At that time, I only thought that there was

Array_pop: displays the last data of the array.

Array_shift: displays data from the front of the array.

After sorting the array from large to small, the first one in the pop-up will get the largest, and the last one will get the smallest. Put the two together, the largest and the smallest, continue to retrieve the data until all data is obtained.

I am most happy. I am glad that my technical boss confirmed that my logic is okay. I have been so happy for several days ....

It is not feasible for me to try this method when I go back. Next I will talk about this method myself.

 

Example:

 

 

SoStep 1: First, let the array be sorted from large to small through bubbling.

 

Step 2Recursively put the first (largest) and last (smallest) popped out together and retrieve them until the result is obtained.

 

Effect:

This is a better method. Here I am talking about the method that I just came up with. Ha, don't be surprised.

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.