Java eight insertion sort algorithm to sort of explain

First, the moving map presentation

Second, the idea of ​​analysis

For example, in ascending order:

1. Starting from the second traverse,

2. The current number (the second digit is the first pass) is compared with the preceding sequence number, if the number is greater than the current number of the front, this number will be placed on the current number of positions, the current index number -1,

3. Repeat the above steps until the current number is not larger than a certain number up to the front, then the current number, put this position,

  1-3 foregoing step is to ensure that the current number is the number ordered, the purpose of the current inner loop is inserted in front of the number in the ordered sequence

4. Repeat step 3 until the traverse to the last digit, the last digit and inserted into an appropriate position, the end of the insertion sort.

The idea of ​​the analysis, each pass execution flow as shown below:

Third, the complexity analysis

1. Time Complexity: insertion algorithm, it is to ensure that the front of the ordered sequence is simply inserted in front of the current number of take a certain location.

Under the best case time complexity so if originally ordered array, the array is O (n)

If the array is exactly down = reverse, such as the original array is 54,321, want arranged small to large, the number of each pass to be moved back in front of, to be performed a total of n-1 + n-2 + ... + 2 + 1 = n * (n-1) / 2 = 0.5 * n2 - 0.5 * n times, and remove the low power factor, so the worst-case time complexity of O (n2)

Average time complexity (n + n2) / 2, so that the average time complexity is O (n2)

2. spatial complexity: insertion sort, only two current number of temporary variables, and the subscript, n, regardless of the size, the space complexity: O (1)

Four, Java code is as follows

java.util.Arrays Import;
public class insertSort {
    public static void main (String [] args) {
        int [] = n-new new int [] {20,12,15,1,5,49,58,24,578,211,20,214, } 78,35,125,789,11;
        int TEMP = 0, J;
        for (int I =. 1; I <n.length; I ++) {
            TEMP n-= [I];
            for (I = J; J> 0; J, ) {
                // if the current count is greater than the current number of the foregoing, the number put in front of a position rearwardly shifted
                IF (n-[-J. 1]> TEMP) {
                    n-[J] = n-[-J. 1];
                   
                    // has moved to a second number of the first number, the number of the current into the first position, the end of this trip
                    IF (J ==. 1) {
                        n-[-J. 1] = TEMP;
                        BREAK;
                    }

                } else {// If not, the current number into a position j, the end of this trip
               
                    n-[j] = TEMP;
                    BREAK;
                }
            }
            System.out.println (of Arrays.toString (n-));
        }
        the System. Out.println (of Arrays.toString (n-));
    }
}

 

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159800.htm