Sort three common structure

Selection sort (in ascending order as an example): by the first internal loop through the array to find the smallest element of the first array element and the exchange position, the second pass of the second array to find the array of smaller elements with the second element exchange position, when the next memory cycle after traversing find the smallest element and exchange position should steer clear of this smallest element. This sorted array of any structure is O (n²) time complexity

private static void meth2() {
        // TODO Auto-generated method stub
        int[] num = { 2, 5, 7, 1, 4 };
        int min = 0;
        int length = num.length;
        for (int i = 0; i < (length - 1); i++) {
            min = i;
            for (int j = i + 1; j < length; j++) {
                if (num[j] < num[min]) {

                    min = j;
                }
            }
            int temp = num[min];
            num[min] = num[i];
            num[i] = temp;
        }
        System.out.println(Arrays.toString(num));

    }

Insertion sort (in ascending order as an example): it assumes that the first element is smallest element, determines the size of the second element to the first element, if the second to the first small, then the switch position, this time the first and the second has been sorted, by comparing the third element of the preceding row has a good second element, if greater than the second, the next cycle is performed, otherwise, proceed with the exchange location after the first element comparison, external control loop until reaching the end of the array is reached. This sort has a relative selection sort has the advantage that if the array itself has been partially sorted, and when the maximum value is already sorted when compared to a maximum value if the element is greater than the latter in comparison it will ignore comparisons with other elements, saving time.

static void meth3 Private () {
        // the TODO Auto-Generated Method Stub
        Scanner Scanner new new SCR = (the System.in);
        int [] = {NUM 2,. 5,. 7,. 1,. 4};
        System.out.println ( " enter what you're looking ");
        int in scr.nextInt = ();

        int = dext Sposo (whether in);

        IF (DEXT == -1) {
            System.out.println ( "Goodbye content does not !!");

        The else {}
            System.out.println ( "subscripts" + dext);

        }

    }

 

 

Bubble sort (in ascending sort for example): Bubble sort of thinking is from left to right (right to left) were the size of neighboring elements of judgment, if the latter element is less than the previous element, switching position, a loop after the maximum value on the far right.

private static void meth1() {
        // TODO Auto-generated method stub
        int[] num = { 2, 5, 7, 1, 4 };
        for (int i = 0; i < num.length - 1; i++) {
            for (int j = 0; j < num.length - 1 - i; j++) {
                if (num[j] < num[j + 1]) {
                    int temp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                }

            }

        }
        System.out.println(Arrays.toString(num));

    }
 

Guess you like

Origin blog.csdn.net/qq_45097560/article/details/91388316