Classic bubble sort-from low to high

Source: Internet
Author: User
Tags rounds

Bubble Sort:

is said to be the first of eight, the popular meaning is to say, in a group of data, the adjacent elements in turn, the largest back, the smallest to come up

I've simply drawn a diagram here, not a process, but there's an analysis process.

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 of the cycle, there will be another cycle, which is a multi-cycle, also known as the escape cycle

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.

Through the above analysis: Ah, suddenly dawned ~ ~ ~

 PackageCom.cn.u4;/** * @authorAdministrator bubble sort, ascending*/ Public classSortnum { Public Static voidMain (string[] args) {//define an array, which I define with a simple syntax        int[] nums={23,12,34,2,67}; //perform several rounds of comparisons to determine several positions         for(inti=0;i<nums.length-1;i++){             for(intj=0;j<nums.length-1-i;j++){                if(nums[j]>nums[j+1]){                    intTmp=nums[j+1]; Nums[j+1]=Nums[j]; NUMS[J]=tmp; }            }
System.out.print ("First" + (i+1) + "post-order Result:");
for (int k=0;k<nums.length;k++) {
System.out.print (nums[k]+ "");
}system.out.println (""); } System.out.println ("Bubbles after the array ="); for(intnum:nums) {System.out.print (num+" "); } }}

Result output:

This thing, I tangled three days, today 51, went out to look at the house, visited the other people's Mountain do not wild, and come back to the enlightened. Summary: Life, need stimulation!

Classic bubble sort-from low to high

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.