Shredded Data Structures and Algorithms - Array

Foreword

It begins with a map and knowledge thanks to blow!

Like opening a point, bloggers can heaven!

Shredded Data Structures and Algorithms - Array

This series of articles has been included to github:

1. What is an array?

An array is a data structure 最简单, 最常用the data structure, the data structure is a linear table, is an in-memory 连续storage space, is a finite number 相同类型composed of variables 有序集合. Each array is called a variable 元素.

Linear table: linear form is understood in the literal sense as a data structure arranged in a line, only two longitudinal directions. Linear elements in the table are one-to-head and tail elements in addition, other elements are connected end to end. In addition arrays, linked lists, queues, stacks also linear table structure.

With integer array, for example, we have a new integer array int[] array = new int[]{1,2,3,4};, elements of elements stored in the array is 1,2,3,4. Then the storage array on the situation in the following figure:

.Png graphic array

In the figure above 粉色the grid it has been occupied representative memory cell, 绿色the storage location of the representative grid array, 白色a grid for representation idle storage units. Array indices are zero-based. So the subject of correspondence between the elements and under are:

Shredded Data Structures and Algorithms - Array

2. The array of strengths and weaknesses

Talking about the advantages of the array, I believe most people would say 随机访问this called killer feature, why is it able to do random access it?

I think there are two points:

  • Contiguous storage space
  • Linear table structure

Because it is a piece of contiguous storage space in memory and table structure is linear, the front and rear elements are one to one, so to be able to let him have the characteristics of random access. In the article data structures and algorithms - the opening of which we introduced time complexity and space complexity of here is not to say, for example, we want to find an array of top of the third element, then print out array[2]will be able to get the third element values, where the output is 3, because the array support random access, so in accordance with the time complexity index for random access O(1), the seek operation as it is performed only once.

这样的结构使它的查询操作非常的方便,有利也有弊,它的插入、删除操作就会变得低效,因为要保证数据的连续性,所以执行插入、删除操作就需要做大量的数据搬移工作。如果这个时候一个数组的随机访问正好访问到没有值得下标上就会获取不到值。如果不搬移数据将中间的空洞补充上,那么内存就不连续了。我们在数组的操作中在详细介绍。

3. 数组的基本操作

3.1 添加元素

  • 中间插入

    中间插入稍微复杂一些,每个元素都有自己的下标,如果一个元素想要插入到数组的中的除首尾的位置,那么插入的该位置上的元素都要向后移动,给新的位置腾出空间,保证连续性。

    Shredded Data Structures and Algorithms - Array

    Shredded Data Structures and Algorithms - Array

  • 尾部插入

    尾部插入这种情况比较简单,直接把元素放到数组尾部的空闲位置即可,等同于更新元素的操作。

    Shredded Data Structures and Algorithms - Array

    QZJyrj.png

3.2 删除元素

删除操作和插入操作的过程正好相反,如果删除的元素在数组的中间,那么其后的元素都要向前移动。

QZNRII.png

 QZNqds.png

3.3 更新元素

carbon (1).png

这里更新元素的时间复杂度为O(1)

3.4 读取元素

carbon.png

这里读元素的时间复杂度为O(1)

4. 容器能够代替数组吗?

针对数组类型,很多语言都提供了容器类,例如Java的List,如果你是一个Java程序员,那么你应该清楚ArrayList,对它应该非常的熟悉,和数组对比它有哪些优势呢?为什么开发的过程中经常使用它,最大的优势就是封装了对数组的操作,例如前面说的插入和删除,如果使用ArrayList还有一个优势是它支持动态扩容,当容器不够大的时候会自动扩容1.5倍,我们完全不需要关心底层的实现逻辑。那么什么时候使用数组更合适呢?有一下几点:

  • ArrayList无法存储基本数据类型,例如int、long需要封装为Integer、Long。这里的装箱、拆箱操作有一定的性能损耗,如果特别关注性能,希望使用基本类型,那么就可以选择数组。
  • 对数据只是简单的存储操作,那么选择数组效率更好些。
  • 当做一些底层开发的时候数组可能用的比较多,比如一些框架。都是比较要求性能的。

5. 参考

《漫画算法》

《数据结构与算法之美》

6. The end of seeking attention links

In my opinion there is back-end programmers should learn the basics of the three "数据结构与算法", "计算机系统", "操作系统Linux". In winter this Internet era, it is not what we wear enough clothes? I (awake at night 纯属扯淡,哈哈) decided to take them to learn the basics of the three together, this series is opening "Shredded Data Structures and Algorithms", each a more complete series will open the next series, do not worry. No. I can focus on the public, more sustained recovery, continuous learning.

注意、注意 前方高能======>

If you can focus on my public interested in my numbers in this series, takes you to a "super way of God, salaries offer, when the technical experts, as a giant, marry white Formica , took to the pinnacle of life, want think there is a little excited. "(ha ha, let me blow a

Guess you like

Origin blog.51cto.com/14451464/2455239