Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why do array subscripts in many languages start with 0 in web development?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "web development why the array subscript of many languages is from 0", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "why the array subscript of many languages in web development is from 0" bar!

Random access to array

Although everyone knows what an Array is, describe it in official terms: array (array) is a linear table data structure. It uses a continuous set of memory space to store a set of data of the same type.

We can grasp a few key words in it to fully understand the structure of arrays.

1, linear table, that is, the arrangement of data from front to back, just like a line, such as queues, stack lists, arrays and so on are all linear table structures.

Of course, a linear table structure has a nonlinear table structure:

2. Continuous memory space and the same data type. Why is data access to a data very efficient? That's because the definition of arrays regulates the structure of arrays, and linear continuity gives us a chance to access them quickly and randomly. But at the same time, it also brings disadvantages, if we insert or delete a piece of data, it is more difficult.

Let's take a look at how arrays achieve random access.

Suppose there is an array like this:

Java int [] a = new int [10]

The operating system allocates a contiguous piece of memory space, assuming 1000 to 1039, then the first address of the memory is base_add = 1000

If you visit relatives, what do you need to know? The address of the relative's home (specific to the house number), the memory is the same, we want to read the data in the memory, and the operating system is also accessed through the address of the memory, so the question is, how do you know the address of the memory?

This involves the addressing of the operating system. For example, if I want to get the value of a [2], the operating system will first calculate the address of the corresponding memory according to the following formula:

Address of a [2] = base_add + 2 * data_unit_size

Dataunitsize represents the size of each element of the data type. Currently, the int type is 4 bytes, so the address of a [2] is 1008.

Can it be said that the time complexity of array search is O (1)? Of course not, normally we don't find the number by subscript, we find it by value, even the time complexity of binary search is O (logn).

Why are deletions and inserts inefficient?

1. Insert operation

Suppose we want to insert a data into the k th position of an array of length n, we have to move the number of k ~ n backward. Similarly, if we insert at the end, we do not need to move the position, and if we insert at the first position, we need to move n, so the average time complexity is: (1, 2, 2, 3, 4, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 3, 4, 2, 3, 4, 2, 2, 3, 4, 2, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 2, 3,

Of course, if the order after insertion is not required to remain the same, a clever way to insert is to put the Kth element at the end and the number to be inserted in the Kth position.

For example, suppose that the following five elements are stored in the array a [10]: a ~.

We now need to insert element x into the third position. We just need to put c into a [5] and assign a [2] to x. Finally, the elements in the array are as follows: a _ journal, b _ r _ r, d _ r _ r _ c.

2. Delete operation.

In fact, it is similar to the insert operation, when we delete the Kth array of the array of length n, we need to move the following data forward. Similarly, the time complexity is also O (n), which is not described in detail here.

Of course, in the special case of not considering the maintenance of continuity, in order to improve the efficiency of deletion, it is not necessary to immediately move each deletion, otherwise if the data is deleted continuously, it will be moved many times in a row. The more ingenious way is to mark the elements to be deleted, actually did not delete, and so on when there is insufficient memory, delete the data of these tags uniformly. This will greatly reduce the large number of data movement operations caused by delete operations.

Disaster! The array is out of bounds

For Java, an exception is thrown when the data is out of bounds, but for some languages, such as C, you will not be given an exception when the array is out of bounds, such as the following code:

Int I = 0; int arr [3] = {0}; for (; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report