Browse Source

Fixed comments

Luis Ortiz 1 year ago
parent
commit
fff3694ec5
1 changed files with 72 additions and 5 deletions
  1. 72
    5
      sorting.py

+ 72
- 5
sorting.py View File

6
 La variable veces define las veces que se va a hacer el ordenamiento
6
 La variable veces define las veces que se va a hacer el ordenamiento
7
 Al final se imprimen los promedios de cada algortimo
7
 Al final se imprimen los promedios de cada algortimo
8
 """
8
 """
9
+
9
 from heapq import merge
10
 from heapq import merge
11
+
12
+from lib2to3.pgen2.token import LESS
13
+
10
 from random import randint
14
 from random import randint
11
 import time
15
 import time
12
 
16
 
14
 def mergeSort(lista):
18
 def mergeSort(lista):
15
 	#definan el algoritmo de ordenamiento mergesort
19
 	#definan el algoritmo de ordenamiento mergesort
16
 	# Carla Ramos Bezares
20
 	# Carla Ramos Bezares
17
-	# Para realizar este código leí las explicaciones e implementaciones que ofrecen
21
+	# Para realizar este codigo lei las explicaciones e implementaciones que ofrecen
18
 	# GeeksforGeeks y Progamiz
22
 	# GeeksforGeeks y Progamiz
19
 
23
 
20
 	# check if the array has more than one element
24
 	# check if the array has more than one element
23
         middle = len(lista)//2
27
         middle = len(lista)//2
24
         leftHalf = lista[:middle]
28
         leftHalf = lista[:middle]
25
         rightHalf = lista[middle:]
29
         rightHalf = lista[middle:]
26
-        
30
+
27
         mergeSort(leftHalf)
31
         mergeSort(leftHalf)
28
         mergeSort(rightHalf)
32
         mergeSort(rightHalf)
29
         # declare pointers
33
         # declare pointers
40
             else:
44
             else:
41
                 lista[k] = rightHalf[j]
45
                 lista[k] = rightHalf[j]
42
                 j = j + 1
46
                 j = j + 1
43
-           
47
+
44
             k = k + 1
48
             k = k + 1
45
 
49
 
46
         # continue updating array grabbing any elements that were left
50
         # continue updating array grabbing any elements that were left
58
 
62
 
59
 def heapSort(lista):
63
 def heapSort(lista):
60
 	#definan el algoritmo de ordenamiento heapsort
64
 	#definan el algoritmo de ordenamiento heapsort
61
-	return lista
65
+    #Andrea V. Nieves
66
+
67
+
68
+    #function def
69
+    def heapify(lista, n, i):
70
+       biggest = i
71
+       left = 2*i + 1
72
+       right = 2*i + 2
73
+
74
+
75
+       if left < n and lista[left] > lista[i]:
76
+           biggest = left
77
+       else:
78
+            biggest = i
79
+
80
+       if right< n and lista[right] > lista[biggest]:
81
+           biggest = right
82
+
83
+       if biggest != i:
84
+           lista[i], lista[biggest] = lista[biggest], lista[i]
85
+           heapify(lista,n,biggest)
86
+
87
+
88
+
89
+
90
+       #actual call
91
+    n = len(lista)
92
+
93
+    for i in range(n // 2 - 1, -1, -1):
94
+        heapify(lista, n, i)
95
+
96
+    for i in range(n - 1, 0, -1):
97
+        lista[i], lista[0] = lista[0], lista[i]
98
+        heapify(lista, i, 0)
99
+
100
+
101
+    return lista
62
 
102
 
63
 def quickSort(lista):
103
 def quickSort(lista):
64
 	#definan el algoritmo de ordenamiento quicksort
104
 	#definan el algoritmo de ordenamiento quicksort
65
-	return lista
105
+    #Juan F. Hernandez
106
+    # Para este codigo se utilizo referencia de stackoverflow
107
+
108
+    #define arreglos a utilizar
109
+
110
+	lowerArray = []
111
+	equalArray = []
112
+	maxArray = []
113
+
114
+
115
+    #organiza los elementos utilizando el pivote para ponerlos en orden
116
+
117
+	if len(lista) > 1:
118
+		pivot = lista[0]
119
+		for x in lista:
120
+			if x < pivot:
121
+				lowerArray.append(x)
122
+			elif x == pivot:
123
+				equalArray.append(x)
124
+			elif x > pivot:
125
+				maxArray.append(x)
126
+
127
+        #concatena arreglos en orden
128
+
129
+		return quickSort(lowerArray)+equalArray+quickSort(maxArray)
130
+
131
+	else:
132
+		return lista
66
 
133
 
67
 def shellSort(lista):
134
 def shellSort(lista):
68
 	#definan el algoritmo de ordenamiento shellsort
135
 	#definan el algoritmo de ordenamiento shellsort