Sort the bubble sort

Source: Internet
Author: User

Reference article:

http://blog.csdn.net/it_zjyang/article/details/53406764

http://blog.csdn.net/hacker00011000/article/details/52176100

Bubble sort (Bubble sort)

principle : Compare two adjacent elements in turn, exchanging large value elements to the right end. First, the 1th and 2nd numbers are compared, the decimals are placed before the large number is placed. And so on Until all sorts are complete. N number to sort complete, a total of N-1 , the number of orders per I trip is (n-i) times
Complexity of Time :O (n2)

Example: To sort an array: int[] arr={6,3,8,2,9,1};

First trip Sort:

First Order: 6 and 3 comparison, 6 greater than 3, swap position: 3 6 8 2 9 1
Second Order: 6 and 8 comparison, 6 less than 8, not swap position: 3 6 8 2 9 1
Third Order: 8 and 2 comparison, 8 greater than 2, swap position: 3 6 2 8 9 1
Fourth order: 8 and 9 comparisons, 8 less than 9, not swapped position: 3 6 2 8 9 1
Fifth Order: 9 and 1 comparison: 9 greater than 1, swap position: 3 6 2 8 1 9
The first trip was a total of 5 comparisons, sorted results: 3 6 2 8 1 9

Second trip sort:

First Order: 3 and 6 comparison, 3 less than 6, not swap position: 3 6 2 8 1 9
Second Order: 6 and 2 comparison, 6 greater than 2, swap position: 3 2 6 8 1 9
Third Order: 6 and 8 comparison, 6 greater than 8, not swap position: 3 2 6 8 1 9
Fourth order: 8 and 1 comparisons, 8 greater than 1, Exchange position: 3 2 6 1 8 9
The second trip took a total of 4 comparisons, sorted results: 3 2 6 1 8 9

Third trip Sort:

First Order: 3 and 2 comparison, 3 greater than 2, swap position: 2 3 6 1 8 9
Second Order: 3 and 6 comparison, 3 less than 6, not swap position: 2 3 6 1 8 9
Third Order: 6 and 1 comparison, 6 greater than 1, swap position: 2 3 1 6 8 9
The second trip took a total of 3 comparisons, sorted results: 2 3 1 6 8 9

Sequence Four:

First Order: 2 and 3 comparison, 2 less than 3, not swap position: 2 3 1 6 8 9
Second order: 3 and 1 comparison, 3 greater than 1, swap position: 2 1 3 6 8 9
The second trip took a total of 2 comparisons, sorted results: 2 1 3 6 8 9

The order of five times:

First Order: 2 and 1 comparison, 2 greater than 1, swap position: 1 2 3 6 8 9
The second trip took a total of 1 comparisons, sorted results: 1 2 3 6 8 9

 public  static  void  bubblesort (int  [] nums)        { int   temp;  for  (int  i=0;i<nums.length;i++  for  (int  j=i+1;j<nums.length;j++ if (Nums[i]>nums[j]) {temp  =nums[i]                    ;                    Nums[i]  =nums[j];                NUMS[J]  =TEMP; }            }        }    }
View Code

Sort the bubble 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.