Sort the bubble sort

Source: Internet
Author: User

Objective

This blog is based on the blog of the Brothers, the blog address click on it can go in, there are many good blog, my sorting algorithm is from this; some of the concepts of data architecture I will not elaborate on, Wu fan's blog is explained in detail, and I write these blog just to record their learning process, Added some of their own understanding, but also want to provide help to others.

  No matter what programming language you learn, when you are learning loops and arrays, you usually introduce a sort algorithm as an example, and this algorithm is generally bubble sort. It's not a nice name, but the simplest and easiest way to understand the algorithm. So, even if you've probably already learned the bubble sort, we'll start our sort trip from this algorithm.

Basic ideas

22 compares the keywords of adjacent records if the inverse is exchanged until there is no reverse order of the records. Bubbling implementations can vary a lot in detail, and we'll explain the idea of bubbling sorting in the simplest form of bubbling implementation code.

Code implementation

1     /**2 * Bubble Sort3 * Heavy down, small4      * @paramarr5      */6      Public voidButtlesort (int[] arr) {7         intLen =arr.length;8         //I indicates the first round comparison (9 numbers only need to compare 8 times)9          for(intI=1; i<len; i++){Ten             // The big down, the small to go up One              for(intj=len-1; j>=i; j--){ A                 if(Arr[j-1] > Arr[j]) {//if the former is greater than the latter -Swap (ARR,J-1,J);//Exchange Arr[j-1] and Arr[j] -                 } the             } -         } -}

Execute process simulation

Assuming that the sequence we want to sort is still {5,3,7,9,1,6,4,8,2}, when I=1, the variable j is reversed from 8 to 1, one by one, and the smaller value is exchanged in front until the lowest value is found at the 1th position. 5-1, when i=1,j=8, we found 8>2, so exchanged their position, j=7 when, 4>2, so exchange ... Until j=5, because 1<2, where does not exchange. J=1, 5>1, swap, finally get the minimum value of 1 place first position. In fact, in the process of continuous circulation, in addition to the keyword 1 in the first place, we also put the keyword 2 from the nineth position to the sixth location. The smaller numbers in the figure float up to the top as bubbles, so the algorithm is named Bubble algorithm.

When i=2, the variable j is reversed from 8 to 2, one by one, when the keyword 2 is swapped to the second position, and the keyword 4 is increased.

The following process is the same, here is not to repeat.

summary

Bubble sort is better understood, it should be no difficulty, but the above code can be improved. Imagine if we were to sort the sequence is {2,1,3,4,5,6,7,8,9}, that is, except that the first and second keywords need to be swapped out, everything else is already in the normal order. When the I=1, swapped 2 and 1, the sequence is now orderly, but the algorithm is still bricks i=2 to 9, and each loop in the J loop is executed once, although there is no exchange of data, but after the large number of comparisons is greatly redundant. so how to improve, it is to everyone's study questions !

There is no way to improve, you can click here to talk about the optimization of the bubble sort.

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.