Differences between bubble and Selection Algorithms

Source: Internet
Author: User

1. Bubble

Bubble is 1 2 3 4 5 6 7 8 9

2 1 3 4 5 6 7 8 9

2 3 1 4 5 6 7 8 9

2 3 4 1 5 6 7 8 9

2 3 4 5 1 6 7 8 9

.........

2 3 4 5 6 7 8 9 1

This is the first cycle.

9 8 7 6 5 4 3 2 1

The Code is as follows: This is a descending order. To increase the order, you only need to set if (a [J] <A [J + 1]) change to if (a [J]> A [J + 1])

For (INT I = 0; I "10; I ++)

For (Int J = 0; j <10-I-1; j ++ ){

If (A [J] <A [J + 1])

{

Int S = A [J] + A [J + 1];

A [J] = S-A [J];

A [J + 1] = S-A [J];

}

}

 

2. Select sort

1 2 3 4 5

2 1 3 4 5

3 1 2 4 5

4 1 2 3 5

5 1 2 3 4

This is to compare the first one with the subsequent one. A value is exchanged whenever the condition is not met.

Code

In ascending order, you only need to change if (a [I]> A [J]) to if (a [I] <A [J])

For (INT I = 0; I "10; I ++) // here 10 is the number of Arrays

For (Int J = 0; j <10; j ++) {// The number of 10 Arrays

If (A [I]> A [J])

{

Int S = A [I] + A [J];

A [I] = S-A [J];

A [J] = S-A [I];

}

}

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.