Algorithms practice:array

introduce

The simplest data structure is the array, which is a contiguous block of memory. It is usually used to represent sequences. Given an array A, A[i] denotes the (i+1)th object stored in the array. Retrieving and updating A[i] takes O(1) time. Insertion into a full array can be handled by resizing, i.e., allocating a new array with additional memory and copying over the entries from the original array. This increases the worst-case time of insertion, but if the newarray has, for example, a constant factor larger than the original array, the average time for insertion is constant since resizing is infrequent. Deleting an element from an array entails moving all successive elements one over to the left to fill the vacated space.

Contiguous memory locations refer to a situation where multiple memory addresses are adjacent or continuous without any gaps between them. In the context of arrays, this means that the elements of the array are stored next to each other in memory.

Characteristics of Contiguous Memory:

  1. Adjacent Storage: Elements in an array are stored one after another in memory, without any other data interspersed between them. For example, if an integer array starts at memory address 1000, the next integer element will be at 1004 (assuming an integer takes 4 bytes of memory).

  2. Sequential Access: Accessing elements based on their index is efficient because the memory addresses are consecutive. By knowing the memory address of the first element and the size of each element, the location of any other element in the array can be calculated easily.

Importance in Array Operations:

  • Efficient Access: Because elements in a contiguous array are stored linearly, accessing elements by index involves simple arithmetic operations to calculate memory addresses. For example, accessing the 5th element in an array directly involves going to the memory address of the first element and adding the appropriate offset (5 times the size of each element).

  • Iteration and Traversal: Iterating through elements of the array becomes more efficient due to their sequential arrangement in memory. Looping through contiguous memory locations allows for faster traversal compared to scattered or non-contiguous data structures.

Drawbacks:

  • Fixed Size: Arrays have a fixed size once they are created. If the array needs to grow beyond its initial capacity, it might require a new allocation of contiguous memory, potentially causing inefficiency or the need to copy elements to a new location.

  • Fragmentation: Over time, as arrays are created and deleted, memory fragmentation can occur, leading to unavailability of contiguous blocks of memory even if the total free memory is sufficient.

Understanding contiguous memory is crucial for understanding the efficiency of array-based data structures and their underlying memory management. It’s also relevant in optimizing algorithms that heavily rely on sequential access to elements, such as search and sorting algorithms.

The time complexity of deleting

The time complexity of deleting an element from an array depends on the position of the element that needs to be deleted.

Deleting from the End of the Array (Given the Index):

If you’re deleting the last element of an array given its index, the time complexity is constant, denoted as O(1). This is because you directly access the element by its index and remove it, requiring no further rearrangement of elements.

# Example: Deleting the last element from an array
arr = [1, 2, 3, 4, 5]
index_to_delete = len(arr) - 1  # Index of the last element
del arr[index_to_delete]
# Time complexity: O(1)

Deleting from the Start or Middle of the Array:

If you want to delete an element from the beginning or middle of the array, after removing the element, the subsequent elements need to be shifted to fill the gap created by the deletion. The time complexity in this case is O(n), where ‘n’ represents the number of elements in the array.

# Example: Deleting an element from the middle of an array
arr = [1, 2, 3, 4, 5]
index_to_delete = 2  # Index of the element '3' to be deleted
arr.pop(index_to_delete)
# Time complexity: O(n)

Deleting with a Given Element (Not Index):

If you’re given the value of the element to delete but not its index, finding the element’s index takes O(n) time (in the worst case), and then the deletion operation itself requires shifting elements, resulting in a time complexity of O(n).

# Example: Deleting an element by its value from an array
arr = [1, 2, 3, 4, 5]
element_to_delete = 3
arr.remove(element_to_delete)  # This involves finding the index first
# Time complexity: O(n) + O(n) = O(n)

In summary:

  • Deleting an element from the end of an array: O(1)
  • Deleting an element from the start or middle of an array: O(n)
  • Deleting an element by its value (not index): O(n) + O(n) = O(n)

english

中文英文
连续内存块contiguous block of memory
A[i]表示 第i+1个对象A[i] denotes the (i+1)th object
取数据retrieve [rɪˈtriːv]
更新数据update
分配内存allocateallocating a new array with additional memory
连续元素successive element
空出的空间vacated space
多个内存地址相邻multiple memory addresses are adjacent
多个内存地址连续multiple memory addresses are continuous
表达连续存储Contiguous memory locations refer to a situation where multiple memory addresses are adjacent or continuous without any gaps between them.
Elements in an array are stored one after another in memory, without any other data interspersed between them.
访问元素access elements
内存地址memory address
顺序访问Sequential Access
固定尺寸fixed size
内存碎片memory fragmentation
算术运算arithmetic operations
迭代Iteration
  • contiguous [kənˈtɪɡjuəs]: touching or next to sth相接的;相邻的:
    array is a contiguous block of memory
  • block of memory 内存块
  • denote [dɪˈnəʊt] to mean sth
    A[i] denotes the (i+1)th object stored in the array.
  • retrieve [rɪˈtriːv] 更新数据update
    Retrieving and updating A[i] takes O(1) time.
  • represent [ˌreprɪˈzent] : to be an example or expression of sth.
    It is usually used to represent sequences.
  • 插入 Insertion insert
  • handle[ ˈhændl] to deal with a situation, a person, an area of work or a strong emotion
    Insertion into a full array can be handled by resizing.
  • additional memory
  • copy over: Over" signifies that there’s a destination to which the copy will be sent, just like walking over a bridge
    He copied over the information from the old spreadsheet to the new one.
  • entail[ɪnˈteɪl] : If one thing entails another, it involves it or causes it.
    Deleting an element from an array entails moving all successive elements one over to the left to fill the vacated space.
  • successive [səkˈsɛsɪv] :following another without interruption
  • vacate [veɪkeɪt] : to cause (something) to be empty, esp by departing from or abandoning it
  • intersperse[ˌɪntəˈspɜːs ]:to put sth in sth else or among or between other things
  • Iteration [$ ˌɪtəˈreiʃən] :an iterating or being iterated; repetition(迭代)
  • traverse[trævɜːʳs ] travel across
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值