Browse Source

Merge branch 'matteing-heapSort' of CCOM4030/EdElSe into master

sergio.mattei 1 year ago
parent
commit
fc283bdcb4
1 changed files with 21 additions and 2 deletions
  1. 21
    2
      sorting.py

+ 21
- 2
sorting.py View File

@@ -13,9 +13,28 @@ def mergeSort(lista):
13 13
 	#definan el algoritmo de ordenamiento mergesort
14 14
 	return lista
15 15
 
16
+# https://en.wikipedia.org/wiki/Heapsort
17
+def heapify(lista, n, i):
18
+	largest = i
19
+	l = 2 * i + 1
20
+	r = 2 * i + 2
21
+	if l < n and lista[i] < lista[l]:
22
+		largest = l
23
+	if r < n and lista[largest] < lista[r]:
24
+		largest = r
25
+	if largest != i:
26
+		lista[i], lista[largest] = lista[largest], lista[i]
27
+		heapify(lista, n, largest)
28
+
16 29
 def heapSort(lista):
17
-	#definan el algoritmo de ordenamiento heapsort
18
-	return lista
30
+	n = len(lista)
31
+
32
+	for i in range(n//2, -1, -1):
33
+		heapify(lista, n, i)
34
+
35
+	for i in range(n-1, 0, -1):
36
+		lista[i], lista[0] = lista[0], lista[i]
37
+		heapify(lista, i, 0)
19 38
 
20 39
 def quickSort(lista):
21 40
 	#definan el algoritmo de ordenamiento quicksort