Finding and sorting data structures

1. Find

1.1 Linear table lookup

(1) Sequential search

public class SearchDemo1 {
    
    
    public static void main(String[] args) {
    
    
//        给定分数数组
        int[] scoreArr = {
    
    89, 45, 78, 45, 100, 98, 86, 100, 65};
//        给定要查找的分数
        int score = 100;
//        完成查找
        int index = -1;
        for (int i = 0; i < scoreArr.length; i++) {
    
    
            if (scoreArr[i] == score){
    
    
                index = i;
                break;
            } }
//        输出结果
        if (index == -1) {
    
    
            System.out.println("该分数不存在");
        }else {
    
    
            System.out.println(score+"的索引是"+index);
        } } }

(2) Search by half: sequential structure, ordered by keyword
① Non-recursive

public class SearchDemo2 {
    
    
    public static void main(String[] args) {
    
    
//        给定数组
        int[] arr = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//给定要查找的值
        int key = 9;
//        进行折半二分查找
        int index = binarySearch(arr, key);
        //        输出结果
        if (index == -1) {
    
    
            System.out.println("该分数不存在");
        } else {
    
    
            System.out.println(key + "的索引是" + index);
        } }
    //    不使用递归
    public static int binarySearch(int[] array, int key) {
    
    
//        指定low和high
        int low = 0;
        int high = array.length - 1;
//        折半查找
        while (low <= high) {
    
    
//            求mid
            int mid = (low + high) / 2;
//            判断是否等于
            if (key == array[mid]) {
    
    
                return mid;
            } else if (key < array[mid]) {
    
    
                high = mid - 1;
            } else {
    
    
                low = mid + 1;
            }
        }
            return -1;
    } }

②Recursion

public class SearchDemo3 {
    
    
    public static void main(String[] args) {
    
    
//        给定数组
        int[] arr = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//给定要查找的值
        int key = 10;
//        进行折半二分查找
        int index = binarySearch(arr, key);
        //        输出结果
        if (index == -1) {
    
    
            System.out.println("该分数不存在");
        } else {
    
    
            System.out.println(key + "的索引是" + index);
        }
    }
    //   使用递归
    public static int binarySearch(int[] array, int key) {
    
    
//指定low和high
        int low = 0;
        int high = array.length - 1;
        return binarySearch(array, key, low, high);
    }
    public static int binarySearch(int[] array, int key, int low, int high) {
    
    
//  递归结束的条件
        if (low > high) {
    
    
            return -1;
        }
        int mid = (low + high) / 2;
        if (key == array[mid]) {
    
    
            return mid;
        } else if (key < array[mid]) {
    
    
            return binarySearch(array, key, low, mid - 1);
        } else {
    
    
            return binarySearch(array, key, mid + 1, high);
        } } }

1.2 Search tree

1.2.1 Binary search/search/sort tree BST

(1) Or an empty tree
(2) Or a binary tree with the following properties:
① If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node
② If the value of all nodes on its right subtree is greater than the value of its root node
③ its left and right subtrees are also binary sorted trees respectively

insert image description hereNote: Perform in-order traversal on the binary search tree to get an ordered set

1.2.2 Balanced Binary Tree

Self-balancing binary search tree, also known as AVL tree (different from AVL algorithm)
(1) It is an empty tree
(2) or the absolute value of the height difference (balance factor) of its left and right subtrees is not More than 1,
and the left and right subtrees are both a balanced binary tree. At the
same time, the balanced binary tree must be a binary search tree, and vice versa.
① Balance factor: The balance factor of a node is the height of the node's left subtree minus the height Height of the right subtree
② Balanced binary tree: A binary sorted tree in which the balance factor of each node is 1, -1, and 0. In other words, the height of the left and right subtrees of each node is at
most 1 binary sorted tree.
(3) The purpose of binary tree is to reduce the level of binary search tree and improve the search speed
. Common implementation methods of balanced binary tree include AVL, red-black tree, scapegoat tree, Treap, and stretch tree.

1.2.3 Red-Black Trees

RB Tree, the full name is Red-Black Tree, also known as "red-black tree", it is a balanced binary tree. Each node of the red-black tree has a storage bit to indicate the color of the node, which can be red or black.
Characteristics of a red-black tree:
(1) Each node is either black or red.
(2) The root node is black
(3) Each leaf node is black (note: the leaf node here refers to an empty leaf node)
(4) If a node is red, its child nodes Must be black.
(5) All paths from a node to descendant nodes of this node contain the same number of black nodes.
insert image description here
The red-black tree is widely used, and it is mainly used to store ordered data. Its time complexity is O(logN), and the efficiency is very high.
For example, TreeSet and TreeMap in Java collections

1.2.4 B-tree (balanced tree)

insert image description here
insert image description here

1.2.5 B+ tree

(1) All data are in the bottom layer
(2) On the basis of B-tree (ie B-tree), add linked list pointers to leaf nodes, all keywords appear in leaf nodes, non-leaf nodes As the index of the leaf node; the B+ tree always hits the leaf node.

insert image description here

1.2.6 B*trees

insert image description here

1.3 Hash table lookup

1.3.1 The structure and characteristics of the hash table

(1) hash table (hash table) is also called hash table
(2) features: fast
(3) there are many structures,
the most popular and easiest to understand is: sequence table (main structure) + linked list
(4) main structure : Sequence table
(5) Each node of the sequence table leads to a separate linked list
insert image description here

1.3.2 How does the hash table add data

(1) Calculate the hash code (call hashCode(), the result is an int value, and the hash code of the integer can be taken by itself)
(2) Calculate the storage location in the hash table (calculate the hash value according to the hashcode)
hashcode is an integer, we need to convert it to the range [0, array length-1]. We require the converted hash values ​​to be distributed as evenly as possible in the interval [0, array length-1] to reduce "hash conflicts"
① An extremely simple and low-level algorithm is: hash value = hashcode/hashcode;
that is, The hash value is always 1. It means that the key-value pair objects will be stored in the array index 1 position, thus forming a very long linked list. It is equivalent to a "hash conflict" every time an object is stored, and HashMap also degenerates into a "linked list".
②A simple and commonly used algorithm is (division and remainder algorithm): hash value = hashcode% array length
This algorithm can make the hash value evenly distributed in the interval of [0, array length-1]. The early HashTable used this algorithm. However, this algorithm is inefficient due to the use of "division". The JDK later improved the algorithm. First of all, it is agreed that the length of the array must be an integer power of 2, so that the effect of the remainder can be achieved by using the bit operation: hash value = hashcode&(array length-1).
(3) Store in the hash table:
Case 1: One-time addition is successful
Case 2: Multiple additions are successful (a conflict occurs [the storage location of the hash table is the same], call equals() to compare with the elements of the corresponding linked list, and compare to Finally, the results are all false,
create a new node, store the data, and add it to the end of the linked list)
Case 3: No addition (if a conflict occurs, call equals() to compare with the elements of the corresponding linked list; after one or more comparisons, the result are all true, indicating repetition,
Do not add)
Conclusion 1: Hash table adds data quickly (3 steps, regardless of conflict)
Conclusion 2: Unique
Conclusion 3: Disordered

insert image description here

1.3.3 How Hash Tables Query Data

The process of adding data is the same.
Conclusion 1: Hash table query data is fast.
Conclusion 2: Hash table deletes data quickly
. , to be deleted and added again)

1.3.4 The role of hashCode and equals

(1) hashCode(): It is used to calculate the hash code, which is an integer. According to the hash code, the storage location of the data in the hash table can be calculated.
(2) equals(): There is a conflict when adding, and it needs to be compared by equals to determine whether it is the same.
The query also needs to be compared using equals to determine whether it is equal.

1.3.5 How to obtain hash codes of various types of data

(1)Integer

// int就是取自身
public static int hashCode(int value) {
    
    
        return value;
    }

(2)Double

  public static int hashCode(double value) {
    
    
        long bits = doubleToLongBits(value);
        return (int)(bits ^ (bits >>> 32));
    }

(3)String

 public int hashCode() {
    
    
        int h = hash;
        if (h == 0 && value.length > 0) {
    
    
            char val[] = value;
            for (int i = 0; i < value.length; i++) {
    
    
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

2. Sort

2.1 Introduction to Sorting

(1) What is sorting
? The function of sorting is to rearrange an arbitrary sequence of data elements into a sequence ordered by key
(2) Internal sorting and external sorting
① The entire sorting process is performed in internal memory, which is called internal Sorting
② The number of elements to be sorted is so large that the internal memory cannot hold all the data, and the sorting needs to be done with the help of an external storage device. This kind of
sorting is called external sorting.
(3) Stable sorting and unstable sorting
① Assuming Ri=Rj, if Ri is before Rj before sorting, and Ri is still before Rj after sorting, then the sorting method used is said to be stable.
②Otherwise, when the context of the same keyword elements changes in the sorting, the sorting method used is said to be unstable
(in some occasions, there may be stability requirements for sorting, for example, when students are sorted by grades and the scores are the same The smaller student number is required first)

2.2 Quick Sort

Quick sort adopts a divide-and-conquer strategy, usually called the divide-and-conquer method. The basic idea of ​​this method is:
(1) First, take a number from the sequence as the reference number (the first number can be taken for simplicity)
( 2) Partitioning process, put all numbers larger than this number to its right, and all numbers less than or equal to it to its left (partition)
(3) Repeat the first and second steps for the left and right intervals, until each interval has only one number (recursion)

Supongo que te gusta

Origin blog.csdn.net/y516369/article/details/126628146
Recomendado
Clasificación