Bubble sorting two Optimizations

Source: Internet
Author: User

(1) The definition of bubble sort is to perform n-1 sorting. Each sorting must compare all adjacent two elements, however, the I-1 elements at the end of array are all sorted in the I-th sorting, so the length of the inner loop is n-I. Algorithm Implementation: [java] void bubbleSort (int array []) {for (int I = 1; I <array. length; ++ I) {for (int j = 0; j <array. length-I; ++ j) {if (array [j]> array [j + 1]) {int tmp = array [j]; array [j] = array [j + 1]; array [j + 1] = tmp ;}}} (2) One-time optimization. We can think of this extreme situation: array itself is sorted, but according to the above algorithm, we still need to traverse n Square/2 times. The reason is that we have no mechanism to check whether the array is sorted. The solution is to add an identifier. If an inner loop is not exchanged, the array is sorted. Algorithm Implementation: [java] void bubbleSort (int array []) {boolean exchange = false; for (int I = 1; I <array. length; ++ I) {for (int j = 0; j <array. length-I; ++ j) {if (array [j]> array [j + 1]) {int tmp = array [j]; array [j] = array [j + 1]; array [j + 1] = tmp; exchange = true ;}} if (! Exchange) {break ;}} (3) Secondary optimization. Let's look at another extreme situation: the array length is n, but only 0, 1 elements are not sorted, so we still need to traverse 2 * n times according to the previous optimization algorithm. The reason is that our inner cycle length is set based on, I sort the array after the I-1 elements are sorted, but in fact, the length of the I-Sort cycle depends on the last exchange position when the I-1 is sorted, for example, the last exchange position of the I-1 is index, it indicates that the elements after the index have been sorted, we only need to record this Index to get the cycle length of the next (I. Algorithm Implementation: [java] void bubbleSort (int array []) {boolean exchange = false; int index = 0; int mark = array. length-1; for (int I = 1; I <array. length; ++ I) {for (int j = 0; j <mark; ++ j) {if (array [j]> array [j + 1]) {int tmp = array [j]; array [j] = array [j + 1]; array [j + 1] = tmp; exchange = true; index = j ;}} if (! Exchange) {break;} mark = index;} summary: of course, this so-called optimization knowledge school statement depends on the level at which you actually work. For example, I am a newbie, so I have always stayed at the bubble sort defined level. Of course, the motivation for learning also comes from this. For example, after this study and summary, it may not be able to rise to the quick sort level, but at least the optimized bubble sort can be applied to the work.

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.