Data structure and algorithm--basic knowledge __java

Source: Internet
Author: User

The data structure is essential for every programmer, and it is also the most important job interview for the major companies. And I am also a beginner to learn the Java version of the data structure, therefore, I am prepared to study in depth, and use code to implement a variety of data structures, I hope that this series of blogs I wrote to provide the same data structure and I do not know how to help beginners, if there is a mistake, I hope that everyone to put forward, I would appreciate it. first, the concept of data structure and algorithm

The data structure is to study how to store the real data in the computer, it can make the data have a certain logical relationship, so that people can access and process the information effectively. In the data structure, there are 4 kinds of storage structures, namely sequential structure, chain structure, hash structure and index structure.
  
The algorithm is the method of processing data, and also the method and idea of solving the problem by the programmer, the efficiency of the algorithm determines the processing speed of the data, and also indicates the programmer's ability, so the algorithm is very important for every programmer. I think the most important thing about programming is the ability to solve problems, that is, thinking and methods, and then is the process of coding, coding in fact, is the reality of things into the form of code, can exercise, so we should pay attention to train their logical thinking ability, personal opinion, do not like to spray.
  
The algorithm should have 5 major characteristics, presumably we all know:
(1) Poor: An algorithm must be executed after the completion of a certain step, otherwise the program dead loop.
(2) Certainty: Each step in the algorithm must have a definite meaning. The computer is a machine, and the program is to tell it how to go, otherwise it will lose direction, not as smart as humans, haha.
(3) Feasibility: Each step in the algorithm must be feasible, that is, each step can be completed in a limited time, rather than unlimited implementation, otherwise meaningless.
(4) Input: An algorithm can have 0 or more inputs.
(5) Output: An algorithm has at least one output. If there is no output the algorithm has a fur use. second, the evaluation of the algorithm

Algorithm evaluation is to evaluate the quality of the algorithm. There are usually a lot of different ways of dealing with one thing, and we definitely choose the best solution. For example, to Beijing, people from different regions will have different ways to travel, for Tianjin, may be the fastest way to reach Beijing, the high-speed train may spend a lot of waiting time. For Shandong may be the fastest way for high-speed rail, for Hainan may be the fastest way to aircraft. Therefore, when we write code, we must consider the pros and cons of the algorithm. Usually, we use 5 metrics to evaluate the algorithm, namely, correctness, robustness, readability, time complexity and space complexity. The previous three indicators should be understandable, the following focus on the complexity of time and space to explain the complexity.
   1. Complexity of Time:

Time complexity represents the time at which the algorithm runs from start to finish, the time complexity of a simple operation, such as subtraction, assignment, and so on, is usually the same for the program to perform a simple operation, which is usually represented by an O order: the order of magnitude. Time complexity usually has O (1 1), O (n N), O (log2n log_{2}n), O (nlog2n nlog_{2}n), O (N2 n^2), O (N3 n^3), O (2n 2^n), O (n! n!) In the form of the highest Chishi. Here are a few common examples to see how time complexity of the algorithm. where O (1 1) indicates that the time run is fixed, typically a simple assignment statement and an output statement, that is, the run-time time is constant. The time complexity of a circular statement is usually O (n n), O (N2 n^2), O (N3 n^3). Examples are as follows:

int n = m;
int count = 0;
for (int i = 0; i < n; i++) {
    count++
}

The algorithm is n+2, takes the highest power, and the complexity is O (n N).

int n = m;
int count = 0;
for (int i = 0; i < n; i++) {for
    (int j = 0; J < N; j +) {
        count++
    }
}

The algorithm is 2 for the loop, the number of operations is N2 n^2+2, the time complexity of O (N2 n^2) Complexity of O (log2n log_{2}n) of the classical algorithm for two-point search.

Int[] array = {1,2,3,4,5,6,7,8};    Defines an array with lookups, which must be ordered
int k = ARRAY.LENGTH/2;//define index
int dest = 5 for first lookup array;   Defines the value that needs to be looked up, where we only consider the existence of the dest presence in the array while
(true) {    //Dead Loop
    if (array[k]==dest) {
        System.out.println (dest);
        break;  Jump out
    of circulation} if
    (array[k]>dest) {  //if dest is less than array[k],
        k = K/2;
    }
    if (array[k]<dest) {
        k = (k+array.length)/2;
    }
}
2. Space complexity

Space complexity is usually a measure of the amount of storage space that is temporarily occupied by an algorithm during its operation. The storage space occupied by an algorithm on the computer memory, including the storage space occupied by the storage algorithm itself, the storage space occupied by the input and output data of the algorithm and the storage space temporarily occupied by the algorithm in the process of operation. THREE aspects. In most cases, the space complexity of the algorithm is expressed as temporary storage space, and the complexity is usually O (1 1).

int a = ten;
int b =;
int temp = A;   
A = b;
b = temp;

The above algorithm is to realize the exchange of two variables and define temp as temporary storage space, so the spatial complexity of the algorithm is O (1 1). Third, summary

Understanding the basic concepts is a necessary condition for deep learning of data structure reading. In addition, in order to write an algorithm, we need to combine time complexity and space complexity to maximize its efficiency. Write this blog, I also in-depth understanding of time complexity, and then will truly implement the data structure of the concrete operation. Please pay attention.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.