""" Carlos J Corrada Bravo Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento La variable maxValor define el valor maximo de los elementos de la lista La variable largoLista define el largo de las listas a ordenar La variable veces define las veces que se va a hacer el ordenamiento Al final se imprimen los promedios de cada algortimo """ from random import randint import time import math def mergeSort(lista): if (len(lista) == 1): return lista n = len(lista) # Se divide la lista firstHalf = lista[0:n//2] secondHalf = lista[n//2:n] # Se vuelve a llamar para cada mitad firstHalf = mergeSort(firstHalf) secondHalf = mergeSort(secondHalf) # Se unen las listas lista = merge(firstHalf,secondHalf) return lista #Esta es la implementación de Heap sort de Geeksforgeeks #https://www.geeksforgeeks.org/heap-sort/ def heapSort(lista): #definan el algoritmo de ordenamiento heapsort n = len(lista) #Se hace el maxheap for i in range(n // 2 - 1, -1, -1): heapify(lista, n, i) #Se extraen elementos uno a uno for i in range(n - 1, 0, -1): (lista[i], lista[0]) = (lista[0], lista[i]) heapify(lista, i, 0) return lista """ Esta implementación de la función partition, relacionada a quicksort fue extraida de geeksforgeeks.org Oct 4, 2022 https://www.geeksforgeeks.org/quick-sort/ """ def partition(lista, low, high): pivot = lista[high] i = low - 1 for j in range(low, high): if lista[j] <= pivot: i = i + 1 (lista[i], lista[j]) = (lista[j], lista[i]) (lista[i + 1], lista[high]) = (lista[high], lista[i + 1]) return i + 1 """ Esta implementación de quicksort fue extraida de geeksforgeeks.org Oct 4, 2022 https://www.geeksforgeeks.org/quick-sort/ """ def quickSort(lista, low, high): #definan el algoritmo de ordenamiento quicksort if low < high: pi = partition(lista, low, high) quickSort(lista, low, pi - 1) quickSort(lista, pi + 1, high) return lista def shellSort(lista): #definan el algoritmo de ordenamiento shellsort return lista def heapify(lista, n, i): largest = i #largest = raíz l = 2 * i * 1 #left r = 2 * i + 2 #right #Ver is existe una rama isquierda y si es mayor a la raíz if l < n and lista[i] < lista[l]: largest = l #Ver is existe una rama derecha y si es mayor a la raíz if r < n and lista[largest] < lista[r]: largest = r #Se cambia la raíz si fuese necesario if largest != i: (lista[i], lista[largest]) = (lista[largest], lista[i]) #swap #Se llama heapify en la raíz nueva heapify(lista, n, largest) def merge(firstHalf, secondHalf): mergeList = [] # Mientras ninguna lista este vacia while (len(firstHalf) != 0) and (len(secondHalf) != 0): if firstHalf[0] > secondHalf[0]: n = secondHalf.pop(0) mergeList.append(n) else: n = firstHalf.pop(0) mergeList.append(n) # Si la primera mitad no esta vacia, se anade a la lista merge while len(firstHalf) != 0: mergeList.append(firstHalf[0]) firstHalf.pop(0) # Si la segunda mitad no esta vacia, se anade a la lista merge while len(secondHalf) != 0: mergeList.append(secondHalf[0]) secondHalf.pop(0) return mergeList maxValor=1000 #define el valor maximo de los elementos de la lista largoLista=1000 #define el largo de las listas a ordenar veces=100 #define las veces que se va a hacer el ordenamiento acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort for i in range(veces): mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar heaplista=list(mergelista) quicklista=list(mergelista) searchlista=list(mergelista) t1 = time.time() #seteamos el tiempo al empezar mergeSort(mergelista) #ejecutamos el algoritmo mergeSort acumulaMerge+=time.time()-t1 #acumulamos el tiempo de ejecucion t1 = time.time() #seteamos el tiempo al empezar heapSort(heaplista) #ejecutamos el algoritmo heapSort acumulaHeap+=time.time()-t1 #acumulamos el tiempo de ejecucion t1 = time.time() #seteamos el tiempo al empezar quickSort(quicklista, 0, len(quicklista) - 1) #ejecutamos el algoritmo quickSort acumulaQuick+=time.time()-t1 #acumulamos el tiempo de ejecucion t1 = time.time() #seteamos el tiempo al empezar shellSort(searchlista) #ejecutamos el algoritmo shellSort acumulaShell+=time.time()-t1 #acumulamos el tiempo de ejecucion #imprimos los resultados print("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)) print("MergeSort " + str(acumulaMerge/veces) + " segundos") print("HeapSort " + str(acumulaHeap/veces) + " segundos") print("QuickSort " + str(acumulaQuick/veces) + " segundos") print("ShellSort " + str(acumulaShell/veces) + " segundos")