Java Bubble sort

Source: Internet
Author: User

Remember that there is a written test to write a bubble sort, all of a sudden, although the school has opened the algorithm and structure of the class, but basically in the NAP, there is no understanding of its sorting principle. Well, before the dashing, now the bitter force, while insomnia time to understand the bubble sort.

Online concept a lot, uneven, and finally written out, and can be sorted in ascending order. But compared to the online writing of the order to find that each trip you write compared to the last element, may be the concept did not understand thoroughly (self comfort). Later found this blog description is very good: http://www.cnblogs.com/kkun/archive/2011/11/23/2260280.html, but feel his code logic is a bit incorrect, and comments have such feedback so do not post, Interested friends can go to understand.

A brief description on the blog

Principle: Compare adjacent elements. If the first one is larger than the second, swap them both, in the order from small to large or from large to small, so that the largest or smallest number is exchanged to the last one after the trip.

Then the 22 comparison exchange starts from the beginning, and ends at the second-lowest.

Examples for small to large sort,

Original Array to sort | 6 | 2 | 4 | 1 | 5 | 9 |

First trip sort (outer loop)

First 22 comparison 6 > 2 swap (inner loop)

Pre-swap Status | 6 | 2 | 4 | 1 | 5 | 9 |

Post-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |

Second 22 comparison, 6 > 4 swap

Pre-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |

Post-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |

Third 22 comparison, 6 > 1 swap

Pre-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |

Post-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |

Fourth time 22 comparison, 6 > 5 swap

Pre-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |

Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Fifth time 22 comparison, 6 < 9 no swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Second trip sort (outer loop)

First 22 comparison 2 < 4 no swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Second 22 comparison, 4 > 1 swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Third 22 Comparisons, 4 < 5 non-exchangeable

Pre-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Fourth time 22 comparison, 5 < 6 no swap

Pre-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

...

Bubbling about the process like this

Start with the concept of writing (self-reflection, we do not spray):

1  Private StaticString Bubblesort (int[] Array) {2         intLength =Array.Length;3          for(inti = 0; i < length; i++) {4              for(intj = 0; J < length; J + +) {5 6                 if(J+1 >=length) {7                      Break;8                 }9 Ten                 intFirst =Array[j]; One                 intSecond = Array[j+1]; A                 if(First >second) { -ARRAY[J] =second; -ARRAY[J+1] =First ; the                 } -             } -         } -         returnarrays.tostring (array); +}
View Code

On-line:

1  Public classBubblesort {2 3      Public Static voidMain (string[] args) {4 5         int[] Array =New int[]{90, 12, 15, 15, 12, 90, 9, 21, 51, 3, 2, 1, 7, 8, 9};6System.out.println ("Array:" +arrays.tostring (array));7System.out.println ("Bubblesort:" +Bubblesort (array));8     }9 Ten     Private StaticString Bubblesort (int[] Array) { One         intLength =Array.Length; A          for(inti = 0; i < length; i++) { -              for(intj = 0; J < Length-1-i; J + +) { -  the                 intFirst =Array[j]; -                 intSecond = Array[j+1]; -                 if(First >second) { -ARRAY[J] =second; +ARRAY[J+1] =First ; -                 } +             } A         } at         returnarrays.tostring (array); -     } -}

Java 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.