Bubble sort and select sort

Source: Internet
Author: User
Tags rounds

Example array:

$array = [62, 88, 58, 47, 62, 35, 73, 51, 99, 37, 93];

First, bubble sort

The array is divided into two parts ordered and unordered, and the adjacent elements in the unordered sequence are compared, if the preceding element value is greater than the value of the element behind it, and the first round is sorted, the maximum (99) is placed at the front of the ordered sequence (10), i.e. the element in the ordered sequence is [99], as follows:

Array ([0] = [1] = + [2] = [3] = [4] = [5] [6] = [7] [8] = +/--[+] = + Panax Notoginseng [9] = [Ten] = 99)

The second round of sorting, in the same vein, from the first element in the unordered sequence, the adjacent elements are compared, if the former is greater than the latter, the exchange of data, at the end of the 9 position (10 is the position of the ordered sequence), the second round after the end, the second round of the largest element inserted in the front of the ordered sequence The element of the ordered sequence is [93,99], and the result is as follows:

Array ([0] = [1] = [2] = [3] = [4] = [5] = [6] = [7] = [8] = [9] = = [Ten] = 99)

And so on, after 10 rounds complete the sort, as follows:

1th Time Sort End
Array ([0] = [1] = + [2] = [3] = [4] = [5] [6] = [7] [8] = +/--[+] = + Panax Notoginseng [9] = [[Ten] = ]


2nd time Sort End
Array ([0] = [1] = [2] = [3] = [4] = [5] = [6] = [7] = [8] = [9] = [+] = [Ten] ~ ~


3rd time Sort End
Array ([0] = [1] = + [2] = [3] [4] = [5] = [6] = [7] = [ 8] =& Gt [9] = [+] = [Ten] ~ ~


4th Time Sort End
Array ([0] = [1] = [2] = + [3] = [4] [5] = [6] = [7] = + + [73]------[+] = 8 Gt [9] = [+] = [Ten] ~ ~


5th Time Sort End
Array ([0] = + [1] = [2] = [3] [+] [4] = [5] = [6] = [7] = 73 [8] =& Gt [9] = [+] = [Ten] ~ ~


6th time Sort End
Array ([0] = [1] = [2] = [3] [+] [4] [5] [6] [7] = + [73] = + 8 Gt [9] = [+] = [Ten] ~ ~


7th Time Sort End
Array ([0] [+] [1] = [2] = [3] = [4] [5] [6] = + + + [7] = 73 [8]------[+]--[+]---] =& Gt [9] = [+] = [Ten] ~ ~


8th Time Sort End
Array ([0] = + [1] = [2] = [3] = [4] [+] + [5] [6] +/-+ [7] = + 73 [8] =& Gt [9] = [+] = [Ten] ~ ~


9th Time Sort End
Array ([0] = + [1] = [2] = [3] = [4] [+] + [5] [6] +/-+ [7] = + 73 [8] =& Gt [9] = [+] = [Ten] ~ ~


10th Time Sort End
Array ([0] = + [1] = [2] = [3] = [4] [+] + [5] [6] +/-+ [7] = + 73 [8] =& Gt [9] = [+] = [Ten] ~ ~

Second, choose the sort

The array is divided into ordered and unordered two parts, ordered sequence in the front, unordered sequence in the back, each round sorting the first element in the unordered sequence is compared with the remaining elements, find the smallest element and put it to the end of the ordered sequence.

The first round of sorting, the ordered sequence is empty by default, finds the smallest element (35) in the array, and puts it in an ordered sequence (with only 351 elements in an ordered sequence), the result is as follows:

Array ([0] = [1] = [2] = + [3] [+] [4] [5] [+] = [6] = [7] = [8] = [9] = [93]

And so on, the 10 rounds are sorted and ended, as follows:

1th Time Sort End
Array ( [0] = [ 1] = [2] = [3] [+] [4] [5] = [6] = 73 [7] = > [8] = [9] = [+] [93] [] = [+]]


2nd time Sort End
Array ( [0] [+] = [1] = [2] = [3] = [4] = [+] + [5] = [6] [+] +/-] = [7] = 51 [ 8] = [9] = [Ten] + 93)


3rd time Sort End
Array ([0] [+] = [ 1] = [2] [3] [4] [5] [6] = [+] = [+] =] = [7] = 58 [ 8] = [9] = [93] [ten] + []]


4th Time Sort End
Array ([0] [+] = [ 1] = [2] [3 ] [4] = [5] = + [6] [7] = + [+] = [+]] = 62 [ 8] = [9] = + [93]


5th Time Sort End
Array ([0] [+] = [ 1] = [2] [3 ] [+] = [4] [+] + [5] = [6] [7] * * * * +] [+] = 62 [ 8] = [9] = [ten] = 93)


6th time Sort End
Array ([0] [+] = [ 1] = [2] [3 ] [+] = [4] + [5] [6] = + + + [7] = 73 [ 8] = [9] = [ten] = 93)


7th Time Sort End
Array ([0] [+] = [ 1] = [2] [3 ] = [4] [+] + [+] + [5] = [6] = [7] = 88 [ 8] = [9] = [93] = [ten] [] + []]


8th Time Sort End
Array ( [0] [+] = [1] = [2] [3] + [4] [5] [6] + + [+] = [7] [+] = [+] = [+]] [ 8] = [9] = [+] [93]


9th Time Sort End
Array ( [0] = + [1] = [2] = [3] = [4] [+] + [5] [6] +/-+ [7] = + 73 [8] =& Gt [9] = [ten] = 93)


10th Time Sort End
Array ( [0] = + [1] = [2] = [3] = [4] [+] + [5] [6] +/-+ [7] = + 73 [8] =& Gt [9] = = [Ten] = 99)

Two things in common:

The array is divided into ordered sequence and unordered sequence, and the elements filtered by each round are inserted into ordered sequence, and the yellow part is ordered sequence.

Different points:

Bubble sort--neighbor element comparison, find the maximum value, put in the front of the ordered sequence;

Select Sort--Compare the first element in the unordered sequence with the remaining unordered sequence element to find the smallest element and insert the last side of the ordered sequence

Bubble sort and select sort

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.