4. The linear array of table

Public concern number MageByte, punctuation set star "look" is that we create a good culture of power. Background reply "plus group" into the technical exchange group won more technical growth.

One array for each programming language for data structures are important, of course, are not the same in different languages ​​and to achieve processing array. Provided in an array in the Java language is used to store a fixed size elements of the same type.

You will say the array so simple, so what to say. Hey hey hey, which contains the mystery may not necessarily everyone knows.

Today's doubts come ......

Array almost always numbered from 0, there is no thought about why an array numbered from 0, rather than from the beginning of it? 1 Use not more in line with what the human mind?

Array Introduction

An array is a linear list data structure, a set of contiguous memory space to store a set of data having the same type.

Which appeared in several important keywords, linear form, and the same type of continuous data memory space, where the interpretation of the meaning of each keyword.

Linear table

Is the same imaging data row line structure, just like our high-speed rail No. G1024, each car end to end, the data only up to "post", "front" and in both directions. In addition arrays, linked lists, queues, stacks are linear structures.

Nonlinear table

For example binary tree, stack, etc. FIG. It is called non-linear, because, in the nonlinear table, not a simple relationship between the data before and after.

Contiguous memory space

Because of its formal contiguous memory space and the same data type. There is a fast hardware features: "random access." A lot of people must be asked when interviewing an array of linked lists what's the difference? Most would answer "list for insert, delete, the time complexity of O (1); look for the array to find the time complexity is O (1)".

The answer is not strict. Finding suitable, but not the time to find the complexity of O (1), even if the data is already sorted, you find the time complexity is O (logn) with a dichotomy. Should be correct, the array support random access, the random access time according to the following table complexity is O (1).

Random access

We all know that the array is based on the table to access the data, how it is implemented random access it?

An int type array of length 4 int[] a = new int[4]for example, an array of a first computer to a contiguous memory space allocated 1000 to 1015. int is 4 bytes, so the total possession of 4*4bytes. The first address of the memory block base_address = 1000. When the random access procedure in the i-th array element, the computer calculates the memory address by addressing the following equation.

targetAddress = base_address + i * data_type_size
  • targetAddress: access destination memory address.
  • base_address: first address an array of memory blocks.
  • i represents the index to be accessed, data_type_size: byte size data types, such as int is four bytes.

Like the first address high-speed rail G1024 number, each car is an array subscript position, each car seat like a byte length.

Knock on the blackboard: the students, addressing an array formula is going on here. This formula also pave the way to explain why the final index starts at 0.

Why subscript starts at 0?

The most precise definition of "index" should be "offset (offset)". Also mentioned the foregoing, if the first address is represented by base_address array, a [0] is the position offset of 0, i.e. the first address, a [i] represents a position offset to the i-th data_type_size, it calculates a [ i] of the memory address just use this formula:

targetAddress[i] = base_address + i * data_type_size

Now the question is, if the array index starts calculating a [i] in the memory address needs to change the formula:

targetAddress[i] = base_address + (i - 1) * data_type_size

Focus here, compared to two formulas, starting at 1 random access array elements each have more than a subtraction, equivalent to more than perform a subtraction instruction.

As a very basic array data structure, random access by index array element is its very basic programming operation, optimizing efficiency will achieve the ultimate as far as possible. Therefore, in order to reduce a subtraction operation, the selected array are numbered starting from zero, rather than from the beginning.

Of course, this can not be said to be absolute, or it may be historical reasons, C language design is from 0, the back of the high-level languages ​​are to follow, but also easy to quickly adapt the program ape, reduce learning costs.

Inefficient insertion and deletion

Pros and cons, this restriction also result in the deletion of the array, inserting such operations become inefficient, in order to ensure the continuity of memory, we need to move data to do the work.

That there is no way to improve it?

Insert

Array of length n, an element is inserted into the k-th position in the array. In order to meet the continuity we need to vacate this position k, to account for the newly inserted data pit, then k n to the data in this section are moved back one. The insertion time complexity is how much? With our analysis, study under way analysis of time and space complexity .

When inserted into the end of the array elements, there is no need to move data, "the best time complexity" is O (1). When the insertion position of the beginning of the array, all the data that needs to sequentially move back one, all the worst time complexity of O (n). And we insert an element probability is the same at each location, so the average time complexity is ( 1 + 2 + 3 + + n ) n = O ( n ) \frac {(1+2+3+…+n)} {n} = O(n) .

Optimization ideas - dove over the magpie's nest

If the array is an ordered sequence, we need to move data after k, if the data stored in random array, only as a set of stored data, to a position of k elements into the array, we can put the original k elements into an array in the last position, the insertion of a new element into the position k, the time complexity reduces to O (1).

Deletion

Similarly, suppose we want to delete the data of the k-th position, if k = n-1, then the best time complexity is O (1). Ruoguo k = 0, worst case time complexity of O (n). Average time complexity is O (n).

Optimization ideas - mark - batch execution

In fact, in some cases you do not need to have to pursue the continuity of data. You can delete multiple operating batch execution.

Such as arrays number [6] is stored in the six elements of type int: 1,2,3,4,5,6. 1,2,3 and then click Delete. Three elements. Every move is required to prevent the deletion of data, as long as we mark data has been deleted, delete threshold is reached, for example, is 3, before the implementation of mobile data, this time only the move operation, greatly reducing data movement.

You will find that this does not remove the core idea is to mark JVM garbage collection algorithm do? Yes, the charm of data structures and algorithms in this, many times we are not going to memorize a data structure or algorithm, but to learn thinking skills and process behind it, these things are the most valuable . If you look carefully, whether in architecture or design, software development, we can always find some shadow algorithms and data structures.

To Learn & summary

With an array of contiguous memory space to store a set of data of the same type, the biggest feature is support for random access, but the insertion, deletion and therefore become less efficient, where the average time complexity is O (n). In the usual business development, we can directly use the container class programming languages, however, especially if it is the underlying development, direct use of the array may be more appropriate.

The question is

We present an array delete operation based on an optimized idea: mark - Batch clear thinking, in Java's JVM, garbage collection algorithm marks clear what is it? Please add the group to share your thoughts or backstage reply "clear markers" to get answers.

Welcome plus group discussions to share with us, our first time feedback.

Recommended Reading

1. The importance of data structure algorithms

2. Time complexity and space complexity

3. Preferably, the worst average, the time complexity is amortized

MageByte

Published 28 original articles · won praise 2 · Views 1453

Guess you like

Origin blog.csdn.net/qq_14855971/article/details/105077429