This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algoritmos.py
70 lines (49 loc) · 1.89 KB
/
algoritmos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from random import randint
from timeit import timeit
import sys
def sortN(numeros):
outputArray = [None]*len(numeros)
countArray = [0]*int(sys.argv[1])
for numero in numeros:
countArray[numero - 1] += 1
for i in range(1, len(countArray)):
countArray[i] += countArray[i-1]
for i in range(len(numeros) - 1, -1, -1):
outputArray[countArray[numeros[i] - 1] - 1] = numeros[i]
countArray[numeros[i] - 1] -= 1
return outputArray
def sortNxN(numeros):
""" Algoritmo de ordenamiento burbuja (Complejidad O(N^2)) """
for i in range(len(numeros)):
for j in range(len(numeros) - i - 1):
if numeros[j] > numeros[j+1]:
numAux = numeros[j+1]
numeros[j+1] = numeros[j]
numeros[j] = numAux
return numeros
def sortNLogN(numeros):
if len(numeros) < 1:
return []
posicionPivot = randint(0, len(numeros) - 1)
pivot = numeros[posicionPivot]
left = []
right = []
numeros.pop(posicionPivot)
for i in range(len(numeros)):
if numeros[i] < pivot:
left.append(numeros[i])
else:
right.append(numeros[i])
return sortNLogN(left) + [pivot] + sortNLogN(right)
print('\033[94m' + '\n--- RESULTADOS DEL TEST (seconds) ---' + '\033[0m')
print('Array size in test ' + sys.argv[1] + ":\n")
print("Size\tN\tNlogN\tN²")
for k in range(int(sys.argv[1])):
resultado1 = timeit(
'sortN([randint(1, k+1) for i in range(k+1)])', globals=globals(), number=1000)
resultado2 = timeit(
'sortNLogN([randint(1, k+1) for i in range(k+1)])', globals=globals(), number=1000)
resultado3 = timeit(
'sortNxN([randint(1, k+1) for i in range(k+1)])', globals=globals(), number=1000)
print(str(k+1) + "\t" + str(round(resultado1, 4)) + "\t" +
str(round(resultado2, 4)) + "\t" + str(round(resultado3, 4)))