Bubble sort of common algorithms

Source: Internet
Author: User

Bubble Sort:

is said to be the first of the eight, the popular meaning is to say, in a group of data, the adjacent elements in order to compare the size, the largest put back, the smallest to come up (or in turn)

Bubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

The bubbling sorting algorithm works as follows:

    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
    3. Repeat the above steps for all elements, except for the last one.
    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

1, determine the position of the 1th number: in turn, compared with other data:

-The 1th time and 2 compared to 2, then the right shift 1 to 2 position, this time, 1 in the position of 2

--compared with 3, if it is larger than 3, then the position of 1 to 3 is shifted to the right, it is not necessary to move, when 1 is in 3 position

-Compared with 4, if it is larger than 4, then move to the right 1-bit to 4 position, it is not necessary to move, this time 1 in the 4 position

-Compared with 5, if it is larger than 5, then move to the right 1-bit to 5 position, it is not necessary to move, this time 1 in the 5 position

After 4 comparisons, we can determine the position of 1.

is the flowchart:

2, determine the position of the 2nd number: In turn, with the exception of 1 other than the data (because the 1th round compared to 1, has been compared, has determined the position of 1, so there is no need to compare with 1):

--The 1th time and 3 compared to 3, then the right shift 1 to 3 position, this time, 2 in the 3 position, it is not necessary to move

--compared with 4, if it is larger than 4, then move to the right 1 to 4, this time 2 is in the 4 position, it is not very moving

-Compared with 5, if it is larger than 5, then move to the right 1-bit to 5 position, this time 1 in the 5 position, it is not necessary to move

After 3 comparisons, we can determine the position of 2.

3, determine the position of the 3rd number: In turn, with the exception of 1, 2 other than the data (because the 1th round, 2nd round comparison 1, 2, has been compared, has determined the position of 1, 2, so there is no need to compare with 1, 2):

--The 1th time and 4 compared to 4, then the right shift 1 to 4 position, this time, 3 in the 4 position, it is not necessary to move

--compared with 5, if it is larger than 5, then move to the right 1 to 5, this time 2 is in the 5 position, it is not very moving

After 2 comparisons, we can determine the position of 3.

4, determine the position of the 4th number: In turn, with the exception of 1, 2, 3 compared to other data (because the 1th, 2nd, 3rd Round 1, 2, 3 when the time has been compared, has determined the position of 1, 2, 3, so there is no need to compare with 1, 2, 3):

--The 1th time and 5 compared to 5, then the right shift 1 to 5 position, this time, 4 in the 5 position, it is not necessary to move

After 1 comparisons, we can determine the position of 4.

5, the last 5 do not have to compare, because has determined the rest of the position, the first round has compared with 5, so the other 4 pits accounted for, the remaining 5 you have what choice? has been fixed.

Above I only in the 1th round of the time of the figure, so that we can understand the queued of a general situation, near to analyze, then, let us analyze, how to implement this logic with the program:

1, first we set 5 number bar, then we can put these 5 numbers in an array. Int[] nums={23,12,34,2,67} or int[] Nums=new int[]{23,12,34,2,67}, here's how to use: Happy is good!

2, to carry out 4 rounds of comparison to determine, this must be a cycle, fixed number of times, we use for it. for (int i=0;i<4;i++), of course you use for (int i=1;i<=4;i++) also line, but the array is starting from 0, so the habit I starting from 0, so also beneficial to see directly: array [i], otherwise also get: array [i-1]. 4 is also: array. length-1

3, each round to cycle the number of different times to determine the location of the data, then in the 2nd step loop, there will be another loop, which is a multi-loop, also known as nested loops

This for loop, we also want to analyze the scope of it:

The first round (we set the variable i) (inner loop) the secondary variable J

1 corresponding array subscript i is 0 4 outer loop 1th time, the inner layer needs to loop 4 times =5-i-1

2 corresponding array subscript i is 1 3 outer loop 2nd time, the inner layer needs to loop 3 times =5-i-1

3 corresponding array subscript i is 2 2 outer loop 3rd time, the inner layer needs to loop 2 times =5-i-1

4 corresponding array subscript i is 3 1 outer loop 4th time, the inner layer needs to loop 1 times =5-i-1

Inside for loop, we set the variable j,for (int j=0;j<; j + +),j< This value, to find out

The principle of the double cycle is: The outer loop 1 times, the inner Loop 1 rounds (traversal), the table has been clearly marked the number of times in each cycle of J to execute, but because we are I is starting from 0, so the scope of J should be 5-i-1, that is: array. length-1-i

4, there is a condition in the inner loop, that is, the former one is larger than the latter, to move, not the same position, if it involves the move, we need a variable to daoteng the Exchange to move a bit of the 2 values.

/** from small to large * *
private static void Frommintomax () {
int count[] = {89, 18, 57, 48, 27};
for (int i = 0; i < count.length-1; i++) {
for (int j = 0; J < count.length-1-I; j + +) {
if (Count[j] > count[j + 1]) {
int temp = Count[j];
COUNT[J] = count[j + 1];
Count[j + 1] = temp;
}
}
System.out.print ("No." + (i + 1) + "Second order Result:");
for (int k = 0; k < count.length; k++) {
System.out.print (Count[k] + "\ t");
}
System.out.println ();
}
System.out.print ("From small to large sorted results:");
for (int con:count) {
System.out.print (con + "T");
}
}

And of course vice versa from large to small sort examples:

private static void Frommaxtomin () {
int score[] = {20, 79, 35, 60, 89, 18, 45, 58};
for (int i = 0; i < score.length-1; i++) {//N-1 order

Sort the current unordered interval score[0....length-i-1] (narrow control range)
for (int j = 0; J < score.length-i-1; j + +) {
if (Score[j] < Score[j + 1]) {//switch the small value to the rear
int temp = Score[j];
SCORE[J] = score[j + 1];
Score[j + 1] = temp;
}
}
System.out.print ("No." + (i + 1) + "Second order Result:");
for (int a = 0; a < score.length; a++) {
System.out.print (Score[a] + "\ t");
}
System.out.println ("");
}
System.out.print ("Final sorting Result:");
for (int a = 0; a < score.length; a++) {
System.out.print (Score[a] + "\ t");
}
}

Bubble sort of common algorithms

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.