Browse Source

Modified sorting.py and heap.py

luislopez66 2 years ago
parent
commit
372ba733e3
2 changed files with 15 additions and 37 deletions
  1. 15
    16
      heap.py
  2. 0
    21
      sorting.py

+ 15
- 16
heap.py View File

12
 # i. Also, n is the size of a heap and arr is array.
12
 # i. Also, n is the size of a heap and arr is array.
13
 
13
 
14
 def heapify(lista, n, i):
14
 def heapify(lista, n, i):
15
-		# largest is root for now
16
-		largest = i
15
+    # largest is root for now
16
+	largest = i
17
 
17
 
18
-		# left child of root
19
-		left = 2 * i + 1
18
+	# left child of root
19
+	l = 2 * i + 1
20
 
20
 
21
-		# right child of root
22
-		right = 2 * i + 2
21
+	# right child of root
22
+	r = 2 * i + 2
23
 
23
 
24
-		# Checks if root has a left child and is greater
25
-		# than root
26
-		if l < n and lista[i] < lista[l] :
27
-			largest = l
24
+	# Checks if root has a left child and is greater
25
+	# than root
26
+	if l < n and lista[i] < lista[l] :
27
+		largest = l
28
 
28
 
29
-		# Checks if root has a right child and is greater
30
-		# than root
31
-		if r < n and lista[largest] < lista[r]:
32
-			largest = r
29
+	# Checks if root has a right child and is greater
30
+	# than root
31
+	if r < n and lista[largest] < lista[r]:
32
+		largest = r
33
 
33
 
34
-		# If necessary, this changes root by swapping va-
35
-		# lues
34
+	# If necessary, this changes root by swapping values, then heapifies
36
 		lista[i], lista[largest] = lista[largest], lista[i]
35
 		lista[i], lista[largest] = lista[largest], lista[i]
37
 
36
 
38
 		heapify(lista, n, largest)
37
 		heapify(lista, n, largest)

+ 0
- 21
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
 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
8
 Al final se imprimen los promedios de cada algortimo
9
-<<<<<<< HEAD
10
 '''
9
 '''
11
 
10
 
12
-=======
13
-"""
14
->>>>>>> sortingHeap
15
 import time
11
 import time
16
 from random import randint
12
 from random import randint
17
 
13
 
18
 import heap
14
 import heap
19
-<<<<<<< HEAD
20
 from merge import merge
15
 from merge import merge
21
-=======
22
->>>>>>> sortingHeap
23
 
16
 
24
 # Python program for implementation of MergeSort
17
 # Python program for implementation of MergeSort
25
 
18
 
28
 # Second subarray is arr[m+1..r]
21
 # Second subarray is arr[m+1..r]
29
 
22
 
30
 def mergeSort(lista, l, r):
23
 def mergeSort(lista, l, r):
31
-<<<<<<< HEAD
32
 	if l < r:
24
 	if l < r:
33
       # Same as (l+r)//2, but avoids overflow for
25
       # Same as (l+r)//2, but avoids overflow for
34
       # large l and h
26
       # large l and h
47
 '''
39
 '''
48
 
40
 
49
 # Python program for implementation of heap Sort (Part II)
41
 # Python program for implementation of heap Sort (Part II)
50
-=======
51
-	# definan el algoritmo de ordenamiento mergesort
52
-	return lista
53
->>>>>>> sortingHeap
54
 
42
 
55
 def heapSort(lista):
43
 def heapSort(lista):
56
 	# Se busca el tamaño de la lista
44
 	# Se busca el tamaño de la lista
64
 	h1 = (n // 2) - 1
52
 	h1 = (n // 2) - 1
65
 	for i in range(h1, -1, -1):
53
 	for i in range(h1, -1, -1):
66
 		heap.heapify(lista, n, i)
54
 		heap.heapify(lista, n, i)
67
-<<<<<<< HEAD
68
   
55
   
69
 	# Se extrae los elementos uno a uno
56
 	# Se extrae los elementos uno a uno
70
 	h2 = n - 1
57
 	h2 = n - 1
73
 		lista[i], lista[0] = lista[0], lista[i]
60
 		lista[i], lista[0] = lista[0], lista[i]
74
 		heap.heapify(lista, 0, i)
61
 		heap.heapify(lista, 0, i)
75
   
62
   
76
-=======
77
-
78
-	h2 = n - 1
79
-	for i in range(h2, 0, -1):
80
-		lista[i], lista[0] = lista[0], lista[i]
81
-		heap.heapify(lista, 0, i)
82
-
83
->>>>>>> sortingHeap
84
 	return lista
63
 	return lista
85
 
64
 
86
 def quickSort(lista):
65
 def quickSort(lista):