# Sorting Algorithm sorting by Bubble Sorting

Source: Internet
Author: User

Bubble Sorting is a simple sorting algorithm. It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.

The Bubble Sorting Algorithm operates as follows:

1. Compare adjacent elements. If the first is bigger than the second, exchange the two of them.

2. perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number.

3. Repeat the preceding steps for all elements except the last one.

4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.

.

Step 1: We compared 40 to 20 and found that 40 is the boss and there is no need to exchange.

Step 2: Take another step forward, that is, compare 20 to 30, and find that 30 is the boss, it is necessary to exchange.

Step 3: Take the ratio of 20 to 10 after the exchange and find that you are the boss and do not need to exchange.

Step 4: Exchange 10 with 50, and exchange 50 with the boss.

Finally, we get the first minimum value until the entire array is cyclically sorted.

Implementation Code:

`Public class bubblesort {// Bubble Sorting Algorithm public static int [] bubblesort (INT [] array) {int temp; // The first loop: indicates the number of times to be compared, length-1 for (INT I = 0; I <array. length-1; I ++) {// loop starting from the last element // J> I: The subscript from the back must be greater than the subscript from the back, otherwise it will surpass. For (Int J = array. length-1; j> I; j --) {// if the previous number is greater than the next one, if (array [J-1]> array [J]) {temp = array [J-1]; array [J-1] = array [J]; array [J] = temp ;}} return array ;} public static void main (string [] ARGs) {int [] array = {2, 5, 1, 8, 9, 3}; system. out. println ("Before sorting:" + arrays. tostring (array); bubblesort (array); system. out. println ("sorted:" + arrays. tostring (array ));}}`

Running result:

Before sorting: [2, 5, 1, 8, 9, 3]

After sorting: [1, 2, 3, 5, 8, 9]

Related Keywords:

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.