Sfoglia il codice sorgente

Modified heap.py and sorting.py

luislopez66 2 anni fa
parent
commit
832ea0d1ac
4 ha cambiato i file con 44 aggiunte e 34 eliminazioni
  1. 24
    15
      .vscode/PythonImportHelper-v2-Completion.json
  2. BIN
      __pycache__/heap.cpython-310.pyc
  3. 16
    16
      heap.py
  4. 4
    3
      sorting.py

+ 24
- 15
.vscode/PythonImportHelper-v2-Completion.json Vedi File

@@ -30,52 +30,52 @@
30 30
         "kind": 2,
31 31
         "importPath": "heap",
32 32
         "description": "heap",
33
-        "peekOfCode": "def heapify(lista, n, i):\n\t\t# largest is root for now\n\t\tlargest = i\n\t\t# left child of root\n\t\tleft = 2 * i + 1\n\t\t# right child of root\n\t\tright = 2 * i + 2\n\t\t# Checks if root has a left child and is greater\n\t\t# than root\n\t\tif l < n and lista[i] < lista[l] :",
33
+        "peekOfCode": "def heapify(lista, n, i):\n\t# largest is root for now\n\tlargest = i\n\t# left child of root\n\tl = 2 * i + 1\n\t# right child of root\n\tr = 2 * i + 2\n\t# Checks if root has a left child and is greater\n\t# than root\n\tif l < n and lista[i] < lista[l] :",
34 34
         "detail": "heap",
35 35
         "documentation": {}
36 36
     },
37 37
     {
38
-        "label": "\t\tlargest",
38
+        "label": "\tlargest",
39 39
         "kind": 5,
40 40
         "importPath": "heap",
41 41
         "description": "heap",
42
-        "peekOfCode": "\t\tlargest = i\n\t\t# left child of root\n\t\tleft = 2 * i + 1\n\t\t# right child of root\n\t\tright = 2 * i + 2\n\t\t# Checks if root has a left child and is greater\n\t\t# than root\n\t\tif l < n and lista[i] < lista[l] :\n\t\t\tlargest = l\n\t\t# Checks if root has a right child and is greater",
42
+        "peekOfCode": "\tlargest = i\n\t# left child of root\n\tl = 2 * i + 1\n\t# right child of root\n\tr = 2 * i + 2\n\t# Checks if root has a left child and is greater\n\t# than root\n\tif l < n and lista[i] < lista[l] :\n\t\tlargest = l\n\t# Checks if root has a right child and is greater",
43 43
         "detail": "heap",
44 44
         "documentation": {}
45 45
     },
46 46
     {
47
-        "label": "\t\tleft",
47
+        "label": "\tl",
48 48
         "kind": 5,
49 49
         "importPath": "heap",
50 50
         "description": "heap",
51
-        "peekOfCode": "\t\tleft = 2 * i + 1\n\t\t# right child of root\n\t\tright = 2 * i + 2\n\t\t# Checks if root has a left child and is greater\n\t\t# than root\n\t\tif l < n and lista[i] < lista[l] :\n\t\t\tlargest = l\n\t\t# Checks if root has a right child and is greater\n\t\t# than root\n\t\tif r < n and lista[largest] < lista[r]:",
51
+        "peekOfCode": "\tl = 2 * i + 1\n\t# right child of root\n\tr = 2 * i + 2\n\t# Checks if root has a left child and is greater\n\t# than root\n\tif l < n and lista[i] < lista[l] :\n\t\tlargest = l\n\t# Checks if root has a right child and is greater\n\t# than root\n\tif r < n and lista[largest] < lista[r]:",
52 52
         "detail": "heap",
53 53
         "documentation": {}
54 54
     },
55 55
     {
56
-        "label": "\t\tright",
56
+        "label": "\tr",
57 57
         "kind": 5,
58 58
         "importPath": "heap",
59 59
         "description": "heap",
60
-        "peekOfCode": "\t\tright = 2 * i + 2\n\t\t# Checks if root has a left child and is greater\n\t\t# than root\n\t\tif l < n and lista[i] < lista[l] :\n\t\t\tlargest = l\n\t\t# Checks if root has a right child and is greater\n\t\t# than root\n\t\tif r < n and lista[largest] < lista[r]:\n\t\t\tlargest = r\n\t\t# If necessary, this changes root by swapping va-",
60
+        "peekOfCode": "\tr = 2 * i + 2\n\t# Checks if root has a left child and is greater\n\t# than root\n\tif l < n and lista[i] < lista[l] :\n\t\tlargest = l\n\t# Checks if root has a right child and is greater\n\t# than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r\n\t# If necessary, this changes root by swapping va-",
61 61
         "detail": "heap",
62 62
         "documentation": {}
63 63
     },
64 64
     {
65
-        "label": "\t\t\tlargest",
65
+        "label": "\t\tlargest",
66 66
         "kind": 5,
67 67
         "importPath": "heap",
68 68
         "description": "heap",
69
-        "peekOfCode": "\t\t\tlargest = l\n\t\t# Checks if root has a right child and is greater\n\t\t# than root\n\t\tif r < n and lista[largest] < lista[r]:\n\t\t\tlargest = r\n\t\t# If necessary, this changes root by swapping va-\n\t\t# lues\n\t\tlista[i], lista[largest] = lista[largest], lista[i]\n\t\t# This heapifies the root repeatedly\n\t\theapify(lista, n, largest)",
69
+        "peekOfCode": "\t\tlargest = l\n\t# Checks if root has a right child and is greater\n\t# than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r\n\t# If necessary, this changes root by swapping va-\n\t# lues\n\t\tlista[i], lista[largest] = lista[largest], lista[i]\n\t\t# This heapifies the root repeatedly\n\t\theapify(lista, n, largest)",
70 70
         "detail": "heap",
71 71
         "documentation": {}
72 72
     },
73 73
     {
74
-        "label": "\t\t\tlargest",
74
+        "label": "\t\tlargest",
75 75
         "kind": 5,
76 76
         "importPath": "heap",
77 77
         "description": "heap",
78
-        "peekOfCode": "\t\t\tlargest = r\n\t\t# If necessary, this changes root by swapping va-\n\t\t# lues\n\t\tlista[i], lista[largest] = lista[largest], lista[i]\n\t\t# This heapifies the root repeatedly\n\t\theapify(lista, n, largest)",
78
+        "peekOfCode": "\t\tlargest = r\n\t# If necessary, this changes root by swapping va-\n\t# lues\n\t\tlista[i], lista[largest] = lista[largest], lista[i]\n\t\t# This heapifies the root repeatedly\n\t\theapify(lista, n, largest)",
79 79
         "detail": "heap",
80 80
         "documentation": {}
81 81
     },
@@ -84,7 +84,7 @@
84 84
         "kind": 2,
85 85
         "importPath": "sorting",
86 86
         "description": "sorting",
87
-        "peekOfCode": "def mergeSort(lista):\n\t# definan el algoritmo de ordenamiento mergesort\n\treturn lista\ndef heapSort(lista):\n\tn = len(lista)\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\tfor i in range(h1, -1, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]",
87
+        "peekOfCode": "def mergeSort(lista, l, r):\n\t# definan el algoritmo de ordenamiento mergesort\n\treturn lista\ndef heapSort(lista):\n\tn = len(lista)\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\th2 = n - 1\n\tfor i in range(h2, 0, -1):",
88 88
         "detail": "sorting",
89 89
         "documentation": {}
90 90
     },
@@ -93,7 +93,7 @@
93 93
         "kind": 2,
94 94
         "importPath": "sorting",
95 95
         "description": "sorting",
96
-        "peekOfCode": "def heapSort(lista):\n\tn = len(lista)\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\tfor i in range(h1, -1, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, i, 0)\n\treturn lista\ndef quickSort(lista):",
96
+        "peekOfCode": "def heapSort(lista):\n\tn = len(lista)\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\th2 = n - 1\n\tfor i in range(h2, 0, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, 0, i)\n\treturn lista",
97 97
         "detail": "sorting",
98 98
         "documentation": {}
99 99
     },
@@ -120,7 +120,7 @@
120 120
         "kind": 5,
121 121
         "importPath": "sorting",
122 122
         "description": "sorting",
123
-        "peekOfCode": "\tn = len(lista)\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\tfor i in range(h1, -1, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, i, 0)\n\treturn lista\ndef quickSort(lista):\n\t#definan el algoritmo de ordenamiento quicksort",
123
+        "peekOfCode": "\tn = len(lista)\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\th2 = n - 1\n\tfor i in range(h2, 0, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, 0, i)\n\treturn lista\ndef quickSort(lista):",
124 124
         "detail": "sorting",
125 125
         "documentation": {}
126 126
     },
@@ -129,7 +129,16 @@
129 129
         "kind": 5,
130 130
         "importPath": "sorting",
131 131
         "description": "sorting",
132
-        "peekOfCode": "\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\tfor i in range(h1, -1, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, i, 0)\n\treturn lista\ndef quickSort(lista):\n\t#definan el algoritmo de ordenamiento quicksort\n\treturn lista",
132
+        "peekOfCode": "\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\th2 = n - 1\n\tfor i in range(h2, 0, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, 0, i)\n\treturn lista\ndef quickSort(lista):\n\t#definan el algoritmo de ordenamiento quicksort",
133
+        "detail": "sorting",
134
+        "documentation": {}
135
+    },
136
+    {
137
+        "label": "\th2",
138
+        "kind": 5,
139
+        "importPath": "sorting",
140
+        "description": "sorting",
141
+        "peekOfCode": "\th2 = n - 1\n\tfor i in range(h2, 0, -1):\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, 0, i)\n\treturn lista\ndef quickSort(lista):\n\t#definan el algoritmo de ordenamiento quicksort\n\treturn lista\ndef shellSort(lista):\n\t#definan el algoritmo de ordenamiento shellsort",
133 142
         "detail": "sorting",
134 143
         "documentation": {}
135 144
     },

BIN
__pycache__/heap.cpython-310.pyc Vedi File


+ 16
- 16
heap.py Vedi File

@@ -10,27 +10,27 @@
10 10
 # i. Also, n is the size of a heap and arr is array.
11 11
 
12 12
 def heapify(lista, n, i):
13
-		# largest is root for now
14
-		largest = i
13
+	# largest is root for now
14
+	largest = i
15 15
 
16
-		# left child of root
17
-		left = 2 * i + 1
16
+	# left child of root
17
+	l = 2 * i + 1
18 18
 
19
-		# right child of root
20
-		right = 2 * i + 2
19
+	# right child of root
20
+	r = 2 * i + 2
21 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
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 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
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 31
 
32
-		# If necessary, this changes root by swapping va-
33
-		# lues
32
+	# If necessary, this changes root by swapping va-
33
+	# lues
34 34
 		lista[i], lista[largest] = lista[largest], lista[i]
35 35
 
36 36
 		# This heapifies the root repeatedly

+ 4
- 3
sorting.py Vedi File

@@ -17,7 +17,7 @@ import heap
17 17
 # First subarray is arr[l..m]
18 18
 # Second subarray is arr[m+1..r]
19 19
 
20
-def mergeSort(lista):
20
+def mergeSort(lista, l, r):
21 21
 	# definan el algoritmo de ordenamiento mergesort
22 22
 	return lista
23 23
 
@@ -28,9 +28,10 @@ def heapSort(lista):
28 28
 	for i in range(h1, -1, -1):
29 29
 		heap.heapify(lista, n, i)
30 30
 
31
-	for i in range(h1, -1, -1):
31
+	h2 = n - 1
32
+	for i in range(h2, 0, -1):
32 33
 		lista[i], lista[0] = lista[0], lista[i]
33
-		heap.heapify(lista, i, 0)
34
+		heap.heapify(lista, 0, i)
34 35
 
35 36
 	return lista
36 37