Course Content
Algorithms and Data Structures Overview
Algorithms and Data Structures Overview
Array Data Structure
An array is a fundamental data structure in computer science used to store a collection of elements, such as numbers, characters, or objects, of the same data type. Arrays offer a way to organize and access data efficiently by storing elements in contiguous memory locations and providing indexed access to each element.
Each element can be accessed using its index in an array. Since computers operate with memory addresses, knowing the index of an element allows us to calculate its address using the following formula:begin_of_array_address + index_of_element * size_of_element
.
begin_of_array_address
: This represents the memory address of the beginning of the array. It indicates the starting point in memory where the array is stored;index_of_element
: This variable represents the index or position of the element we are interested in within the array. It denotes the element's location relative to the array's beginning;size_of_element
: This variable signifies the size or length of each element in the array, typically measured in bytes. It indicates the amount of memory occupied by each element in the array.
The time complexity of this operation is O(1)
, and this is the main feature of arrays that if we know the index, we can access the given element in contrast time.
In Python, real arrays aren't implemented natively. To use this data structure we have to import NumPy library:
import numpy as np from lolviz import * # Assuming this library is for visualization of Python objects from IPython.display import display_png # Importing `display_png` function for displaying PNG images # Generate a random array of integers between 0 and 100 with a size of 10 real_array = np.random.randint(0, 100, size=(10)) # Print the data type of the array values print("Array data type is: ", real_array.dtype) # Display a visual representation of the array using the `objviz` function from the `lolviz` library display_png(objviz(real_array))
Thanks for your feedback!