Detailed ordering of java language selection

In the previous article we talked about bubble sorting, and next we talked about choosing sorting.Selective sorting performs better than bubble sorting in sorting efficiency.

Select the sorting idea: find the minimum subscript, record the minimum subscript, and exchange the data of the minimum subscript and the first position in each cycle.

Sort results: number from smallest to largest.

Five data examples are also illustrated with the idea of selecting and sorting.
The data are: 8, 5, 2, 6, 1.

The first cycle is as follows:

Simple Note: Since subscripts need to be recorded, you need to define a variable to record the minimum subscript with a default value of 0.

The second cycle is as follows:

Simple description: Since the first cycle has sorted the minimum values.Therefore, in the second cycle, the minimum data does not participate in the comparison.

The third cycle is as follows:

Simple description: Sorted data (1,2).Do not participate in this sorting.

The fourth cycle is as follows:

Simple description: Sorted data (1,2,5).Do not participate in this sorting.

Text and illustration are detailed and directly coded:

public static void main(String[] args) {
        // Define an array of 10 data
        int data[] = new int[10];
        // Initialize Array
        initData(data);
        // Print Pre-Sort Array Data
        print(data);
        // Select Sort
        selectSort(data);
        // Print sorted array data
        print(data);
    }

/**
     * Select the sorting idea: find the minimum subscript, record the minimum subscript, and exchange the data of the minimum subscript and the first position in each cycle.
     * 
     * @param data
     */
    private static void selectSort(int[] data) {

        int minIndex = 0;
        // Number of comparisons
        for (int i = 0; i < data.length; i++) {

            minIndex = i;
            // Loop Find Minimum Subscript
            for (int j = i + 1; j < data.length; j++) {

                if (data[j] < data[minIndex]) {
                    // Record subscript
                    minIndex = j;
                }
            }

            // Go here and find the minimum subscript.Exchange data for a location and minimum value subscript
            if (minIndex != i) {//If you are the minimum subscript, you do not need to swap
                int temp = data[i];
                data[i] = data[minIndex];
                data[minIndex] = temp;
            }

        }

    }

 // This method randomly generates 1-100 random numbers
    private static void initData(int[] data) {

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

            data[i] = (int) Math.ceil(Math.random() * 100);
        }

    }

    // This method is used to print array data
    private static void print(int[] data) {

        System.out.print("[");
        for (int i = 0; i < data.length; i++) {

            if (i != data.length - 1) {
                System.out.print(data[i] + ",");
            } else {
                System.out.println(data[i] + "]");
            }
        }

    }

Next: Insert sort details, look forward to....

Posted on Sun, 24 May 2020 09:26:05 -0700 by vinoindiamca