Introduction to Data Structures and Algorithms

A Guide to Data Structures and Algorithms

program = data structure + algorithm

Know the data structure

  • What are data structures ?

    Data structure is a discipline that studies data storage and data manipulation in computers .

  • Why learn data structures ?

    Learning data structures allows us to write better code. When we are familiar with common data structures, we can choose a more reasonable data structure when we write programs, making our code more efficient and concise, and saving a lot of time and memory.

  • The composition of the data structure : 数据的逻辑结构+ 数据的存储结构+数据的运算

    • Logical structure of data : refers to observing and analyzing data from the logical point of view of data,The essence is the relationship between data

      There are four main types: set , linear structure , tree structure , graph structure

      logical structure of data features example
      set structure The relationship between elements is loose, and there is only a relationship between elements whether they belong to the same set
      linear structure There is a one-to-one relationship between elements
      tree structure There is a one-to-many relationship between elements
      graphic structure There is a many-to-many relationship between elements

      These four types are divided into two categories, namely: linear structure and nonlinear structure. Common linear structures include: arrays, queues, linked lists, and stacks; non-linear structures include: two-dimensional arrays, multidimensional arrays, generalized tables, tree structures, and graph structures

    • Data storage structure : It is the concrete realization of the logical structure of the data in the computer. We also call it the physical structure of the data.The essence is the way of data storage

      There are four main types: sequential storage , link storage , index storage and hash storage

      data storage structure describe Implementation
      sequential storage The storage method of storing data in storage units with continuous addresses, the logical relationship and physical relationship are consistent array
      link store Use a group of arbitrary storage units to store the data elements of the linear table (this group of storage units can be continuous or discontinuous), and use pointers or references to determine the logical relationship linked list
      index storage Separately store data elements and storage methods between elements direction chart
      hash storage Also known as hash storage, it is a storage method that tries to establish a definite correspondence between the storage location of the data element and the key code Hash table
    • Data operation : refers to the operation of the data structure (mainly for the elements stored in the data structure),The essence is the operation of data

      Common data operations include:

      1. 创建(create): 创建一个空的数据结构
      2. 清除(clear): 删除数据结构中的所有数据元素
      3. 插入(insert): 在数据结构指定的位置插入一个元素
      4. 删除(remove):将数据结构中的某个元素删除
      5. 搜索(search):在数据结构中搜索满足特定条件的数据元素
      6. 更新(update):修改数据结构中的某个数据元素的值
      7. 访问(visit):访问数据结构中的某个数据元素
      8. 遍历(traverse):按照某种次序访问数据结构中的每一数据元素,是每个数据元素恰好被访问一次
      注:每种数据结构还可包含一些特定的计算
      1. 树状结构中,找某个元素的儿子/父亲的操作
      2. 线形结构中,找某个元素的前驱或后继
      
  • Basic concepts in data structures :

    • Data element : It is the basic unit that makes up data and has a certain meaning, also known as record or node
    • Data item : It is the basic unit that makes up the data element, and it is the smallest data unit that cannot be further divided. A data element can consist of several data items

    image-20220812105931025

    The data element is the basic unit of access and processing

    My understanding of data elements and data items:

    Data elements are similar to objects in programming, and data items are the attributes of objects. When we program, we usually first determine the object, and then manipulate the attributes through the object.


Recognize Algorithms

  • What is an algorithm ?

    Official definition: An algorithm is an accurate and complete description of the steps to solve a characteristic problem.

    My understanding: Algorithm is a calculation method in short, it is a method, a strategy, we can use this method to get the result we want, when we encounter a mathematical problem to find the root number 2 with six decimal places When the approximate solution is obtained, an algorithm such as the dichotomy method can be used to solve it.

    (This understanding is biased, the calculation method ≠ the calculation method, and sometimes the algorithm does not necessarily need to be calculated)

  • Features of the algorithm :

    • Input : An algorithm can accept zero or more input arguments
    • Output : An algorithm has at least one or more outputs
    • Deterministic : Each step of the algorithm must be determined without ambiguity. For the same type of input, there can only be one execution path, and only the same result can be obtained.
    • Finiteness : The execution steps of the algorithm are limited, and each step can be executed within an acceptable time range
    • Feasibility : the algorithm can be implemented using code, run on the machine and finally get the correct result

A simple algorithm example:

Find the sum of all integers between 1 and n.

Method 1: Violent solution

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i=0;i<n;i++){
    
    
  sum += i;
}
System.out.pirntln(sum);

Method 2: Formula method (Gaussian summation algorithm)

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
sum = (1+n)*n/2;
System.out.pirntln(sum);

Obviously, Algorithm 2 is better than Algorithm 1! So how to judge the pros and cons of these two algorithms? This requires performance analysis of the algorithm

  • Algorithm performance analysis : Usually the space complexity and time complexity of the algorithm are used to evaluate the pros and cons of an algorithm (generally speaking, we will pay more attention to the time complexity of the algorithm)

    • Space complexity : the computer memory occupied by the program written according to the algorithm

    • Time complexity : the total time it takes for the program written according to the algorithm to execute to get the correct result

      Time complexity analysis :

      • Post hoc analysis : Write a code that tests the running time of the program to test the running time of the algorithm program

        For example:

        long start = System.currentTimeMillis();
        /*-------------待测试的代码---------------*/
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        sum = (1+n)*n/2;
        System.out.pirntln(sum);
        /*--------------------------------------*/
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        

        Pros: intuitive, simple

        Disadvantage: It cannot accurately reflect the time complexity of the algorithm, and the test will be affected by the hardware environment

      • Pre-analysis method : the algorithm can be analyzed according to the following main factors

        1) The quality of the machine language code produced by compilation (compiler-dependent, difficult to intervene)

        2) The speed at which the machine executes instructions (related to the machine hardware, difficult to intervene)

        3) The input scale of the problem (human intervention is possible)

        4) Algorithm strategy (human intervention is possible)

        According to 3) and 4) a T ( n ) = O ( f ( n ) ) T(n) = O(f(n)) can be constructedT(n)=O ( f ( n )) function to analyze the time complexity of the algorithm (where n represents the number of times the algorithm is executed)

Summarize

  • The relationship between the logical structure and physical structure of data : the logical structure of data is a larger concept than the storage structure. It focuses on the relationship between data elements and does not care about the storage method of data elements, which is very abstract; and The storage structure focuses on the storage method of data, which is a concrete thing. Depending on the storage structure, different relationships between elements can be realized. Therefore, a logical structure of data can be realized by various storage structures. For example, a linear structure can be realized by Sequential storage, linked storage implementation.

    To give an inappropriate example: when building a house, the architect draws a sketch to describe the structure between the various parts of the house. This sketch is equivalent to the logical structure of the house; and the contractor chooses what materials to use to realize it. The process is the storage structure of the house.

  • The relationship between algorithms and data structures , in my opinion, data structures are a tool to make algorithms better. Data structure is a way of storing data, which is realized by the data storage structure; while algorithm is a method to solve problems, which is realized by code; when we implement the algorithm, we need to choose a suitable data structure to make the code take up less more memory, less time.

Data Structure Classification


Recommended reading:


  1. The logical point of view refers to an observation method that only focuses on the relationship between data and ignores other secondary factors. For example: From a logical point of view, the relationship between men and women may be the relationship between husband and wife, siblings, and siblings. It pays more attention to relationships than other secondary factors, such as age, height... ↩︎

Guess you like

Origin blog.csdn.net/qq_66345100/article/details/130514414