Pārlūkot izejas kodu

Merge branch 'sortingHeap' of https://git.ccom.uprrp.edu/CCOM4030/JeLuOrRi into sortingFinal

luislopez66 2 gadus atpakaļ
vecāks
revīzija
5d4c573d63
2 mainītis faili ar 66 papildinājumiem un 0 dzēšanām
  1. 37
    0
      heap.py
  2. 29
    0
      sorting.py

+ 37
- 0
heap.py Parādīt failu

@@ -0,0 +1,37 @@
1
+''' Luis Andrés López Mañán
2
+    Program written by hand as a draft on 09/19/2022
3
+    Credit goes to GeeksForGeeks
4
+    Link: https://www.geeksforgeeks.org/python-program-for-heap-sort/
5
+    Last access on 09/19/2022
6
+    Program wrriten and edited on 10/10/2022
7
+'''
8
+
9
+# This function heapifies a subtree rooted at an index
10
+# i. Also, n is the size of a heap and arr is array.
11
+
12
+def heapify(lista, n, i):
13
+		# largest is root for now
14
+		largest = i
15
+
16
+		# left child of root
17
+		left = 2 * i + 1
18
+
19
+		# right child of root
20
+		right = 2 * i + 2
21
+
22
+		# Checks if root has a left child and is greater
23
+		# than root
24
+		if l < n and lista[i] < lista[l] :
25
+			largest = l
26
+
27
+		# Checks if root has a right child and is greater
28
+		# than root
29
+		if r < n and lista[largest] < lista[r]:
30
+			largest = r
31
+
32
+		# If necessary, this changes root by swapping va-
33
+		# lues
34
+		lista[i], lista[largest] = lista[largest], lista[i]
35
+
36
+		# This heapifies the root repeatedly
37
+		heapify(lista, n, largest)

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

@@ -3,12 +3,20 @@ Carlos J Corrada Bravo
3 3
 Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
4 4
 La variable maxValor define el valor maximo de los elementos de la lista
5 5
 La variable largoLista define el largo de las listas a ordenar
6
+<<<<<<< HEAD
6 7
 La variable veces define las veces que se va a hacer el ordenamiento 
8
+=======
9
+La variable veces define las veces que se va a hacer el ordenamiento
10
+>>>>>>> a2416a1f1584bddc699e3c6dfe5efdd25f2ac172
7 11
 Al final se imprimen los promedios de cada algortimo
8 12
 """
9 13
 from random import randint
10 14
 import time
15
+<<<<<<< HEAD
11 16
 from merge import merge
17
+=======
18
+from heap import heap
19
+>>>>>>> a2416a1f1584bddc699e3c6dfe5efdd25f2ac172
12 20
 
13 21
 # Python program for implementation of MergeSort
14 22
 
@@ -16,6 +24,7 @@ from merge import merge
16 24
 # First subarray is arr[l..m]
17 25
 # Second subarray is arr[m+1..r]
18 26
 
27
+<<<<<<< HEAD
19 28
 def mergeSort(lista, l, r):
20 29
 	if l < r:
21 30
 
@@ -30,6 +39,23 @@ def mergeSort(lista, l, r):
30 39
 
31 40
 def heapSort(lista):
32 41
 	#definan el algoritmo de ordenamiento heapsort
42
+=======
43
+def mergeSort(lista):
44
+	# definan el algoritmo de ordenamiento mergesort
45
+	return lista
46
+
47
+def heapSort(lista):
48
+	
49
+	n = len(lista)
50
+	h1 = (n // 2) - 1
51
+	for i in range(h1, -1, -1):
52
+		heapify(lista, n, i)
53
+
54
+	for i in range(h1, -1, -1):
55
+		lista[i], lista[0] = lista[0], lista[i]
56
+		heapify(lista, i, 0)
57
+
58
+>>>>>>> a2416a1f1584bddc699e3c6dfe5efdd25f2ac172
33 59
 	return lista
34 60
 
35 61
 def quickSort(lista):
@@ -81,4 +107,7 @@ print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
81 107
 print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
82 108
 print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
83 109
 print ("ShellSort " + str(acumulaShell/veces) + " segundos")
110
+<<<<<<< HEAD
84 111
 
112
+=======
113
+>>>>>>> a2416a1f1584bddc699e3c6dfe5efdd25f2ac172