Developing DS learning Basics I
Linear Data Structures
Sequence of data organized linearly or sequentially, where each element is added to its previous and next adjacents.
Array
An array is a series or list of variables in the memory block that have the same name and the same data type, but are differentiated with special numbers called #subscripts, also called index. The index is a number that indicates the position of the item.
For example, it could represent an employee identification list or a price list for items for sale.
It is widely used in various applications, such as:
- Storing data for processing
- Implementation of data structures such as #Filas and #Pilas
- Representing data in tables and arrays
- Creating dynamic data structures such as linked lists and trees
Array types
1-dimension array
Stores only one row of elements
Multi-dimensional array
Stores multiple rows of elements
Basic operations in the Arrays
Transversal
Visiting each element of an array in a specific order:
def sequential(array):
for element in array
print(element)
arr = [2, 4, 5, 10, 15, 3]
sequential(arr)
Click here to see result
[2, 4, 5, 10, 15, 3]
def reverse(array):
for element in array[::-1]:
print(element)
arr = [2, 4, 5, 10, 15, 3]
reverse(arr)
Click here to see result
[3, 15, 10, 5, 4, 2]
Insertion
Adding a new element to the array at a specific index, where we call the function to insert a specific value into the array on the desired line.
our array 2, 4, 5, 10, 15, 3 will receive the value 10 in position 3
def insert(array, element, position):
if position < 0 or position > len(array):
print("Invalid position")
else:
array.insert(position, element)
arr = [2, 4, 5, 10, 15, 3]
insert(arr, 10, 3)
print(arr)
Click here to see result
[2, 4, 5, 10, 15, 3]
Delete
Removing an array element from a specific index. Let's remove position 2 from arr
def remove(array, index):
if index < 0 or index >= len(array):
print(“Invalid index”)
else:
array.pop(index)
arr = [2, 4, 5, 10, 10, 15, 3]
remove(arr, 2)
print(arr)
Click here to see result
[2, 4, 10, 10, 15, 3]
Search
Finding the index of an array element. What happens if we look for index 4?
def search(array, value):
for i in range(len(array)):
if array[i] == value:
return i
return -1
arr = [2, 4, 10, 10, 15, 3]
index = search(arr, 4)
value = search(arr, index)
print(f"Value found at position {index} is {value}")
Click here to see result
Value found at position 4 is 15
Update
updates the value of an array at the given index and returns a message confirming the change. If the index is invalid, it reports the error and keeps the array unchanged.
Let's update position 3with value 15.
def update(array, index, new_value):
if 0 <= index < len(array):
array[index] = new_value
return new_value
arr = [2, 4, 10, 10, 15, 3]
index = 3
new_value = 15
update(arr, index, new_value)
print(f"Element at index {index} updated to {new_value}", arr)
Click here to see result
Element at index 3 updated to 15
Updated array: [2, 4, 10, 15, 15, 3]
Update
updates the value of an array at the given index and returns a message confirming the change. If the index is invalid, it reports the error and keeps the array unchanged.
def display(array):
return array
arr = [2, 4, 10, 10, 15, 3]
message = display(arr)
print(message)
Click here to see result
Element 15 removed from queue.
Current queue:
6 2 9 4
References
1. Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Third Edition (3rd. ed.). The MIT Press, 2009.
2. GONZAGA DE OLIVEIRA, S. L. Algoritmos e seus fundamentos. Lavras: Editora UFLA, 2011.
3. SZWARCFITER, Jayme L.; MARKENZON, Lilian. Estruturas de Dados e Seus Algoritmos. Rio de Janeiro: LTC, 2010. E-book. ISBN 978-85-216-2995-5.