Просмотр исходного кода

Modified heap.py and sorting.py

luislopez66 2 лет назад
Родитель
Сommit
832ea0d1ac
4 измененных файлов: 44 добавлений и 34 удалений
  1. 24
    15
      .vscode/PythonImportHelper-v2-Completion.json
  2. Двоичные данные
      __pycache__/heap.cpython-310.pyc
  3. 16
    16
      heap.py
  4. 4
    3
      sorting.py

+ 24
- 15
.vscode/PythonImportHelper-v2-Completion.json Просмотреть файл

30
         "kind": 2,
30
         "kind": 2,
31
         "importPath": "heap",
31
         "importPath": "heap",
32
         "description": "heap",
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
         "detail": "heap",
34
         "detail": "heap",
35
         "documentation": {}
35
         "documentation": {}
36
     },
36
     },
37
     {
37
     {
38
-        "label": "\t\tlargest",
38
+        "label": "\tlargest",
39
         "kind": 5,
39
         "kind": 5,
40
         "importPath": "heap",
40
         "importPath": "heap",
41
         "description": "heap",
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
         "detail": "heap",
43
         "detail": "heap",
44
         "documentation": {}
44
         "documentation": {}
45
     },
45
     },
46
     {
46
     {
47
-        "label": "\t\tleft",
47
+        "label": "\tl",
48
         "kind": 5,
48
         "kind": 5,
49
         "importPath": "heap",
49
         "importPath": "heap",
50
         "description": "heap",
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
         "detail": "heap",
52
         "detail": "heap",
53
         "documentation": {}
53
         "documentation": {}
54
     },
54
     },
55
     {
55
     {
56
-        "label": "\t\tright",
56
+        "label": "\tr",
57
         "kind": 5,
57
         "kind": 5,
58
         "importPath": "heap",
58
         "importPath": "heap",
59
         "description": "heap",
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
         "detail": "heap",
61
         "detail": "heap",
62
         "documentation": {}
62
         "documentation": {}
63
     },
63
     },
64
     {
64
     {
65
-        "label": "\t\t\tlargest",
65
+        "label": "\t\tlargest",
66
         "kind": 5,
66
         "kind": 5,
67
         "importPath": "heap",
67
         "importPath": "heap",
68
         "description": "heap",
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
         "detail": "heap",
70
         "detail": "heap",
71
         "documentation": {}
71
         "documentation": {}
72
     },
72
     },
73
     {
73
     {
74
-        "label": "\t\t\tlargest",
74
+        "label": "\t\tlargest",
75
         "kind": 5,
75
         "kind": 5,
76
         "importPath": "heap",
76
         "importPath": "heap",
77
         "description": "heap",
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
         "detail": "heap",
79
         "detail": "heap",
80
         "documentation": {}
80
         "documentation": {}
81
     },
81
     },
84
         "kind": 2,
84
         "kind": 2,
85
         "importPath": "sorting",
85
         "importPath": "sorting",
86
         "description": "sorting",
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
         "detail": "sorting",
88
         "detail": "sorting",
89
         "documentation": {}
89
         "documentation": {}
90
     },
90
     },
93
         "kind": 2,
93
         "kind": 2,
94
         "importPath": "sorting",
94
         "importPath": "sorting",
95
         "description": "sorting",
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
         "detail": "sorting",
97
         "detail": "sorting",
98
         "documentation": {}
98
         "documentation": {}
99
     },
99
     },
120
         "kind": 5,
120
         "kind": 5,
121
         "importPath": "sorting",
121
         "importPath": "sorting",
122
         "description": "sorting",
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
         "detail": "sorting",
124
         "detail": "sorting",
125
         "documentation": {}
125
         "documentation": {}
126
     },
126
     },
129
         "kind": 5,
129
         "kind": 5,
130
         "importPath": "sorting",
130
         "importPath": "sorting",
131
         "description": "sorting",
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
         "detail": "sorting",
142
         "detail": "sorting",
134
         "documentation": {}
143
         "documentation": {}
135
     },
144
     },

Двоичные данные
__pycache__/heap.cpython-310.pyc Просмотреть файл


+ 16
- 16
heap.py Просмотреть файл

10
 # i. Also, n is the size of a heap and arr is array.
10
 # i. Also, n is the size of a heap and arr is array.
11
 
11
 
12
 def heapify(lista, n, i):
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
 		lista[i], lista[largest] = lista[largest], lista[i]
34
 		lista[i], lista[largest] = lista[largest], lista[i]
35
 
35
 
36
 		# This heapifies the root repeatedly
36
 		# This heapifies the root repeatedly

+ 4
- 3
sorting.py Просмотреть файл

17
 # First subarray is arr[l..m]
17
 # First subarray is arr[l..m]
18
 # Second subarray is arr[m+1..r]
18
 # Second subarray is arr[m+1..r]
19
 
19
 
20
-def mergeSort(lista):
20
+def mergeSort(lista, l, r):
21
 	# definan el algoritmo de ordenamiento mergesort
21
 	# definan el algoritmo de ordenamiento mergesort
22
 	return lista
22
 	return lista
23
 
23
 
28
 	for i in range(h1, -1, -1):
28
 	for i in range(h1, -1, -1):
29
 		heap.heapify(lista, n, i)
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
 		lista[i], lista[0] = lista[0], lista[i]
33
 		lista[i], lista[0] = lista[0], lista[i]
33
-		heap.heapify(lista, i, 0)
34
+		heap.heapify(lista, 0, i)
34
 
35
 
35
 	return lista
36
 	return lista
36
 
37