Basic data structures: arrays

Basic data structures: arrays

Array is a list containing the data. The index to identify the location of each item of data in the array.

To understand the performance of a data structure (such as arrays), and how to operate the program have to analyze this data structure. General data structure has the following four operations (or usage).

• Read: View data on a location in the data structure. For arrays, this means that the value of an indexed view the data referred to. For example, to see what food index 2, is a read.

• Find: Find out where a data value from the data structure. For arrays, this means checking whether it contains a value, if included, it had given its index. For example, check the "dates" if present in the food list, given its corresponding index, is a find.

• Insert: to increase the data structure of a data value. For arrays, this means one more and fill in a grid value. For example, the shopping list to add one to the "figs", is a plug.

• Delete: remove a data value from the data structure. For arrays, this means to a data item in the array removed. For example, the shopping list "bananas" removed, is a kind of delete.

 

Speed of operation, not a time basis, but by the number of steps calculated. Furthermore, speed of operation is also known as time complexity .

 

Read

Read, view the data values ​​in the array that is an index referring to.

As long as this step is enough, because the computer itself has the ability to jump to an index position either.

Therefore, the reading of the array is a very efficient operation, as long as it is like a step. Naturally, this is the fastest step. This one-step any index reading ability is one of the reasons the array of easy to use.

O (1)

 

Seek

Find is to check if it contains a value, if included, will have to give its index.

One by one to check the lattice approach is to find the most basic method - linear search.

A grid array of N, up to the number of steps to find the linear N (N can be any natural number).

O (N)

 

insert

To insert a new element in the array of speed, depending on which you want to insert it into position.

Suppose you want to insert in the end. So just one step. Because the computer knows the memory address of the beginning of the array, the array contains know how many elements, it is possible to calculate the memory address to be inserted, and then insert a step jump there on the line.

However, at the beginning or in the middle array is inserted, it is another matter. In this case, we need to move other elements to make room, then have to spend the extra few steps. Least efficient insertion (take up a few steps) is inserted at the beginning of the array. Because this time the need to have all the elements in the array to the right. An array with N elements, the worst-case data is inserted takes N + 1 step. That is inserted at the beginning of the array, resulting in movement of N times, with the first insert.

O (N)

 

delete

Delete data on the array is to eliminate some of its index.

Like with insert, delete, delete the worst case is the first element of the array. Because the array element does not allow empty, empty when the index 0, then the rest should be left to remove all the elements fill in the blank. For an array with N elements, delete operation requires a maximum of N steps.

O (N)

 

Reference: Data Structures and Algorithms illustrated .1

Guess you like

Origin www.cnblogs.com/ooo0/p/12162034.html
Recommended