Javase (day10: a simple algorithm for sorting and finding packages and arrays)

1. Package

  • Why do I need a bag
    1. Managing classes through packages is similar to managing blockbusters through folders.
    2. The emergence of package provides a multi-layer namespace for classes, that is, the complete class name is "package name + class name".
    Note: in different packages, we can define classes with the same name; However, in the same package, we cannot define a class with the same name.
  • How to define packages
    1. Rules: the naming rules of "identifier" must be followed.
    2. Specification: it must comply with "all letters are in lowercase, with". "Between multiple words" Link, and do the naming standard of "top-level domain name written upside down".
  • How to use packages
    In the first line of valid code in the source file, we use the package keyword to declare which package the class in the current source file is in
    Note: when using IDEA to run a program, there must be a package declaration; However, if you use DOS commands to run the program, you must remove the package declaration
  • Recognize common packages
    1,java.lang contains some core classes of Java language, such as String, Math, System, etc.
    2,java.awt contains several classes that make up the abstract window toolkits, which are used to build and manage the graphical user interface (GUI) of the application.
    3,java.net contains classes that perform network related operations.
    4,java.io contains classes that provide a variety of input / output functions.
    5,java.util contains some utility classes, such as defining system features and using functions related to date and calendar.
  • Access method of class
    1. First: simplified access
    When we need to access the class in "current package" or "java.lang", we can simplify the access directly through "class name"
    2. Second: access with package name
    When we need to access classes outside the "current package" (excluding classes in the java.lang package), we should use the "package name" method to access
    For example: Java util. Scanner input = new java. util. Scanner(System.in);

2. import keyword

When we need to access classes other than the "current package" (excluding classes in java.lang package), if we use the "package name" method to access this class, the operation code is very complex,
If you want to simplify access through "class name", you must import the class through the import keyword, and then simplify access through the class name.

  • Precautions for import keyword
    1. When we need to import more than one class in a package, we can use the wildcard *

    Note: during development, it is not recommended to use wildcards to import all classes in a package, because this method is very inefficient
    2. When we need to use the "class with the same name" in "different packages", one of the classes must be accessed by "with package name".
    3,JDK1. After 5, "static import" is also added, that is, it can import "static attributes" and "static methods" in a class
    (not recommended in development, which will lead to poor reading)

3. Bubble sorting

// Before optimization, the sorting process may be completed in advance. At this time, there is no need to continue to compare and exchange public class BubbleSort{
    public static void main(String[] args) {
        int[] arr = {5, 2, 6, 7, 1, 4, 3};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

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

// public class BubbleSort after optimization{
    public static void main(String[] args) {
        int[] arr = {5, 2, 6, 7, 1, 4, 3};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            // Before each sorting, it is assumed that the sorting has been completed
            boolean flag = true;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    // There was an exchange, so the hypothesis was overturned
                    flag = false;
                }
            }
            if (flag)
                break;
        }
    }
}

4. Select sort

public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {5, 2, 6, 7, 1, 4, 3};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int min = i;
            for (int j = i; j <= arr.length - 1; j++) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
            }
        }
    }
}

5. Dichotomy search

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        int index = binarySearch(arr, 5);
        System.out.println(index);
    }

    public static int binarySearch(int[] arr, int element) {
        int min = 0;
        int max = arr.length - 1;
        int mid = 0;
        while (true) {
            mid = (min + max) / 2;
            if (element > arr[mid]) {
                min = mid + 1;
            }else if (element < arr[mid]) {
                max = mid - 1;
            }else {
                return mid;
            }

            if (min > max) {
                // Returns a negative number indicating that the element was not found
                return -1;
            }
        }
    }

}

Keywords: Java

Added by Tentious on Tue, 08 Mar 2022 02:28:11 +0200