Android frame structure optimization; data structures (arrays)

definition

Array (the Array) is a linear table structure, which uses a set of contiguous memory space to store a set of data having the same type.
There are a few key words in this definition:

Linear table

The data table is called a linear row as the imaging of a line structure. That is a linear relationship between the data elements in the table is one to one relationship, i.e., in addition to the first and last data element, other data elements are end to end.
Linear table comprising: a table and a sequence linked list
address sequence table is continuous inside the element, such as an array or arrays implemented with queue, stack, and other
address list nodes which are not continuous, connected together by a pointer as shown below:

Linear table can specifically explained with reference to the linear form

The linear table linear table team concept is the fee, such as a binary tree, stack, drawing and so on. It is called non-linear table is because, among the non-linear data table is not one of context, as follows:

 

& Contiguous memory space of the same type

This limit is based on the definition, and therefore may only require an element of the array is calculated with respect to the first address (a base_address) offset, you can easily calculate the address of the element.
To a length of 4 array of type int int [] arr = new int [ 4]; Case. When initializing the array, the computer will give a continuous array arr allocated memory space from 1000 to 1015, wherein the first address is base_address = 1000, as shown below:

Each computer will assign a physical address of the memory cell, then the computer to access data in memory by the physical address. When the computer needs to access an element in the array is, can be addressed by the following formula to calculate the address of the memory element:

arr[i]_address = base_address + i * data_type_size

Wherein i represents the elements needed to access the standard, a base_address representative of the first address, data_type_size_ representing each element of memory size, such as is stored in the array of type int java, so data_type_size_ is 4 bytes.

The basic operation of the array

  • Insert
  • Deletion
  • Find operation

Insert

There are two cases insert.

在数组的末尾插入元素

If the operation is inserted at the end, without moving the position of any element, directly add a new position in the last array element can. Thus the insertion end of an array of data time complexity is O (1)

在数组头部插入元素。

If the element is inserted into the head position, and that in order to maintain continuity of the data memory, all the data needs to sequentially move back one, the worst case time complexity is O (n)

Deletion

And insert elements similar to, if you want to delete the data array of the K position, in order to maintain the continuity of memory, you need to position K + 1 to the last element are moved forward one. Also two cases

删除数组末尾元素

The best time complexity is O (1)

删除数组头部元素

The worst time complexity is O (n)

Random access

When we already know an element index in the array, we can directly access the element through the index, such as assuming we already know the number 23 in the array [10, 15, 23, 45] the subscripts 2, so we can [2] to point 23 directly arr. Thus an array of random access time complexity is O (1)

Find operation

Suppose we need to array [10, 15, 23, 45] find whether there are 23, because we do not know which element 23 is the number of the subscript, from start to finish is required in order to traverse each element in the array, and It determines whether or equal to 23.

Code may be as follows:

for (let index = 0; index < array.length; index++) {
    if(element === array[index]) {
      return index;
    }
  }

Thus an element of the array to find the time complexity is O (n)

An array of time complexity summary

Published 56 original articles · won praise 1 · views 2894

Guess you like

Origin blog.csdn.net/chuhe1989/article/details/104989011