""" 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 def heapSort(lista): #definan el algoritmo de ordenamiento heapsort return lista #Se le da credito al programador de la funcion al final del codigo def quickSort(lista): #definan el algoritmo de ordenamiento quicksort return lista def shellSort(lista): # Subarrays are sorted according to intervals # After each set of subarrays is sorted, interval value is updated and process repeats # Function stops once iteration with interval = 1 has executed # print(lista) interval = len(lista) // 2 while interval > 0: # Process repeats for each value between 1 -> interval for i in range(0, interval): # Process repeats as long as the current value being considered is greater than the value to its left # Being greater than the value to its left means that it is not in the correct location j = i while j + interval < len(lista): if lista[j] > lista[j + interval]: # Swapping values so that smaller value is to the left temp = lista[j] lista[j] = lista[j + interval] lista[j + interval] = temp # print(lista) n = j # Continue comparing value that was swapped left to other values to the left to make sure it is placed in the correct location while n - interval >= 0 and lista[n] < lista[n - interval]: # Swapping values so that smaller value is to the left temp = lista[n] lista[n] = lista[n - interval] lista[n - interval] = temp n -= interval # print(lista) # Update index to continue comparison with the next value in the sub array j += interval interval //= 2 # print(lista) 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 # t1 = time.clock() #seteamos el tiempo al empezar # mergeSort(lista) #ejecutamos el algoritmo mergeSort # acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion # t1 = time.clock() #seteamos el tiempo al empezar # heapSort(lista) #ejecutamos el algoritmo heapSort # acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion # # t1 = time.clock() #seteamos el tiempo al empezar # quickSort(lista) #ejecutamos el algoritmo quickSort # acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion t1 = time.perf_counter() #seteamos el tiempo al empezar shellSort(lista) #ejecutamos el algoritmo shellSort acumulaShell+=time.perf_counter()-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")