Browse Source

Adding cambios finales.

diegoaperez 2 years ago
parent
commit
e841eae30d
1 changed files with 84 additions and 1 deletions
  1. 84
    1
      sorting.py

+ 84
- 1
sorting.py View File

@@ -11,11 +11,71 @@ from random import randint
11 11
 import time
12 12
 
13 13
 def mergeSort(lista):
14
+        #Camila Vazquez Rodriguez
14 15
 	#definan el algoritmo de ordenamiento mergesort
15
-	return lista
16
+        if len(lista) > 1:
17
+                mid = len(lista)//2
18
+                L = lista[:mid]
19
+                R = lista[mid:]
20
+
21
+                mergeSort(L)
22
+                mergeSort(R)
23
+
24
+                i = 0
25
+                j = 0
26
+                k = 0
27
+
28
+        while i < len(L) and j < len(R):
29
+                if L[i] <= R[j]:
30
+                        lista[k] = L[i]
31
+                        i += 1
32
+                else:
33
+                        lista[k] = R[j]
34
+                        j += 1
35
+                k += 1
36
+
37
+        while i < len(L):
38
+                lista[k] = L[i]
39
+                i += 1
40
+                k += 1
41
+
42
+        while j < len(R):
43
+                lista[k] = R[j]
44
+                j += 1
45
+                k += 1
46
+
47
+        return lista
16 48
 
17 49
 def heapSort(lista):
18 50
 	#definan el algoritmo de ordenamiento heapsort
51
+	"""
52
+	Carlos Hernández
53
+	Implementación de heapSort.
54
+	"""
55
+	def max_heapify(lista, idx, heap_size):
56
+		"""Convertir el nodo `idx` y sus descendientes en un max heap."""
57
+		left_idx = 2 * idx + 1
58
+		right_idx = 2 * idx + 2
59
+		largestval_idx = idx
60
+		if left_idx < heap_size and lista[idx] < lista[left_idx]:
61
+			largestval_idx = left_idx
62
+		if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
63
+			largestval_idx = right_idx
64
+		if largestval_idx != idx:
65
+			lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
66
+			max_heapify(lista, largestval_idx, heap_size)
67
+
68
+	def build_max_heap(lista, heap_size):
69
+		"""Construir un max heap the un heap dado."""
70
+		for idx in range((heap_size - 1) // 2, -1, -1):
71
+			max_heapify(lista, idx, heap_size)
72
+
73
+	heap_size = len(lista)
74
+	build_max_heap(lista, heap_size)
75
+	for idx in range(len(lista) - 1, 0, -1):
76
+		lista[0], lista[idx] = lista[idx], lista[0]
77
+		heap_size -= 1
78
+		max_heapify(lista, 0, heap_size)
19 79
 	return lista
20 80
 
21 81
 def quickSort(lista):
@@ -45,6 +105,29 @@ def quickSort(lista):
45 105
 
46 106
 def shellSort(lista):
47 107
 	#definan el algoritmo de ordenamiento shellsort
108
+
109
+	#El algoritmo compara elementos de una lista
110
+	#que tienen una distancia fija entre ellos, la
111
+	#differencia en distancia se minimiza por cada
112
+	#iterazion del algoritmo
113
+
114
+	num = int(len(lista))
115
+	dif = int(num/2)
116
+
117
+	while dif > 0:
118
+		for i in range(int(dif),int(num)):
119
+			a = lista[i]
120
+			j = i
121
+
122
+			#por cada intervalo se reorganizara los elementos
123
+			#si se cumple la declaracion dentro del while loop
124
+			while j >= dif and lista[int(j - dif)] > a:
125
+				lista[int(j)] = lista[int(j - dif)]
126
+				j -= dif
127
+
128
+			lista[int(j)] = a
129
+		dif/= 2
130
+
48 131
 	return lista
49 132
 
50 133
 maxValor=1000 	#define el valor maximo de los elementos de la lista