Introduction to Java (Java Array - Array Sorting Algorithm)

First, the array sorting algorithm

    1.1 Bubble sort

    In program design, it is often necessary to sort a set of arrays, which is more convenient for statistics and query.

    Bubble sort is one of the most commonly used array sorting algorithms. The process of sorting array elements always puts decimals forward and large numbers backward, similar to the upward movement of bubbles in water, so it is called bubble sort.

    (1) Basic idea

    The basic idea of ​​bubble sort is to compare the values ​​of adjacent elements. If the conditions are met, exchange the element values, move the smaller element to the front of the array, and move the larger element to the back of the array (that is, the two elements exchange positions). Small elements rise from bottom to top like bubbles.

    (2) Algorithm example

    The bubbling algorithm is implemented by a double-layer loop, in which the outer loop is used to control the number of sorting rounds, generally the length of the array to be sorted is reduced by 1, because only one array element is left in the last loop, no comparison is required, and the array has been completed. sort. The inner loop is mainly used to compare the size of each adjacent element in the array to determine whether to swap positions. The number of comparisons and swaps decreases with the number of sorting rounds.

    (3) Algorithm implementation

    eg : implements bubble sort, positive sort.

public class BubbleSort{
    public static void main(String[] args){
        int[] array = { 63, 4, 24, 1, 3, 15 }; //Create an array whose elements are out of order
        BubbleSort sorter = new BubbleSort(); //Create an object of the bubble sort class
        sorter.sort(array); //Call the sort method to sort the array
    }
    /**
     * Bubble Sort
     *@param array The array to sort
     */
    public void sort(int[] array){
        for(int i = 0 ; i < array.length ; i++){
            / / Compare two adjacent elements, the larger number bubbles back
            for(j = 0 ; j < array.length-i ; j++){
                if(array[j] > array[j+1]){
                    int temp = array[j]; //Save the first element value to a temporary variable
                    array[j] = array[j+1]; //Save the second element value to the first element unit
                    array[j+1] = temp; //Save the temporary variable (that is, the original value of the first element) to the second element
                }
        }
    }
    showArray(array); //Output the array elements after bubble sort
    }
    /**
     * Display all elements in the array
     *@param array array to display
     */
    public void showArray(int[] array){
        for(int i : arrray){ //traverse the array
            System.out.print(">"+i); //Output the value of each array element
        }
        System.out.println();
    }
}
    The running result is: > 1 > 3 > 4 > 15 > 24 > 63 

    The main idea of ​​bubble sort is to compare two adjacent elements, and exchange them if certain conditions are met (such as judging the size or before and after the date, etc.), and arranging the largest (or smallest) element at the end in each loop, The next loop is to do the same for the other elements in the array.

    1.2 Direct selection sort

    The direct selection sorting method is a kind of selection sorting. Its sorting speed is faster than that of bubble sorting, and it is also a commonly used sorting algorithm.

    (1) Basic idea

    The basic idea of ​​direct selection sorting is to compare the sorting position with other array elements, and exchange element values ​​if the conditions are met. Note that the difference between bubble sorting is not to exchange adjacent elements, but to compare the elements that meet the conditions with the specified sorting position. Swap (such as sorting from the last element), so that the sorted position gradually expands, and finally the entire array becomes the sorted format.

    Compared with bubble sort, direct selection sort has much fewer exchanges, so it is faster.

    (2) Algorithm example

    Each pass selects the smallest (or largest) element from the data elements to be sorted, and sequentially places it at the end of the sorted array until all the data elements to be sorted are sorted.

First acquaintance with array resources [ 63 4 24 1 3 15 ]
After the first round of sorting [ 15 4 24 1 3 ] 63
After the second round of sorting [ 15 4 3 1 ] 24 63
After the third round of sorting [ 1 4 3 ] 15 24 63
After the fourth round of sorting [ 1 3 ] 4 15 24 63
After the fifth sort [ 1 ] 3 4 15 24 63

    (3) Algorithm implementation

    Implement direct selection sort, positive sort.

/**
* Direct selection sort algorithm example
*/
public class SelectSort{
    public static void main(String[] args){
        int[] array = { 63, 4, 24, 1, 3, 15 }; //create an array
        SelectSort sorter = new SelectSort(); //Create an object of direct selection sorter
        sorter.sort(array); //Call the method of the sort object to sort the array
    }     
    /**
    * Direct selection sort
    * @param array The array to sort
    */
    public void sort(int[] array){
        int index ;
        for(int i = 1 ; i < array.length ; i++){
            index = 0;
            for(int j = 1 ;j <= array.length-i ; j++){
                if(array[j] > array[index]){
                    index = j ;
                }
            }
        //The two numbers on the swap position array.length-i and index (maximum)
        int temp = array[array.length-i]; //Save the first element value to a temporary variable
        array[array.length-i] = array[index]; //Save the second element value to the first element unit
        array[index] = temp; //Save the temporary variable, that is, the original value of the first element, to the second element
        }
        showArray(array); //Output the sorted array value directly
    }
    /**
    * Display all elements in the array
    * @param array The array to display
    */
    public void showArray(int[] array){
        for(int i : array){ //traverse the array
            System.out.print(">"+i); //Output the value of each array element
        }
        System.out.println();
    }
}
    The results are: > 1 > 3 > 4 > 15 > 24 > 63

    1.3 Reverse sorting

    Reverse sort is to reorder the contents of the original array in reverse order. The reverse sort algorithm is also often used in program development.

    (1) Basic idea

    It is to replace the last element of the array with the first element, the second-to-last element with the second element, and so on, until all array elements are reversed and replaced.

    (2) Algorithm example

    Reverse sorting is to replace the elements on both sides of the array, so it only needs to loop half the length of the array. For example, if the length of the array is 7, then the for loop only needs to loop 3 times.

    eg :

Initial array resource [ 10 20 30 40 50 60 ]
60 [ 20 30 40 50 ] 10 after the first pass
60 50 [ 30 40 ] 20 10 after the second run
After the third round of sorting 60 50 40 30 20 10

    (3) Algorithm implementation

    Implement reverse sorting.

/**
* Example of reverse sorting algorithm
*/
public class ReverseSort{
    public static void main(String[] args){
        int[] array = {10 ,20 ,30 ,40, 50,60}; //Create an array
        ReverseSort sorter = new ReverseSort(); //Create an object of the reverse sort class
        sorter.sort(array); //Call the method of the sort object to reverse the array
    }
    /**
    *Direct selection sort
    *@param array The array to sort
    */
    public void sort(int[] array){
        System.out.println("The original content of the array:");
        showArray(array); //Output the array value before sorting
        int temp ;
        int len = array.length;
        for(int i = 0 ; i < len/2 ; i ++){
            temp = array[i];
            arrray[i] = array[len-1-i];
            array[len-1-i] = temp;
        }
        System.out.println("The contents of the reversed array: ");
        showArray(array); //Output the sorted array value
   }
    /**
     * display all elements in the array
    *@param array array to display
    */
    public void showArray(int[] array){
        for(int i : array){ //traverse the array
            System.out.printl("\t"+i); //Output the value of each array element
        }  
    System.out.println();
    }
}

    The running result is:

    The original content of the array: 10 20 30 40 50 60

    The contents of the reversed array: 60 50 40 30 20 10



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631995&siteId=291194637