Practica 1
Practica 1
October 6, 2020
1 Practica 1
Adriana Valadez Squivias Se crea una funcion de devuelve un arreglo de numeros aleatorios
def getRandomNumbers(size):
return [randint(0,200)for _ in range(size)]
[2]: randomNumbers=getRandomNumbers(5)
ascendentNumbers = sorted(randomNumbers)
descendentNumbers = sorted(randomNumbers, reverse = True)
1
print("bubble sort:")
print(len(lista))
return lista
print(performance)
bubble sort:
1000
bubble sort:
1300
bubble sort:
1600
bubble sort:
1900
bubble sort:
2200
bubble sort:
2500
bubble sort:
2800
bubble sort:
3100
2
bubble sort:
3400
bubble sort:
3700
bubble sort:
4000
bubble sort:
4300
bubble sort:
4600
bubble sort:
4900
[0.29065680503845215, 0.4593842029571533, 0.7998840808868408,
1.1299455165863037, 1.462775468826294, 1.8515520095825195, 2.2814574241638184,
2.6408560276031494, 3.1876418590545654, 3.7974517345428467, 4.422271251678467,
5.156589508056641, 5.844255447387695, 6.858909845352173]
[7]: plt.plot(performance)
plt.title("Caso aleatorio Bubble sort")
3
1.2 Bubble sort mejor caso
[8]: performance = []
for x in range(startCount,endCount,stepCount):
ascendentNumbers = sorted(getRandomNumbers(x))
start = time.time()
bubble_sort(ascendentNumbers)
end = time.time()
performance.append(end - start)
print(performance)
bubble sort:
1000
bubble sort:
1300
bubble sort:
1600
bubble sort:
1900
bubble sort:
2200
bubble sort:
2500
bubble sort:
2800
bubble sort:
3100
bubble sort:
3400
bubble sort:
3700
bubble sort:
4000
bubble sort:
4300
bubble sort:
4600
bubble sort:
4900
[0.20505642890930176, 0.3390960693359375, 0.5282728672027588,
0.8098134994506836, 1.0227129459381104, 1.3260595798492432, 1.641322135925293,
1.9537911415100098, 2.257321834564209, 2.7721691131591797, 3.1787869930267334,
3.8378281593322754, 4.125147104263306, 4.891324520111084]
[9]: plt.plot(performance)
plt.title("Mejor caso Bubble sort")
4
[9]: Text(0.5, 1.0, 'Mejor caso Bubble sort')
print(performance)
bubble sort:
1000
bubble sort:
1300
bubble sort:
1600
bubble sort:
1900
bubble sort:
2200
bubble sort:
5
2500
bubble sort:
2800
bubble sort:
3100
bubble sort:
3400
bubble sort:
3700
bubble sort:
4000
bubble sort:
4300
bubble sort:
4600
bubble sort:
4900
[0.34529900550842285, 0.6262035369873047, 1.0363614559173584,
1.3877105712890625, 1.8467557430267334, 2.331789016723633, 2.748182773590088,
3.4751758575439453, 4.119062423706055, 4.959979295730591, 5.98060154914856,
6.5922746658325195, 7.763664960861206, 8.582663297653198]
[11]: plt.plot(performance)
plt.title("Peor caso Bubble sort")
6
2 Merge sort
definición de la función:
lista.clear()
merge_sort(Left)
merge_sort(Right)
else:
lista.append(Right.pop(0))
return lista
merge sort:
5
[196, 172, 62, 87, 94]
[62, 87, 94, 172, 196]
7
2.0.1 Merge sort caso intermedio (aleatorio)
[14]: performance = []
for x in range(startCount,endCount,stepCount):
print("merge sort:")
print(x)
start = time.time() #O(1)
merge_sort(getRandomNumbers(x)) #O(n2)
end = time.time() #O(1)
performance.append(end - start) #O(1)
print(performance)
merge sort:
1000
merge sort:
1300
merge sort:
1600
merge sort:
1900
merge sort:
2200
merge sort:
2500
merge sort:
2800
merge sort:
3100
merge sort:
3400
merge sort:
3700
merge sort:
4000
merge sort:
4300
merge sort:
4600
merge sort:
4900
[0.01595282554626465, 0.007999897003173828, 0.0240020751953125,
0.017951488494873047, 0.015616893768310547, 0.031240224838256836,
0.032794952392578125, 0.03590202331542969, 0.04002118110656738,
0.039977073669433594, 0.03978848457336426, 0.04686379432678223,
0.06244659423828125, 0.05947542190551758]
8
[15]: plt.plot(performance)
plt.title("Caso intermedio Merge sort")
print(performance)
merge sort:
1000
merge sort:
1300
merge sort:
1600
9
merge sort:
1900
merge sort:
2200
merge sort:
2500
merge sort:
2800
merge sort:
3100
merge sort:
3400
merge sort:
3700
merge sort:
4000
merge sort:
4300
merge sort:
4600
merge sort:
4900
[0.011351585388183594, 0.008020877838134766, 0.007994890213012695,
0.016000032424926758, 0.008160114288330078, 0.01563405990600586,
0.031226158142089844, 0.03125810623168945, 0.031221866607666016,
0.03126692771911621, 0.03554940223693848, 0.0320589542388916,
0.03123950958251953, 0.04155850410461426]
[17]: plt.plot(performance)
plt.title("Mejor caso Merge sort")
10
2.2 Merge sort peor caso
[18]: performance = []
for x in range(startCount,endCount,stepCount):
descendantNumbers = sorted(getRandomNumbers(x), reverse = True)
print("merge sort:")
print(x)
start = time.time()
merge_sort(descendantNumbers)
end = time.time()
performance.append(end - start)
print(performance)
merge sort:
1000
merge sort:
1300
merge sort:
1600
merge sort:
1900
merge sort:
2200
merge sort:
11
2500
merge sort:
2800
merge sort:
3100
merge sort:
3400
merge sort:
3700
merge sort:
4000
merge sort:
4300
merge sort:
4600
merge sort:
4900
[0.008069515228271484, 0.0079803466796875, 0.007997989654541016,
0.014364480972290039, 0.015639781951904297, 0.031238794326782227,
0.01563549041748047, 0.015634775161743164, 0.031239032745361328,
0.031241178512573242, 0.0429997444152832, 0.03199958801269531,
0.04841113090515137, 0.039296627044677734]
[19]: plt.plot(performance)
plt.title("Peor caso Merge sort")
12
13