Java Basics (12) - sorting and finding

Posted by CanWeb on Thu, 03 Feb 2022 00:26:58 +0100

catalogue

1, Bubble sorting

thought

code implementation

example

2, Select sort

Sorting thought

code implementation

Example: sort the array {21,54,21,2,4,87,24,82,4,5}

3, Quick sort

Sorting thought

code implementation

Instance to quickly sort the array {21,54,21,2,4,87,24,82,4,5}

4, Binary search

Algorithmic thought

code implementation

Example: find the element 5 in the array {2, 2, 2, 4, 5, 21, 24, 54, 82, 87} using the binary search algorithm.

5, Basic search

Algorithmic thought

code implementation

Example: use the basic lookup to find the element 54 in the array {2, 2, 2, 4, 5, 21, 24, 54, 82, 87}

1, Bubble sorting

  1. thought

    The idea of bubble sorting is to cycle through the array, compare adjacent elements in pairs, and move large elements backward. Therefore, the first cycle determines the element at the maximum index (the maximum value), the second cycle determines the penultimate element, and so on.

    After observation, it is found that the length of the array is - 1 times to complete the bubble sorting, and the number of times to compare each time is - 1.

  2. code implementation

    package utils.sort;
    ​
    public class BubbleSortUtils {
        public static void bubbleSort(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = 1; j < arr.length - i; j++) {
                    if (arr[j-1]>arr[j]){
                        int t=arr[j-1];
                        arr[j-1]=arr[j];
                        arr[j]=t;
                    }
                }
            }
        }
    }

  3. example

    Bubble sort the integer array {21, 54, 65, 21, 8, 93, 2, 32, 20, 58, 92}.

    import utils.sort.BubbleSortUtils;
    ​
    import java.util.Arrays;
    ​
    public class Test {
        public static void main(String[] args) {
            int[] arr={21,54,65,21,8,93,2,32,20,58,92};
            BubbleSortUtils.bubbleSort(arr);
            System.out.println(Arrays.toString(arr));
        }
    }

2, Select sort

  1. Sorting thought

    1. Start from the 0 index and compare with the following elements in turn. The small elements are put forward. After the first sorting, the minimum value appears at the minimum index;
    2. In the first comparison, the array length is compared - 1 times from the 0 index. In the second sorting, the array length is compared - 2 times from the 1 index. In the third sorting, the array length is compared - 3 times from the 2 index, and so on until the sorting is completed.
    3. The array length is compared - 1 times in total.
  2. code implementation

    package org.util;
    ​
    public class SelectSortUtils {
        public static void SelectSort(int[] arr){
            for (int i = 0; i < arr.length; i++) {
                int a=i;
                for (int j = i; j < arr.length; j++) {
                    if(arr[a]>arr[j]){
                        a=j;
                    }
                }
                int t=arr[a];
                arr[a]=arr[i];
                arr[i]=t;
            }
        }
    }

  3. Example: sort the array {21,54,21,2,4,87,24,82,4,5}

    import org.util.SelectSortUtils;
    ​
    import java.util.Arrays;
    ​
    public class Test {
        public static void main(String[] args) {
            int[] arr={21,54,21,2,4,87,24,82,4,5};
            SelectSortUtils.SelectSort(arr);
            System.out.println(Arrays.toString(arr));
        }
    }

3, Quick sort

  1. Sorting thought

    Pit filling method

    1. Dig out the reference number to form the first pit;
    2. Find the number smaller than him from the back to the front, dig out the number and fill it into the previous pit;
    3. Find the number larger than or equal to him from front to back. After finding it, dig out the number and fill it into the previous pit;
    Repeat steps 2 and 3.
  2. code implementation

    package org.util;
    ​
    public class QuickSortUtils {
        public static void quickSort(int[] arr, int start, int end) {
            if (start < end) {
                int index = getIndex(arr, start, end);
                quickSort(arr, start, index - 1);
                quickSort(arr, index + 1, end);
            }
        }
        public static int getIndex(int[] arr, int start, int end) {
            int i = start;
            int j = end;
            int x = arr[i];
            if (i < j) {
                while (i < j) {
                    while (i < j && arr[j] >= x)
                        j--;
                    if (arr[j] < x) {
                        arr[i] = arr[j];
                        i++;
                    }
                    while (i < j && arr[i] < x)
                        i++;
                    if (arr[i] > x) {
                        arr[j] = arr[i];
                        j--;
                    }
                }
            }
            arr[i] = x;
            return i;
        }
    }

  3. Instance to quickly sort the array {21,54,21,2,4,87,24,82,4,5}

    import org.util.QuickSortUtils;
    ​
    import java.util.Arrays;
    ​
    public class Test {
        public static void main(String[] args) {
            int[] arr={21,54,21,2,4,87,24,82,4,5};
            QuickSortUtils.quickSort(arr,0,arr.length-1);
            System.out.println(Arrays.toString(arr));
        }
    }

4, Binary search

  1. Algorithmic thought

    The premise is that the array elements must be ordered.

    The idea of the algorithm: find the element of the intermediate index every time, and compare the element whose size can be reduced by half.

  2. code implementation

    package org.locate;
    
    public class BinaryLocate {
        public static int locate(int[] arr, int x) {
            return binaryLocate(arr, 0, arr.length - 1, x);
        }
    
        public static int binaryLocate(int[] arr, int start, int end, int x) {
            int centerIndex = (start + end) / 2;
            while (start<=end) {
                if (arr[centerIndex] == x) {
                     return centerIndex;
                } else if (arr[centerIndex] > x) {
                     end=centerIndex-1;
                } else {
                     start=centerIndex+1;
                }
                centerIndex=(start+end)/2;
            }
            return -1;
        }
    }

  3. Example: find the element 5 in the array {2, 2, 2, 4, 5, 21, 24, 54, 82, 87} using the binary search algorithm.

    import org.locate.BinaryLocate;
    
    public class Test2 {
        public static void main(String[] args) {
            int[] arr={2, 2, 2, 4, 5, 21, 24, 54, 82, 87};
            System.out.println(BinaryLocate.locate(arr, 21));
        }
    }

5, Basic search

  1. Algorithmic thought

    Use the loop to traverse the array, compare each item of the array element with the checked element, and find the element to be searched.

  2. code implementation

    package org.locate;
    
    public class PrimaryLocate {
        public static int primaryLocate(int[] arr,int ele){
            for (int i = 0; i < arr.length - 1; i++) {
                if(ele==arr[i]){
                    return i;
                }
            }
            return -1;
        }
    }

  3. Example: use the basic lookup to find the element 54 in the array {2, 2, 2, 4, 5, 21, 24, 54, 82, 87}

    import org.locate.BinaryLocate;
    import org.locate.PrimaryLocate;
    
    public class Test2 {
        public static void main(String[] args) {
            int[] arr={2, 2, 2, 4, 5, 21, 24, 54, 82, 87};
            System.out.println(PrimaryLocate.primaryLocate(arr,54));
        }
    }

Topics: Java Algorithm