""" 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 heapq import merge from lib2to3.pgen2.token import LESS from random import randint import time def mergeSort(lista): #definan el algoritmo de ordenamiento mergesort # Carla Ramos Bezares # Para realizar este codigo lei las explicaciones e implementaciones que ofrecen # GeeksforGeeks y Progamiz # check if the array has more than one element if len(lista) > 1: # divide the array in two halves middle = len(lista)//2 leftHalf = lista[:middle] rightHalf = lista[middle:] mergeSort(leftHalf) mergeSort(rightHalf) # declare pointers i = j = k = 0 # using our "smaller" arrays, place elements in the correct position of our array while i < len(leftHalf) and j < len(rightHalf): if leftHalf[i] < rightHalf[j]: lista[k] = leftHalf[i] i = i + 1 else: lista[k] = rightHalf[j] j = j + 1 k = k + 1 # continue updating array grabbing any elements that were left while i < len(leftHalf): lista[k] = leftHalf[i] i = i + 1 k = k + 1 while j < len(rightHalf): lista[k] = rightHalf[j] j = j + 1 k = k + 1 return lista def heapSort(lista): #definan el algoritmo de ordenamiento heapsort #Andrea V. Nieves #function def def heapify(lista, n, i): biggest = i left = 2*i + 1 right = 2*i + 2 if left < n and lista[left] > lista[i]: biggest = left else: biggest = i if right< n and lista[right] > lista[biggest]: biggest = right if biggest != i: lista[i], lista[biggest] = lista[biggest], lista[i] heapify(lista,n,biggest) #actual call n = len(lista) for i in range(n // 2 - 1, -1, -1): heapify(lista, n, i) for i in range(n - 1, 0, -1): lista[i], lista[0] = lista[0], lista[i] heapify(lista, i, 0) return lista def quickSort(lista): #definan el algoritmo de ordenamiento quicksort #Juan F. Hernandez # Para este codigo se utilizo referencia de stackoverflow #define arreglos a utilizar lowerArray = [] equalArray = [] maxArray = [] #organiza los elementos utilizando el pivote para ponerlos en orden if len(lista) > 1: pivot = lista[0] for x in lista: if x < pivot: lowerArray.append(x) elif x == pivot: equalArray.append(x) elif x > pivot: maxArray.append(x) #concatena arreglos en orden return quickSort(lowerArray)+equalArray+quickSort(maxArray) else: return lista def shellSort(lista): #definan el algoritmo de ordenamiento shellsort #Luis E. Ortiz Cotto #Este codigo tiene su base de GeeksforGeeks distance = len(lista) / 2 #Coge la distancia de la mitad de la lista while distance > 0: for i in range(distance, len(lista)): #Empieza a ordenar haciendo un insertion sort para la distancia tmp = lista[i] #Guarda temporeramente el valor que esta en la posicion i que se va a cambiar j = i #Tiene el valor que va al momento #Empieza a ordernar los elementos hasta que llegue a la localizacion donde esta el valor temporero while j >= distance and lista[j - distance] > tmp: lista[j] = lista[j - distance] j -= distance #Poner el valor temporero en el su posicion correcta lista[j] = tmp distance /= 2 #Coge la mitad otra vez de la que ya esta 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): 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.clock() #seteamos el tiempo al empezar mergeSort(mergelista) #ejecutamos el algoritmo mergeSort acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion t1 = time.clock() #seteamos el tiempo al empezar heapSort(heaplista) #ejecutamos el algoritmo heapSort acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion t1 = time.clock() #seteamos el tiempo al empezar quickSort(quicklista) #ejecutamos el algoritmo quickSort acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion t1 = time.clock() #seteamos el tiempo al empezar shellSort(searchlista) #ejecutamos el algoritmo shellSort acumulaShell+=time.clock()-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"