Classic sorting algorithm-Bubblesort

Source: Internet
Author: User
: This article mainly introduces the classic sorting algorithm-bubble sorting Bubblesort. if you are interested in the PHP Tutorial, refer to it.
 

Classic sorting algorithm-Bubble sort

The principle is to compare adjacent numbers in ascending order or in ascending order,

After such a trip, the largest or smallest number is switched to the last one,

Then compare and exchange data from the beginning until the last and second digits are over.

The example is sorted in ascending order,

Original array to be sorted | 6 | 2 | 4 | 1 | 5 | 9 |

First sort (external loop)

For the first time, compare 6> 2 switches (Inner Loop)

Status before switching | 6 | 2 | 4 | 1 | 5 | 9 |

Status after switch | 2 | 6 | 4 | 1 | 5 | 9 |

The second comparison, 6> 4 exchanges

Status before switching | 2 | 6 | 4 | 1 | 5 | 9 |

Status after switch | 2 | 4 | 6 | 1 | 5 | 9 |

The third comparison, 6> 1 Exchange

Status before switching | 2 | 4 | 6 | 1 | 5 | 9 |

Status after switch | 2 | 4 | 1 | 6 | 5 | 9 |

Comparison of the fourth time, 6> 5 exchanges

Status before switching | 2 | 4 | 1 | 6 | 5 | 9 |

Status after switch | 2 | 4 | 1 | 5 | 6 | 9 |

For the fifth comparison, do not swap for 6 <9

Status before switching | 2 | 4 | 1 | 5 | 6 | 9 |

Status after switch | 2 | 4 | 1 | 5 | 6 | 9 |

Second sort (external loop)

For the first time, the comparison is 2 <4 without exchange.

Status before switching | 2 | 4 | 1 | 5 | 6 | 9 |

Status after switch | 2 | 4 | 1 | 5 | 6 | 9 |

The second comparison, 4> 1 Exchange

Status before switching | 2 | 4 | 1 | 5 | 6 | 9 |
Status after switch | 2 | 1 | 4 | 5 | 6 | 9 |

Comparison of the third time in pairs, 4 <5 without exchange

Status before switching | 2 | 1 | 4 | 5 | 6 | 9 |
Status after switch | 2 | 1 | 4 | 5 | 6 | 9 |

For the fourth comparison, do not exchange 5 <6

Status before switching | 2 | 1 | 4 | 5 | 6 | 9 |

Status after switch | 2 | 1 | 4 | 5 | 6 | 9 |

Third sort (external loop)

The first two-to-one comparison 2> 1 Exchange

Status after switch | 2 | 1 | 4 | 5 | 6 | 9 |

Status after switch | 1 | 2 | 4 | 5 | 6 | 9 |

For the second comparison, 2 <4 is not exchanged.

Status after switch | 1 | 2 | 4 | 5 | 6 | 9 |
Status after switch | 1 | 2 | 4 | 5 | 6 | 9 |

Comparison of the third time in pairs, 4 <5 without exchange

Status after switch | 1 | 2 | 4 | 5 | 6 | 9 |
Status after switch | 1 | 2 | 4 | 5 | 6 | 9 |

The fourth sorting (external loop) has no exchange

The fifth sorting (external loop) has no exchange

Paste your own code:

Function bubble_sort ($ arr = array (, 78, 39) {$ len = count ($ arr ); // This layer controls the number of rounds to be bubbling for ($ I = 1; $ I <$ len; $ I ++) {// This layer loop is used to control the number of times a number needs to be compared in each round ($ k = 0; $ k <$ len-$ I; $ k ++) {if ($ arr [$ k]> $ arr [$ k + 1]) {$ tmp = $ arr [$ k + 1]; $ arr [$ k + 1] = $ arr [$ k]; $ arr [$ k] = $ tmp; @ $ arr ['cly _ times '] + = 1; // Set loop end} if ($ arr ['cly _ times '] = $ arr ['cly _ times']-1) {return $ arr ;}} return $ arr ;}

The above introduces the classic sorting algorithm-Bubble sorting Bubble sort, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.