Common application algorithms of Java basic 5-array

Posted by Cogen2 on Sun, 03 May 2020 01:33:51 +0200

Common algorithms

1. Bubble sorting:

Principle: compare two adjacent elements and exchange the elements with large values to the right end

Example:

public static void bubbleSort(int[] a) {
    int n = a.length;
    //In total n-1 Comparison of wheels
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < n - i; j++) {
            if (a[j] > a[j + 1]) {//exchange
                int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

2. Select Sorting:

Principle: select the smallest elements from the records to be sorted each time, and place the order at the end of the ordered sequence until all records are sorted.

Example:

public static void selectionSort(int[] a) {        
    for(int i = 0; i < a.length - 1; i++) {// Do the first i Rank order
        int m = i;
        for(int j = i + 1; j < a.length; j++){// Select the smallest record
            if(a[j] < a[m]){
                m = j; //Note the location of the currently found minimum
            }
        }
        if(i != m){  //exchange a[i]and a[m]
            int temp = a[i];
            a[i] = a[m];
            a[m] = temp;
        }
    }    
}

3. Insert sort:

Principle: start from the first element a[0] of the array, insert the next element a[1] in front of or behind a[0], and then continue the process. Insert a[i] into the sorted a[0]~a[i-1] every time

Example:

public static void insertSort(int[] a) {
    for (int i = 1; i < a.length; i++) {
        for (int j = i; j > 0; j--) {
            if (a[j] < a[j - 1]) {
                int temp = a[j];
                a[j] = a[j - 1];
                a[j - 1] = temp;
            } else {
                break;
            }
        }
    }
}

4. Quick sorting:

  • Fast sorting is an improvement of bubble sorting
  • Idea: find the proper pivot in the array, then divide the array into two parts, and sort the left and right arrays respectively

Two point search

  • Precondition: find in sorted array
  • The basic idea of binary search is:

First, the location of the middle point of the search interval is determined: int mid = (low+upper) / 2;

– then compare the value to be found with the value at the middle point:

    • If equal, the lookup succeeds and returns to this location.
    • If the location value of the middle point is greater than the value to be queried, the new search interval is the left area of the location of the middle point.
    • If the location value of the middle point is less than the value to be queried, the new search interval is the right area of the location of the middle point. The next search is for a new search interval.

Example:

public static int binarySearch(int[] a, int num) {
    int low = 0;                                  // Starting point
    int upper = a.length - 1;              // End
    while (low <= upper) {
        int mid = (low + upper) / 2;     // Middle point
        if (a[mid] < num) {                  // The value of the middle point is less than the value to find
            low = mid + 1;                    // Change the starting point of the search to the position next to the middle point
        } else if (a[mid] > num) {        // The value of the middle point is greater than the value to find
            upper = mid - 1;                 // Change the end point of the search to the position before the middle point
        } else {                                   // The value of the middle point is equal to the value to find
            return mid;                         // Return to this location
        }
    }
    return -1;
}            

Array help class Arrays

  • java.util.Arrays array manipulation tool
public static void sort(int[] a);  //Optimized quick sorting method
public static int binarySearch(int[] a, int key); //Using binary search

Two dimensional array

  • Java does not have a real multi-dimensional array. A two-dimensional array can be regarded as an array of elements. Such as:
int [][] a = { {1}, {4,5,6}, {7,8}};

Topics: Java less