Compare commits

...

4 Commits

Author SHA1 Message Date
Vinicius Silva 34504aaf5f Add more algorithms 2024-05-13 19:24:18 -03:00
Vinicius Silva f15cbebb9d Add more algorithms 2024-05-13 19:22:31 -03:00
Vinicius Silva e32352b117 Adding more algorithms 2024-05-13 12:13:28 -03:00
Vinicius Silva 956871e340 Adding more algorithms 2024-05-13 12:12:23 -03:00
14 changed files with 334 additions and 0 deletions

View File

@ -0,0 +1,21 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i , j):
tmp = lista[i]
lista[i] = lista[j]
lista[j] = tmp
def insertion():
for i in range(0, len(lista)-1):
j = (i+1)
while j > 0 and lista[j-1] < lista[j]:
swap(j-1, j)
j -= 1
if __name__ == '__main__':
insertion()
print(lista)

View File

@ -0,0 +1,24 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i , j):
tmp = lista[j]
lista[j] = lista[i]
lista[i] = tmp
def selection():
for i in range(0, len(lista)):
pos = i
for j in range((i+1), len(lista)):
if lista[j] > lista[pos]:
pos = j
swap(pos, i)
if __name__ == '__main__':
selection()
print(lista)

View File

@ -0,0 +1,27 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i ,j):
tmp = lista[i]
lista[i] = lista[j]
lista[j] = tmp
def buble():
for i in range(0, len(lista)):
j = len(lista)-1
less_pos = j
while i < j:
if lista[j] < lista[less_pos]:
less_pos = j
j -= 1
if lista[less_pos] < lista[i]:
swap(i, less_pos)
if __name__ == '__main__':
buble()
print(lista)

View File

@ -0,0 +1,20 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i, j):
tmp = lista[i]
lista[i] = lista[j]
lista[j] = tmp
def insertion():
for i in range(0, len(lista)):
j = i
while j > 0 and lista[j-1] > lista[j]:
swap(j, j-1)
j -= 1
if __name__ == '__main__':
insertion()
print(lista)

View File

@ -0,0 +1,25 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i, j):
tmp = lista[i]
lista[i] = lista[j]
lista[j] = tmp
def selection():
for i in range(0, len(lista)):
less_pos = i
for j in range((i+1), len(lista)):
if lista[j] < lista[less_pos]:
less_pos = j
if less_pos != i:
swap(less_pos, i)
if __name__ == '__main__':
selection()
print(lista)

View File

@ -0,0 +1,24 @@
lista = sorted([0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82])
def binary_callable(val):
return binary(val, 0, len(lista)-1)
def binary(val, i, j):
middle = int((i + j)/2)
if j < i:
return False
elif val == lista[middle]:
return True
elif val > lista[middle]:
return binary(val, middle+1, j)
else:
return binary(val, i, middle-1)
if __name__ == '__main__':
print('FOUND') if binary_callable(-1) else print('NOT FOUND')
print('FOUND') if binary_callable(4) else print('NOT FOUND')
print('FOUND') if binary_callable(0) else print('NOT FOUND')
print('FOUND') if binary_callable(82) else print('NOT FOUND')
print('FOUND') if binary_callable(88) else print('NOT FOUND')

View File

@ -0,0 +1,15 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def sequential(val):
for element in lista:
if element == val: return True
return False
if __name__ == '__main__':
print('FOUND') if sequential(-1) else print('NOT FOUND')
print('FOUND') if sequential(4) else print('NOT FOUND')
print('FOUND') if sequential(0) else print('NOT FOUND')
print('FOUND') if sequential(82) else print('NOT FOUND')
print('FOUND') if sequential(88) else print('NOT FOUND')

View File

View File

@ -0,0 +1,17 @@
class Node():
def __init__(self, val):
self.val = val
self.right = None
self.left = None
class BinaryTree():
def __init__(self, root):
self.root = root
def insert(self, val):
node = self.root

View File

@ -0,0 +1,35 @@
class HashSet():
def __init__(self, type):
self.type = type
self.hash = []
def insert(self, data):
if type(data) == self.type and not self.__contains(data):
self.hash.append(data)
def remove(self, data):
self.hash.remove(data)
def __contains(self, data):
return data in self.hash
def __str__(self):
string = ''
for element in self.hash:
string += f'{element} '
return string
if __name__ == '__main__':
hashset = HashSet(str)
hashset.insert('Vinicius')
hashset.insert(1)
hashset.insert(4)
hashset.insert('Teste')
print(hashset.__str__())
hashset.remove('Vinicius')
print(hashset.__str__())

View File

@ -0,0 +1,51 @@
class Cell():
def __init__(self, data):
self.data = data
self.next = None
class LinkedList():
def __init__(self, root):
self.root = root
def insert(self, data):
cell = self.root
while cell.next:
cell = cell.next
cell.next = Cell(data)
def remove(self, data):
if self.root.data == data:
self.root = self.root.next
else:
cell = self.root
while cell.next and cell.next.data != data:
cell = cell.next
if not cell.next: cell.next = cell.next.next
def __str__(self):
string = ''
r = self.root
while r != None:
string += f'{r.data} '
r = r.next
return string
if __name__ == '__main__':
linked = LinkedList(Cell('Vinicius'))
linked.insert('Teste')
linked.insert('Felix')
linked.remove('Felix')
linked.remove('Felix')
print(linked.__str__())

View File

@ -0,0 +1,37 @@
class Queque():
def __init__(self):
self.data = []
self.total_elements = 0
def enqueque(self, element):
self.data = [element] + self.data
self.total_elements += 1
def dequeque(self):
if self.total_elements > 0:
self.data[self.total_elements-1]
self.total_elements -= 1
def __str__(self):
string = ''
for i in range(0, self.total_elements):
string += f'{self.data[i]} '
return string
if __name__ == '__main__':
queque = Queque()
queque.enqueque(1)
queque.enqueque(2)
queque.enqueque(3)
queque.enqueque(4)
queque.enqueque(5)
queque.dequeque()
queque.dequeque()
queque.dequeque()
queque.enqueque(44)
print(queque.__str__())

View File

@ -0,0 +1,38 @@
class Stack():
def __init__(self):
self.data = []
def pop(self):
if len(self.data) == 0:
raise IndexError('Stack out of range')
else:
last_element = self.data[0]
self.data.remove(last_element)
return last_element
def push(self, data):
self.data = self.data + [data]
def __str__(self):
string = ''
for element in self.data:
string += f'{element} '
return string
if __name__ == '__main__':
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.pop()
stack.pop()
stack.pop()
stack.push(44)
print(stack.__str__())