Pārlūkot izejas kodu

Delete 'sorting.py'

removing from project branch
orlando.medina4 2 gadus atpakaļ
vecāks
revīzija
5cb64f1ce9
1 mainītis faili ar 0 papildinājumiem un 181 dzēšanām
  1. 0
    181
      sorting.py

+ 0
- 181
sorting.py Parādīt failu

@@ -1,181 +0,0 @@
1
-"""
2
-Carlos J Corrada Bravo
3
-Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
4
-La variable maxValor define el valor maximo de los elementos de la lista
5
-La variable largoLista define el largo de las listas a ordenar
6
-La variable veces define las veces que se va a hacer el ordenamiento
7
-La variable veces define las veces que se va a hacer el ordenamiento
8
-Al final se imprimen los promedios de cada algortimo
9
-"""
10
-from random import randint
11
-import time
12
-from merge import merge
13
-import heapq
14
-
15
-# Python program for implementation of MergeSort
16
-
17
-# Merges two subarrays of arr[].
18
-# First subarray is arr[l..m]
19
-# Second subarray is arr[m+1..r]
20
-
21
-def mergeSort(lista, l, r):
22
-	if l < r:
23
-
24
-	# Same as (l+r)//2, but avoids overflow for
25
-	# large l and h
26
-	m = l+(r-l)//2
27
-
28
-	# Sort first and second halves
29
-	mergeSort(lista, l, m)
30
-	mergeSort(lista, m+1, r)
31
-	merge(lista, l, m, r)
32
-
33
-''' Luis Andrés López Mañán
34
-    Program written by hand as a draft on 09/19/2022
35
-    Credit goes to GeeksForGeeks
36
-    Link: https://www.geeksforgeeks.org/python-program-for-heap-sort/
37
-    Last access on 09/19/2022
38
-    Program wrriten and edited on 10/10/2022
39
-'''
40
-
41
-# This function heapifies a subtree rooted at an index
42
-# i. Also, n is the size of a heap and arr is array.
43
-
44
-def heapify(lista, n, i):
45
-
46
-		# largest is root for now
47
-		largest = i
48
-
49
-		# left child of root
50
-		l = 2 * i + 1
51
-
52
-		# right child of root
53
-		r = 2 * i + 2
54
-
55
-		# Checks if root has a left child and is greater than root
56
-		if l < n and lista[i] < lista[l] :
57
-			largest = l
58
-
59
-		# Checks if root has a right child and is greater than root
60
-		if r < n and lista[largest] < lista[r]:
61
-			largest = r
62
-
63
-		# If necessary, this changes root by swapping values
64
-    	if largest != i:
65
-        	lista[i], lista[largest] = lista[largest], lista[i]
66
-
67
-		# This heapifies the root repeatedly
68
-		heapify(lista, n, largest)
69
-
70
-def heapSort(lista):
71
-
72
-		n = len(lista)
73
-		n2 = (n // 2) - 1
74
-		nMinus = n - 1
75
-
76
-		for i in range(n2, -1, -1):
77
-			heapify(lista, n, i)
78
-
79
-		for i in range(nMinus, -1, -1):
80
-			lista[i], lista[0] = lista[0], lista[i]
81
-			heapify(lista, i, 0)
82
-
83
-		return lista
84
-
85
-def quickSort(lista):
86
-	#definan el algoritmo de ordenamiento quicksort
87
-	elements = len(lista)
88
-    
89
-    #Base case
90
-	if elements < 2:
91
-		return lista
92
-    
93
-	current_position = 0 #Position of the partitioning element
94
-
95
-	for i in range(1, elements): #Partitioning loop
96
-		if lista[i] <= lista[0]:
97
-			current_position += 1
98
-			temp = lista[i]
99
-			lista[i] = lista[current_position]
100
-			lista[current_position] = temp
101
-
102
-	temp = lista[0]
103
-	lista[0] = lista[current_position] 
104
-	lista[current_position] = temp #Brings pivot to it's appropriate position
105
-    
106
-	left = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot
107
-	right = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot
108
-
109
-	lista = left + [lista[current_position]] + right #Merging everything together
110
-	return lista
111
-
112
-''' 
113
-	This algorithm was taken from: https://www.programiz.com/dsa/shell-sort 
114
-	and was adapted in order to work for this assigment.
115
-'''
116
-
117
-def shellSort(lista):
118
-	#definan el algoritmo de ordenamiento shellsort
119
-
120
-	# determening the size of the list and calculates the gap value. 
121
-	n = len(lista)
122
-	gap = n // 2
123
-
124
-	# this algorithm will run until gap reaches 1 
125
-	while gap > 0:
126
-		for i in range(gap, n):
127
-			temp = lista[i] # storing all items from the list into temp 
128
-			j = i 
129
-
130
-			# compares the number in temp with the 0th possition (start of list)
131
-			# if temp is larger than the first element then we swap them
132
-			while j >= gap and lista[j - gap] > temp: 
133
-				lista[j] = lista[j - gap]
134
-				j -= gap
135
-
136
-			lista[j] = temp
137
-		gap = gap // 2 # decreases the gap to continue the loop 
138
-
139
-	return lista
140
-
141
-maxValor=1000 	#define el valor maximo de los elementos de la lista
142
-largoLista=1000 #define el largo de las listas a ordenar
143
-veces=100 		#define las veces que se va a hacer el ordenamiento 
144
-
145
-acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
146
-acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
147
-acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
148
-acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
149
-
150
-for i in range(veces):
151
-	mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
152
-	heaplista=list(mergelista)
153
-	quicklista=list(mergelista)
154
-	searchlista=list(mergelista)
155
-
156
-	t1 = time.process_time()					#tomamos el tiempo inicial
157
-	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
158
-	acumulaMerge+=time.process_time() - t1		#acumulamos el tiempo de ejecucion
159
-	#print(mergelista)							#desplegamos la lista
160
-
161
-	t1 = time.process_time()				#tomamos el tiempo inicial
162
-	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
163
-	acumulaHeap+=time.process_time() - t1 	#acumulamos el tiempo de ejecucion
164
-	#print(heaplista)						#desplegamos la lista
165
-
166
-	t1 = time.process_time()				#tomamos el tiempo inicial
167
-	quickresult = quickSort(quicklista)					#ejecutamos el algoritmo quickSort
168
-	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
169
-	print(quicklista)						#desplegamos la lista
170
-
171
-	t1 = time.process_time()				#tomamos el tiempo inicial
172
-	shellSort(searchlista)					#ejecutamos el algoritmo shellSort
173
-	acumulaShell+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
174
-	print(searchlista)						#desplegamos la lista
175
-
176
-#imprimos los resultados
177
-print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
178
-print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
179
-print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
180
-print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
181
-print ("ShellSort " + str(acumulaShell/veces) + " segundos")