Java Data structures and algorithms (i)--Introduction

Source: Internet
Author: User

This series of blogs we will learn about data structures and algorithms, why to learn data structures and algorithms, here I give a simple example.

Programming is like a car, and the data structure and algorithm is the gearbox inside the car. A driver who doesn't understand the principle of transmission is also able to drive, the same as a person who does not understand the data structure and algorithms can also be programmed. But if a driver understands the mechanics of the gearbox, such as reducing speed to get more traction, or by reducing traction to get faster travel speed. When climbing, the use of 1 gear, you can get greater traction, downhill when the use of low-end car to limit the speed of travel. Back to programming, for example, a class student name to be temporarily stored in memory, you will choose what data structure to store, array or ArrayList, or hashset, or other data structure. If you do not understand the data structure, may choose a container to store, but also to complete all the functions, but later, if with the increase in student data, random choice of the data structure will certainly have a performance problem, and a person who understands the data structure and algorithm, in the actual programming will choose the appropriate data structure to solve the corresponding problem, Will greatly improve the performance of the program.

1. Data structure

  A data structure is a way of storing and organizing data in a computer, which refers to a collection of elements of one or more specific relationships that exist between each other.

Typically, a well-chosen data structure can lead to higher operational or storage efficiency. Data structures are often associated with efficient retrieval algorithms and indexing techniques.

I. Basic functions of data structure

  ①, how to insert a new data item

②, how to find a particular data item

③, how to delete a specific data item

④, how to iterate over each item of data for display or other action

Second, the commonly used data structure

The advantages and disadvantages of these structures are as follows: first, a general impression, the following will be explained in detail!!!

  

2. Algorithm

  The algorithm is simply the step to solve the problem.

In Java, algorithms are usually implemented by methods of classes. The previous data structure, such as why the linked list is inserted, deleted fast, and the lookup is slow, balanced two-tree insert, delete, find all fast, this is the implementation of these data structures caused by the algorithm. The various sort implementations we talked about later are also important areas of the algorithm category.

One , five characteristics of the algorithm

有穷性:对于任意一组合法输入值,在执行又穷步骤之后一定能结束,即:算法中的每个步骤都能在有限时间内完成。

②, certainty: The actions to be performed in each case are defined in the algorithm so that the performer or reader of the algorithm can define its meaning and how it is executed. And under any conditions, the algorithm has only one execution path.

③,可行性:算法中的所有操作都必须足够基本,都可以通过已经实现的基本操作运算有限次实现之。

④, there is input: as an algorithm processing the magnitude of the object, usually reflected in the algorithm of a set of variables. Some inputs need to be entered during the execution of the algorithm, while some algorithms may not have input on the surface and are actually embedded in the algorithm.

⑤,有输出:它是一组与“输入”有确定关系的量值,是算法进行信息加工后得到的结果,这种确定关系即为算法功能。

  

Second, the design principles of the algorithm

  ①, correctness : First, the algorithm should meet the requirements given in a specific "rule description" approach. Secondly, the understanding of whether the algorithm is "correct" can have the following four levels:

First, the program syntax error.

Second, the program for several sets of input data can be obtained to meet the needs of the results.

Third, the procedure for carefully selected, typical, harsh cut with a number of difficult to enter a few sets of input data can be obtained to meet the requirements of the results.

Four, the procedure for all legitimate input data can be satisfied with the results of the requirements.

PS: Usually the third layer of meaning of correctness as a measure of an algorithm eligibility criteria.

  ②, readability : Algorithm for people's reading and communication, followed by computer implementation. Therefore, the algorithm should be easy to understand, on the other hand, the obscure program is easy to hide more errors and difficult to debug.

  ③, robustness : When the input data is illegal, the algorithm should react appropriately or handle it appropriately, rather than produce an inexplicable output result. Also, the method of handling the error should not be to interrupt the execution of the program, but should return a value that represents the nature of the error or error for processing at a higher level of abstraction.

  ④, high efficiency and low storage requirements : Usually the algorithm efficiency is worth the algorithm execution time; storage is the maximum amount of storage space required during the execution of the algorithm, both of which are related to the scale of the problem.

The previous three points of correctness, readability and robustness are all well understood. For the 4th algorithm execution efficiency and storage, we know the comparison algorithm, may say "a algorithm twice times faster than the B algorithm," and so on, but in fact, this statement does not make any sense. Because when the number of data items changes, the efficiency ratio of the A and B algorithms can also change, such as the data item increased by 50%, maybe a algorithm is three times times faster than the B algorithm, but if the data items are reduced by 50%, it is possible that a algorithm and B algorithm speed. So the speed of the description algorithm must be associated with the number of data items. That is, "Big O" notation, which is a relative representation of the complexity of the algorithm, here I briefly introduce, followed by a specific algorithm to describe.

Relative (relative): You can only compare the same things. You can't compare an arithmetic that does arithmetic multiplication with an algorithm that sorts an integer list. However, comparing the arithmetic operations of 2 algorithms (one doing multiplication, one doing addition) will tell you something meaningful;

Representation (representation): Large O (in its simplest form) simplifies the comparison between algorithms for a single variable. The choice of this variable is based on observations or assumptions. For example, the comparison between sorting algorithms is usually based on a comparison operation (comparing 2 nodes to determine the relative order of the 2 nodes). This assumes that the computational overhead of the comparison operation is significant. But what if the calculation overhead of the comparison operation is small and the calculation overhead of the interchange operation is significant? This changes the previous comparison mode;

Complexity (complexity): If it takes me 1 seconds to sort 10,000 elements, how long does it take to sort 1 million elements? In this case, complexity is the measure of relative to other things.

And then we're talking about the storage of the algorithm, including:

The space occupied by the procedure itself;

The space occupied by the input data;

The space occupied by the auxiliary variable;

The higher the efficiency of an algorithm, the better, and the lower the storage is the better.

3. Summary

In this article, we briefly introduce the concept of data structure and algorithm, the algorithm is the steps to solve the problem, and the implementation of the data structure can not be separated from the algorithm, it may be more ambiguous to understand, do not worry, we will be in the concrete data structure and algorithm implementation of the process explained in detail.

  

Java Data structures and algorithms (i)--Introduction

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.