Array
An array is a way to store data of the same type or class (integers, characters, Boolean, etc.), although in some languages data of different classes can be stored. each element is enumerated with its index. With this you can easily access an array element only with its index. the enumeration of these elements within the array starts from 0 in most languages and this implies that it ends at n-1 (where n is the size of the array, the number of elements). For this reason it is said that in programming we start counting from 0. The array is probably the simplest and most widely used data structure.
In python you can declare
array=["1","2","3"]
# ind: 0 1 2
you can access an object with its index, for example
array[1]
#output: 2
There are several methods that can be used in an array. here are some of them
append
array.append("4") #O(1)
This method will add an element to the end of the list. this will not change the indixes of the other elements.so it will have a complexity of 1 because
pop
array.pop() #O(1)
This method will remove the last item from the list, and like the previous method, this will not change the indexes of the other items, so it will have a complexity of 1 since it will have a complexity of 1. so it will have a complexity of 1 because Although pop will also delete an element if we give it the index as parameter
array.pop(2) #O(n)
But it will no longer have a complexity of 1. It will be n
insert
array.insert(0,"0")#O(1)
This method will insert the "0" at the top of the list but will move all other items to the right 1 index. This will make the complexity n (the size of the list)
there are many more methods that we can find like max, min, sort that can be seen in the python wiki
how an array is constructed
To better understand how it is constructed we can make one
class my_array():
def __init__(self):
self.length = 0
self.data = {}
def __str__(self):
return str(self.__dict__)
def get(self, index):
return self.data[index]
def push(self, item):
self.data[self.length] = item
self.length += 1
def pop(self):
self.length -= 1
last_item = self.data[self.length]
del self.data[self.length]
return last_item
def insert(self, index, item):
for i in range(self.length, index, -1):
self.data[i] = self.data[i-1]
self.length += 1
self.data[index] = item
def delete(self,index):
for i in range(index, self.length-1):
self.data[i] = self.data[i+1]
del self.data[self.length - 1]
self.length -= 1
Code example
we can see an example code that consists of merging 2 arrays and sorting them. There are many ways to do this, the simplest way is to add the arrays and sort them:
def merge_arrays(arr1, arr2):
return sorted(set(arr1+arr2))
but this is not the fastest way to do it, since it is sorted after the addition, the best and fastest way would be that when the array is merged, it is already sorted. And this would be done by sorting it while merging it. that way:
def merge(array1, array2):
new_array = []
flag = 0
first_array_index = second_array_index = 0
while not (first_array_index>=len(array1) or second_array_index>=len(array2)):
if array1[first_array_index] <= array2[second_array_index]:
new_array.append(array1[first_array_index])
first_array_index += 1
else:
new_array.append(array2[second_array_index])
second_array_index += 1
if first_array_index==len(array1):
flag = 1
if flag == 1:
for item in array2[second_array_index:]:
new_array.append(item)
else:
for item in array1[first_array_index:]:
new_array.append(item)
return new_array
Pros and cons
Pros:
We can see that the pros of using this structure is that it has an index and this makes it quick to see any item, and it is also the best options for sorting. We can see that it is quick to remove the last item and add an item at the end (pop and push).
Cons:
It is a bit slow to insert or delete an item(since the index of the items that follow after the deleted item or the item to be added must be moved) likewise with deleting an item.