Java programmers learn to share good route optimization and bubble sort

Java programmers learn to share good route optimization and bubble sort, bubble sort is certainly typical sort of exchange, such as the collation is ascending, with the following columns:

  A[0] A[1] A[2] A[3] ...... A[n]

The A [0] and A [1], and if A [0]> A [1], the position of the two elements to be exchanged, otherwise change, and then continue to Comparative A [1] and A [2], until A [n-1] and A [n]. That compares two adjacent elements, if before a large, exchange (or not swap), then continue to compare elements behind, after comparing each round, the largest element will move to the final (complete round bubble); and then start the second round bubble, this will select the second largest element. Bubbling process is repeated until no neighboring elements to be exchanged, the sort is complete, the bubbles as carbonated beverages, and therefore called bubble sort.

   

Simplify the process, a simple array is provided, in programming, the element index starts from 0:

[5, 3, 1, 4, 2]


    int[] nums = {5, 3, 1, 4, 2};

    //要比较n次,n是数组中数字的个数

    for (int i = 0; i < nums.length; i++) {

        //比较j和j+1位置的元素,每比较完一次,最大的元素会移动到末尾

        for (int j = 0; j < nums.length - 1; j++) {

            if (nums[j] > nums[j + 1]) {

                int temp = nums[j + 1];

                nums[j + 1] = nums[j];

                nums[j] = temp;

            }

        }

    }

Comparison of the first round, after this round of comparison, the largest element in the end

[<font color = "red" > 5, 3 </ font>, 1, 4, 2] Comparison 0 and an element exchange [<font color = "red" > 3, 5 </ font>, 1 , 4, 2] <br/>

[3, <font color = " red"> 5, 1 </ font>, 4, 2] Comparison 1 and 2 elements, exchange [3, <font color = " red"> 1, 5 </ font> , 4, 2] <br/>

[3, 1, <font color = "red"> 5, 4 </ font>, 2] Comparative elements 2 and 3, the exchange [3,1, <font color = " red"> 4, 5 </ font>, 2] <br/>

[3, 1, 4, < font color = "red"> 5, 2 </ font>] Comparative 3 and 4 elements, exchange [3, 1, 4, < font color = "red"> 2, 5 </ font>] largest element at the end <br/>

Comparison of the second round, after this round of comparison, at the end of the two largest elements

[<font color = "red" > 3, 1 </ font>, 4, 2, 5] Comparison 0 and an element exchange [<font color = "red" > 1, 3 </ font>, 4 , 2, 5] <br/>

[1, <font color = "red"> 3, 4 </ font>, 2, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 3, 4 </ font >, 2, 5] <br/>

[1, 3, <font color = "red"> 4, 2 </ font>, 5] Comparative 2 and 3 elements, exchange [1, 3, <font color = "red"> 2, 4 </ font>, 5] <br/>

[1, 3, 2, <font color = "red"> 4, 5 </ font>] Comparative 3 and 4 elements, fixed [1, 3, 2, <font color = "red"> 4, 5 </ font>] this comparison is superfluous, because after the first round, the end of the maximum element in <br/>

Comparison of the third round, after this round of comparison, the largest of the three elements to the end

[<Font color = "red"> 1, 3 </ font>, 2, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 3 </ font>, 2, 4, 5] <br/>

[1, <font color = " red"> 3, 2 </ font>, 4, 5] Comparison 1 and 2 elements, exchange [1, <font color = " red"> 2, 3 </ font> , 4, 5] <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] comparison of the excess <br/>

[1, 2, 3, <font color = "red"> 4, 5 </ font>] Comparative 3 and 4 elements, fixed [1, 2, 3, <font color = "red"> 4, 5 </ font>] this comparison is superfluous <br/>

Compare the fourth round, after this round of comparison, the largest of four elements to the end

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 2, 3 </ font > 4, 5] this comparison is superfluous <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] this comparison is superfluous <br/>

[1, 2, 3, <font color = "red"> 4, 5 </ font>] Comparative 3 and 4 elements, fixed [1, 2, 3, <font color = "red"> 4, 5 </ font>] this comparison is superfluous <br/>

Comparison of the fifth round, after this round of comparison, the sorting is done

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5] this comparison is superfluous <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 2, 3 </ font > 4, 5] this comparison is superfluous <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] this comparison is superfluous <br/>

[1, 2, 3, <font color = "red"> 4, 5 </ font>] Comparative 3 and 4 elements, fixed [1, 2, 3, <font color = "red"> 4, 5 </ font>] this comparison is superfluous <br/>

Bubble sort can be seen from scratch each time compared to the end of the comparison, but the actual

After the first round of comparison, the last number is the maximum, the next round is not involved in the comparison

After the second round of comparison, the largest number is the last 2, the comparison is not involved in a

After the third round compared to the last three numbers is the biggest ...

Therefore, when the n-th wheel comparison can be excluded at the end of the element it has been sorted, i.e. the end of the n-1 elements


int[] nums = {5, 3, 1, 4, 2};

        //要比较n次,n是数组中数字的个数

        for (int i = 0; i < nums.length; i++) {

            //比较j和j+1位置的元素,每比较完一次,最大的元素会移动到末尾

            //最后i个元素不参与本次比较

            for (int j = 0; j < nums.length - 1 - i; j++) {

                if (nums[j] > nums[j + 1]) {

                    int temp = nums[j + 1];

                    nums[j + 1] = nums[j];

                    nums[j] = temp;

                }

            }

        }

operation result:

Comparison of the first round, after this round of comparison, the largest element in the end

[<font color = "red" > 5, 3 </ font>, 1, 4, 2] Comparison 0 and an element exchange [<font color = "red" > 3, 5 </ font>, 1 , 4, 2] <br/>

[3, <font color = " red"> 5, 1 </ font>, 4, 2] Comparison 1 and 2 elements, exchange [3, <font color = " red"> 1, 5 </ font> , 4, 2] <br/>

[3, 1, <font color = "red"> 5, 4 </ font>, 2] Comparative elements 2 and 3, the exchange [3,1, <font color = " red"> 4, 5 </ font>, 2] <br/>

[3, 1, 4, < font color = "red"> 5, 2 </ font>] Comparative 3 and 4 elements, exchange [3, 1, 4, < font color = "red"> 2, 5 </ font>] largest element at the end <br/>

Comparison of the second round, after this round of comparison, at the end of the two largest elements

[<font color = "red" > 3, 1 </ font>, 4, 2, 5] Comparison 0 and an element exchange [<font color = "red" > 1, 3 </ font>, 4 , 2, 5] <br/>

[1, <font color = "red"> 3, 4 </ font>, 2, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 3, 4 </ font >, 2, 5] <br/>

[1, 3, <font color = "red"> 4, 2 </ font>, 5] Comparative 2 and 3 elements, exchange [1, 3, <font color = "red"> 2, 4 </ font>, 5] <br/>

Comparison of the third round, after this round of comparison, the largest of the three elements to the end

[<Font color = "red"> 1, 3 </ font>, 2, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 3 </ font>, 2, 4, 5] <br/>

[1, <font color = " red"> 3, 2 </ font>, 4, 5] Comparison 1 and 2 elements, exchange [1, <font color = " red"> 2, 3 </ font> , 4, 5] <br/>

Compare the fourth round, after this round of comparison, the largest of four elements to the end

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5] <br/>

The above algorithm can reduce the number of repeated comparisons, the number of comparisons is fixed. But if the original elements in the array is relatively orderly, the following conditions occur:

The initial array is: [3, 1, 2, 4, 5]

first round

[<Font color = "red"> 3, 1 </ font>, 2, 4, 5] Comparison 0 and an element exchange [<font color = "red"> 1, 3 </ font>, 2 , 4, 5] <br/>

[1, <font color = "red"> 3, 2 </ font>, 4, 5] Comparison 1 and 2 elements, exchange [1, <font color = "red"> 2, 3 </ font> , 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 3 and 4 elements, fixed [1, 2, 3, <font color = "red"> 4, 5 </ font>] <br/>

second round

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 2, 3 </ font >, 4, 5] <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] <br/>

Third round

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 2, 3 </ font >, 4, 5] <br/>

Fourth Round

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] <br/>

Actually elements in the array, beginning only the number 3 position needs to be moved, other elements are relatively orderly. After the completion of the movement 3, the other elements need not necessarily. So after the second round have not found any element of exchange, it means sorting has been completed, the third and fourth rounds are superfluous.

For optimizing bubble sort, if a round is not any comparison exchange takes place, represents the sort has been completed, no further sorted:


        int[] nums = {3, 1, 2, 4, 5};

        boolean flag;//是否交换的标志

        //要比较n次,n是数组中数字的个数

        for (int i = 0; i < nums.length; i++) {

            // 每次遍历标志位都要先置为false,才能判断后面的元素是否发生了交换

            flag = false;

            //比较j和j+1位置的元素,每比较完一次,最大的元素会移动到末尾

            //最后i个元素不参与本次比较

            for (int j = 0; j < nums.length - 1 - i; j++) {

                if (nums[j] > nums[j + 1]) {

                    int temp = nums[j + 1];

                    nums[j + 1] = nums[j];

                    nums[j] = temp;

                    flag = true;    //表示本轮发生了交换

                }

            }

            // 如果为false,代表本轮没有交换,元素已经有序

            if(!flag) break;

        }

    }

The initial array is: [3, 1, 2, 4, 5]

first round

[<Font color = "red"> 3, 1 </ font>, 2, 4, 5] Comparison 0 and an element exchange [<font color = "red"> 1, 3 </ font>, 2 , 4, 5] <br/>

[1, <font color = "red"> 3, 2 </ font>, 4, 5] Comparison 1 and 2 elements, exchange [1, <font color = "red"> 2, 3 </ font> , 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 3 and 4 elements, fixed [1, 2, 3, <font color = "red"> 4, 5 </ font>] <br/>

second round

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparison 1 and 2 elements, fixed [1, <font color = "red"> 2, 3 </ font >, 4, 5] <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] <br/>

In the above ordering, 3 is the last mobile element. After three elements are ordered. So long as the preceding element 3 can compare. The last element of the exchange occurs, the elements behind it is ordered, we do not need to participate compare


int[] nums = {3, 1, 2, 4, 5};

        boolean flag;//是否交换的标志

        int lastExchangeIndex =0;//最后一次交换的位置

        int sortBorder = nums.length - 1;

        //要比较n次,n是数组中数字的个数

        for (int i = 0; i < nums.length; i++) {

            // 每次遍历标志位都要先置为false,才能判断后面的元素是否发生了交换

            flag = false;

            //比较j和j+1位置的元素,每比较完一次,最大的元素会移动到末尾

            //最后i个元素不参与本次比较

            for (int j = 0; j < sortBorder ; j++) {

                if (nums[j] > nums[j + 1]) {

                    int temp = nums[j + 1];

                    nums[j + 1] = nums[j];

                    nums[j] = temp;

                    flag = true;    //表示本轮发生了交换

                    lastExchangeIndex =j;//记录最后一次发生交换的位置

                }

            }

            // 如果为false,代表本轮没有交换,元素已经有序

            sortBorder = lastExchangeIndex;

            if(!flag) break;

        }

operation result:

first round

[<Font color = "red"> 3, 1 </ font>, 2, 4, 5] Comparison 0 and an element exchange [<font color = "red"> 1, 3 </ font>, 2 , 4, 5] <br/>

[1, <font color = "red"> 3, 2 </ font>, 4, 5] Comparison 1 and 2 elements, exchange [1, <font color = "red"> 2, 3 </ font> , 4, 5] <br/>

[1, <font color = "red"> 2, 3 </ font>, 4, 5] Comparative 2 and 3 elements, fixed [1, 2, <font color = "red"> 3, 4 < / font>, 5] <br/>

[1, 2, <font color = "red"> 3, 4 </ font>, 5] Comparative 3 and 4 elements, fixed [1, 2, 3, <font color = "red"> 4, 5 </ font>] <br/>

second round

[<Font color = "red"> 1, 2 </ font>, 3, 4, 5] Comparison 0 and an element fixed [<font color = "red"> 1, 2 </ font>, 3, 4, 5]

Guess you like

Origin blog.51cto.com/14479068/2427460