Java Data Structures and Algorithms (1)

 


  In this series of blogs, we will learn about data structures and algorithms, and why we should learn about data structures and algorithms. Here I will give a simple example.

  Programming is like a car, and data structures and algorithms are the gearbox inside the car. A driver who does not understand the principle of the gearbox can also drive, and similarly a person who does not understand data structures and algorithms can also program. But if a driver understands the principles of the gearbox, like reducing the speed to get more traction, or reducing the traction to get faster driving. Then use 1st gear when climbing, you can get more traction; when going downhill, use low gear to limit the speed of the car. Back to programming, for example, to temporarily store the names of students in a class in memory, what data structure would you choose to store, an array or an ArrayList, or a HashSet, or other data structures. If you don’t understand the data structure, you may choose a container to store and complete all the functions. However, if the amount of student data increases in the later stage, the data structure you choose will definitely have performance problems, and a person who understands the data structure and Algorithms, in actual programming, will choose appropriate data structures to solve the corresponding problems, which will greatly improve the performance of the program.


1. Data structure

  A data structure is the way a computer stores and organizes data, and refers to a collection of data elements that have one or more specific relationships with each other.

  Often, 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.

  First, the basic function of the data structure

  ①, how to insert a new data item

  ②, how to find a specific data item

  ③ How to delete a specific data item

  4. How to iteratively access each data item for display or other operations

  2. Commonly used data structures

   

 

  The advantages and disadvantages of these structures are as follows: first have a general impression, and will explain in detail later! ! !

  

 


2. Algorithm

  Algorithms are simply steps to solve a problem.

  In Java, algorithms are usually implemented by methods of classes. The previous data structures, such as why the linked list is fast to insert and delete, but the search is slow, and the balanced binary tree is fast to insert, delete, and search, all of which are caused by the algorithms that implement these data structures. The various sorting implementations we talk about later are also important areas of the algorithm category.

  First , the five characteristics of the algorithm

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

  ②、确定性:在每种情况下所应执行的操作,在算法中都有确切的规定,使算法的执行者或阅读者都能明确其含义及如何执行。并且在任何条件下,算法都只有一条执行路径。

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

  ④、有输入:作为算法加工对象的量值,通常体现在算法当中的一组变量。有些输入量需要在算法执行的过程中输入,而有的算法表面上可以没有输入,实际上已被嵌入算法之中。

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

  

  二、算法的设计原则

  ①、正确性:首先,算法应当满足以特定的“规则说明”方式给出的需求。其次,对算法是否“正确”的理解可以有以下四个层次:

        一、程序语法错误。

        二、程序对于几组输入数据能够得出满足需要的结果。

        三、程序对于精心选择的、典型、苛刻切带有刁难性的几组输入数据能够得出满足要求的结果。

        四、程序对于一切合法的输入数据都能得到满足要求的结果。

        PS:通常以第 三 层意义的正确性作为衡量一个算法是否合格的标准。

  ②、可读性:算法为了人的阅读与交流,其次才是计算机执行。因此算法应该易于人的理解;另一方面,晦涩难懂的程序易于隐藏较多的错误而难以调试。

  ③、健壮性:当输入的数据非法时,算法应当恰当的做出反应或进行相应处理,而不是产生莫名其妙的输出结果。并且,处理出错的方法不应是中断程序执行,而是应当返回一个表示错误或错误性质的值,以便在更高的抽象层次上进行处理。

  ④、高效率与低存储量需求:通常算法效率值得是算法执行时间;存储量是指算法执行过程中所需要的最大存储空间,两者都与问题的规模有关。

  前面三点 正确性,可读性和健壮性相信都好理解。对于第四点算法的执行效率和存储量,我们知道比较算法的时候,可能会说“A算法比B算法快两倍”之类的话,但实际上这种说法没有任何意义。因为当数据项个数发生变化时,A算法和B算法的效率比例也会发生变化,比如数据项增加了50%,可能A算法比B算法快三倍,但是如果数据项减少了50%,可能A算法和B算法速度一样。所以描述算法的速度必须要和数据项的个数联系起来。也就是“大O”表示法,它是一种算法复杂度的相对表示方式,这里我简单介绍一下,后面会根据具体的算法来描述。

  相对(relative):你只能比较相同的事物。你不能把一个做算数乘法的算法和排序整数列表的算法进行比较。但是,比较2个算法所做的算术操作(一个做乘法,一个做加法)将会告诉你一些有意义的东西;

  表示(representation):大O(用它最简单的形式)把算法间的比较简化为了一个单一变量。这个变量的选择基于观察或假设。例如,排序算法之间的对比通常是基于比较操作(比较2个结点来决定这2个结点的相对顺序)。这里面就假设了比较操作的计算开销很大。但是,如果比较操作的计算开销不大,而交换操作的计算开销很大,又会怎么样呢?这就改变了先前的比较方式;

  复杂度(complexity):如果排序10,000个元素花费了我1秒,那么排序1百万个元素会花多少时间?在这个例子里,复杂度就是相对其他东西的度量结果。

  然后我们在说说算法的存储量,包括:

  程序本身所占空间;

  输入数据所占空间;

  辅助变量所占空间;

  一个算法的效率越高越好,而存储量是越低越好。


3、总结

  本篇文章我们简单的介绍了数据结构和算法的概念,算法是解决问题的步骤,而数据结构的实现离不开算法,可能理解起来比较模糊,不用担心,后面我们会在具体的数据结构和算法实现过程中详细讲解。

  参考书籍:《Java数据结构和算法》链接:https://pan.baidu.com/s/1dFjgRSH 密码:ig0f

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325784723&siteId=291194637