Vernacular resolved bubble sort optimization

        Bubble sort is that every two adjacent data after comparison, in ascending descending exchange, at least every sort make a maximum or minimum element (default ordering from left to right, in ascending order of the maximum element in descending order of the smallest element ) to move the tail after repeated N times, the sorting is completed.

       Suppose we  2,5,1,3,4,6  this integer array to be sorted.

       We first look before optimization bubble sort:

 public static int[] bubbleSort(int a[]){
        int n=a.length;
        for(int i=0;i<n;i++){
            for(int j=0;j<n-i-1;j++){
                if(a[j]>a[j+1]){
                    int tmp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=tmp;
                }
            }
       
        }
        return a;
    }

Ordering process: 

                                Initial 2,5,1,3,4,6
                                first 2,1,3,4,5,6
                                1,2,3,4,5,6 second
                                third 1,2,3,4 , 5,6
                                fourth 1,2,3,4,5,6
                                fifth 1,2,3,4,5,6
                                sixth 1,2,3,4,5,6

  in conclusion:

        We can see the original bubble sort, sort number of times 6 times. After the second array has been ordered, of which the third, fourth, fifth, sixth sorting are superfluous.

Optimized:

public static int[] bubbleSort(int a[]){
        int n=a.length;
        for(int i=0;i<n;i++){
            boolean flag=true;
            for(int j=0;j<n-i-1;j++){
                if(a[j]>a[j+1]){
                    int tmp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=tmp;
                    flag=false;
                }
            }
            if(flag)break;
        }
        return a;
    }

Ordering process: 

                                Initial 2,5,1,3,4,6
                                first 2,1,3,4,5,6
                                second 1,2,3,4,5,6

 

 Optimization Analysis:

       The main plus for us to optimize the flag Boolean variable, in the inner loop j is a local variable, if there is no data exchange occurs when a particular sort, then we believe that this array of integers has reached the fully ordered.

      Why is it so:

        We look to optimize the code above, did not happen after the exchange of data in the third. At this time, since I = 2 n-=. 6  , the inner loop is: for (int J = 0; J <. 3; J ++)   before the three elements will 1,2,3 , respectively, with the adjacent elements 2, 3 4  comparison: find are ordered, without the need for data exchange occurs, the first four elements must be completely ordered, then the two elements is already the biggest bubble of 2 data, when a particular sort did not happen data exchange, we believe that this array of integers has reached the fully ordered.

  in conclusion:

        We can see bubble sort, this sort of optimization for the number of times 2 times. Optimized number before ordering six times, four times to reduce sorting time. Careful observation will find that we reduce the number and array of  degree of order  related.

        So what is the degree of order: the name suggests is ordered (ascending or descending) the number of initial array , we have the above-mentioned  2,5,1,3,4,6 array, for example, from left to right by default ascending order, as long as the foregoing elements is less than the latter element that is considered an ordered pair, which is a degree of order  11 , may be interested students ordered elements to be sent to a review.

       That is, the higher the degree of order, the number of sorting optimized will be less.

Expansion and divergence:

     In fact according to the degree of order to optimize this sort of thinking, have already been made in the JDK is Arrays.sort () of  DualPivotQuicksort.sort () method, which is Collections.sort () method when ordering a set of sorting, Chinese called biaxial benchmark quick sort , this idea is to use the following code:

The above code says: When the array is very high degree of order, will use the merge algorithm so that an orderly array, and merge algorithm is specifically for orderly array to be sorted.

to sum up:

      When we use the algorithm first met a particular set of data into, and then analyze the code line by line until after fully understood. Go to even think of a better way to solve this problem, the better way to solve, will continue to think about whether this generation of optimized way into other algorithms, and finally to sum up the recovery disk.

      When we encounter any problems using the above cycle of thinking to think again after, we will gradually increase the depth of thinking, problem-solving skills will become increasingly strong, the code bug will be less and less, pay more and will higher.

    Popular point that: Do not worry, be patient, hello world who is not from the beginning of it

 

 

Published 25 original articles · won praise 51 · views 20000 +

Guess you like

Origin blog.csdn.net/Royal_lr/article/details/102856998