Bubble sort and choose sort by Difference python

Source: Internet
Author: User
Tags array sort

Reference: https://www.cnblogs.com/banana201/p/4928733.html

# # Bubble Sort Method (Bubblesort) # #

The so-called ordering method, that is, a set of unordered sequence order (from large to small, or small to large), then what is called bubble sorting method, bubble sorting method and how to achieve an orderly array of arrays.

The specific implementation of the bubbling sorting method is this, starting from the first element of the array ' arr[0] ', 22 comparison * * (' arr[n],arr[n+1] '), if the previous number is greater than the number of subsequent (' arr[n] > Arr[n+1] '), then the position of the two element is exchanged, Move the large number backwards. The largest number will be swapped to the last position (Arr[n-1]) after a round of comparisons in turn.

Let's take a look at how the bubble sorting method is sorted.

Array sort before 7 23 12 4 33 21 2 17 13 9
First round sort 7 12 4 23 21 2 17 13 9 33
Second round sort 7 4 12 21 2 17 13 9 23
Third round sort 4 7 12 2 17 13 9 21
Fourth round sort 4 7 2 12 13 9 17
Fifth round sort 4 2 7 12 9 13
Sixth round sort 2 4 7 9 12
Seventh round sort 2 4 7 9
Eighth round sort 2 4 7
Nineth Round sort 2 4

As you can see, the order of each round, the largest number of elements participating in the comparison in this round, will float to the end. and the name of the bubble sort comes from here, too.

# # Selection Sorting Method (Selectionsort) # #

What does the so-called choice mean, the choice is to choose one of the thousands of flowers, in the choice of sorting method is said, in each cycle, by comparing the choice you need * * * *.

The process of selecting the sorting method is that the pass-through comparison, the selection of the most important element in each round, and then the most important element in the round to exchange * *, so the key is to record the results of each comparison, that is, after each comparison of the maximum position (subscript).

Let's take a look at the process of selecting sorting:

Array sort before 7 23 12 4 33 21 2 17 13 9
First round cycle 2 23 12 4 33 21 7 17 13 9
Second round cycle 4 12 23 33 21 7 17 13 9
Third round cycle 7 23 33 21 12 17 13 9
Fourth round cycle 9 33 21 12 17 13 23
Fifth round cycle 12 21 33 17 13 23
Sixth round cycle 13 33 17 21 23
Seventh round cycle 17 33 21 23
Eighth round cycle 21 33 22
Nineth Round Cycle 22 33

Through this process, we can see that every round of the cycle, will find the most value element, the next round of sorting will not have to consider this element.

Bubble sort and choose sort by Difference python

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.