""" Carlos J Corrada Bravo Este programa calcula el promedio de tiempo de ejecución 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 def mergeSort(lista): #definan el algoritmo de ordenamiento mergesort return lista # Tomada de: https://www.geeksforgeeks.org/heap-sort/ # To heapify subtree rooted at index i. # n is size of heap def heapify(arr, n, i): largest = i # Initialize largest as root l = 2 * i + 1 # left = 2*i + 1 r = 2 * i + 2 # right = 2*i + 2 # See if left child of root exists and is # greater than root if l < n and arr[largest] < arr[l]: largest = l # See if right child of root exists and is # greater than root if r < n and arr[largest] < arr[r]: largest = r # Change root, if needed if largest != i: arr[i], arr[largest] = arr[largest], arr[i] # swap # Heapify the root. heapify(arr, n, largest) # Tomada de: https://www.geeksforgeeks.org/heap-sort/ def heapSort(lista): #definan el algoritmo de ordenamiento heapsort n = len(lista) # Build a maxheap. for i in range(n//2 - 1, -1, -1): heapify(lista, n, i) # One by one extract elements for i in range(n-1, 0, -1): lista[i], lista[0] = lista[0], lista[i] # swap heapify(lista, i, 0) return lista def quickSort(lista): #definan el algoritmo de ordenamiento quicksort return lista def shellSort(lista): #definan el algoritmo de ordenamiento shellsort return lista 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): lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar listaMerge = lista[:] listaHeap = lista[:] listaQuick = lista[:] listaShell = lista[:] t1 = time.process_time() #seteamos el tiempo al empezar mergeSort(listaMerge) #ejecutamos el algoritmo mergeSort acumulaMerge+=time.process_time()-t1 #acumulamos el tiempo de ejecucion t1 = time.process_time() #seteamos el tiempo al empezar heapSort(listaHeap) #ejecutamos el algoritmo heapSort acumulaHeap+=time.process_time()-t1 #acumulamos el tiempo de ejecucion t1 = time.process_time() #seteamos el tiempo al empezar quickSort(listaQuick) #ejecutamos el algoritmo quickSort acumulaQuick+=time.process_time()-t1 #acumulamos el tiempo de ejecucion t1 = time.process_time() #seteamos el tiempo al empezar shellSort(listaShell) #ejecutamos el algoritmo shellSort acumulaShell+=time.process_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")