Java eight sorting sorting algorithm of choice

First, the moving map presentation

Second, the idea of ​​analysis

1. The first compared with all the numbers behind, is less than (or less than) the first number when the temporary index smaller number, after the end of the first pass, the first number, and staging that the minimum number of the exchange, the first number is the minimum (or maximum number)

2. The index position to a second, compared with the second number of digits after all, a trip down, determining a second small (or the second large) number

Repeat the above steps

Until the pointer moves to the penultimate position, determine the penultimate small (or penultimate large) number, then the last one will determine the sort is complete.

Third, the negative heteroaryl analysis

1. Regardless of whether the original array orderly and time complexity is O (n2),

Because no one should compare the number with a number of other, (n-1) 2 times, decomposition: n2-2n + 1, remove the low power and constant, n2 remaining, so the final time complexity is n2

2. The space complexity is O (1), since only two auxiliary variables are defined, regardless of the size of n, the spatial complexity is O (1)

java.util.Arrays Import;
public class the Main {
    public static void main (String [] args) {
        int [] = n-new new int [] {1,6,3,8,33,27,66,9,7, } 88;
        int TEMP, index = -1;
        for (int I = 0; I <-n.length. 1; I ++) {
            index = I;
            // If so, a small number of temporary subscript
            for (int . 1 = I + J; J <n.length; J ++) {
                IF (n-[index]> n-[J]) {
                    index = J;
                }
            }
            //// trip down to the minimum number obtained, and this exchanging data
            IF (index> 0) {
                TEMP n-= [I];
                n-[I] n-= [index];
                n-[index] = TEMP;
            }
            System.out.println(Arrays.toString(n));
        }
        System.out.println(Arrays.toString(n));
    }
}

Guess you like

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