Bläddra i källkod

Modified sorting.py and heap.py

luislopez66 2 år sedan
förälder
incheckning
372ba733e3
2 ändrade filer med 15 tillägg och 37 borttagningar
  1. 15
    16
      heap.py
  2. 0
    21
      sorting.py

+ 15
- 16
heap.py Visa fil

@@ -12,27 +12,26 @@
12 12
 # i. Also, n is the size of a heap and arr is array.
13 13
 
14 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 35
 		lista[i], lista[largest] = lista[largest], lista[i]
37 36
 
38 37
 		heapify(lista, n, largest)

+ 0
- 21
sorting.py Visa fil

@@ -6,20 +6,13 @@ La variable largoLista define el largo de las listas a ordenar
6 6
 La variable veces define las veces que se va a hacer el ordenamiento
7 7
 La variable veces define las veces que se va a hacer el ordenamiento
8 8
 Al final se imprimen los promedios de cada algortimo
9
-<<<<<<< HEAD
10 9
 '''
11 10
 
12
-=======
13
-"""
14
->>>>>>> sortingHeap
15 11
 import time
16 12
 from random import randint
17 13
 
18 14
 import heap
19
-<<<<<<< HEAD
20 15
 from merge import merge
21
-=======
22
->>>>>>> sortingHeap
23 16
 
24 17
 # Python program for implementation of MergeSort
25 18
 
@@ -28,7 +21,6 @@ from merge import merge
28 21
 # Second subarray is arr[m+1..r]
29 22
 
30 23
 def mergeSort(lista, l, r):
31
-<<<<<<< HEAD
32 24
 	if l < r:
33 25
       # Same as (l+r)//2, but avoids overflow for
34 26
       # large l and h
@@ -47,10 +39,6 @@ def mergeSort(lista, l, r):
47 39
 '''
48 40
 
49 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 43
 def heapSort(lista):
56 44
 	# Se busca el tamaño de la lista
@@ -64,7 +52,6 @@ def heapSort(lista):
64 52
 	h1 = (n // 2) - 1
65 53
 	for i in range(h1, -1, -1):
66 54
 		heap.heapify(lista, n, i)
67
-<<<<<<< HEAD
68 55
   
69 56
 	# Se extrae los elementos uno a uno
70 57
 	h2 = n - 1
@@ -73,14 +60,6 @@ def heapSort(lista):
73 60
 		lista[i], lista[0] = lista[0], lista[i]
74 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 63
 	return lista
85 64
 
86 65
 def quickSort(lista):