Browse Source

Modified heap.py

luislopez66 2 years ago
parent
commit
824e0b6df3
2 changed files with 510 additions and 6 deletions
  1. 504
    0
      .vscode/PythonImportHelper-v2-Completion.json
  2. 6
    6
      heap.py

+ 504
- 0
.vscode/PythonImportHelper-v2-Completion.json View File

1
+[
2
+    {
3
+        "label": "time",
4
+        "kind": 6,
5
+        "isExtraImport": true,
6
+        "importPath": "time",
7
+        "description": "time",
8
+        "detail": "time",
9
+        "documentation": {}
10
+    },
11
+    {
12
+        "label": "randint",
13
+        "importPath": "random",
14
+        "description": "random",
15
+        "isExtraImport": true,
16
+        "detail": "random",
17
+        "documentation": {}
18
+    },
19
+    {
20
+        "label": "heap",
21
+        "kind": 6,
22
+        "isExtraImport": true,
23
+        "importPath": "heap",
24
+        "description": "heap",
25
+        "detail": "heap",
26
+        "documentation": {}
27
+    },
28
+    {
29
+        "label": "merge",
30
+        "importPath": "merge",
31
+        "description": "merge",
32
+        "isExtraImport": true,
33
+        "detail": "merge",
34
+        "documentation": {}
35
+    },
36
+    {
37
+        "label": "heapify",
38
+        "kind": 2,
39
+        "importPath": "heap",
40
+        "description": "heap",
41
+        "peekOfCode": "def heapify(lista, n, i):\n\tlargest = i # Initialize largest as root\n\tl = 2 * i + 1 # left = 2*i + 1\n\tr = 2 * i + 2 # right = 2*i + 2\n\t# See if left child of root exists and is\n\t# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n\t# See if right child of root exists and is\n\t# greater than root",
42
+        "detail": "heap",
43
+        "documentation": {}
44
+    },
45
+    {
46
+        "label": "\tlargest",
47
+        "kind": 5,
48
+        "importPath": "heap",
49
+        "description": "heap",
50
+        "peekOfCode": "\tlargest = i # Initialize largest as root\n\tl = 2 * i + 1 # left = 2*i + 1\n\tr = 2 * i + 2 # right = 2*i + 2\n\t# See if left child of root exists and is\n\t# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n\t# See if right child of root exists and is\n\t# greater than root\n\tif r < n and lista[largest] < lista[r]:",
51
+        "detail": "heap",
52
+        "documentation": {}
53
+    },
54
+    {
55
+        "label": "\tl",
56
+        "kind": 5,
57
+        "importPath": "heap",
58
+        "description": "heap",
59
+        "peekOfCode": "\tl = 2 * i + 1 # left = 2*i + 1\n\tr = 2 * i + 2 # right = 2*i + 2\n\t# See if left child of root exists and is\n\t# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n\t# See if right child of root exists and is\n\t# greater than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r",
60
+        "detail": "heap",
61
+        "documentation": {}
62
+    },
63
+    {
64
+        "label": "\tr",
65
+        "kind": 5,
66
+        "importPath": "heap",
67
+        "description": "heap",
68
+        "peekOfCode": "\tr = 2 * i + 2 # right = 2*i + 2\n\t# See if left child of root exists and is\n\t# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n\t# See if right child of root exists and is\n\t# greater than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r\n\t# Change root, if needed",
69
+        "detail": "heap",
70
+        "documentation": {}
71
+    },
72
+    {
73
+        "label": "\t\tlargest",
74
+        "kind": 5,
75
+        "importPath": "heap",
76
+        "description": "heap",
77
+        "peekOfCode": "\t\tlargest = l\n\t# See if right child of root exists and is\n\t# greater than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r\n\t# Change root, if needed\n\tif largest != i:\n\t\t(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap\n\t\t# Heapify the root.\n\t\theapify(lista, n, largest)",
78
+        "detail": "heap",
79
+        "documentation": {}
80
+    },
81
+    {
82
+        "label": "\t\tlargest",
83
+        "kind": 5,
84
+        "importPath": "heap",
85
+        "description": "heap",
86
+        "peekOfCode": "\t\tlargest = r\n\t# Change root, if needed\n\tif largest != i:\n\t\t(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap\n\t\t# Heapify the root.\n\t\theapify(lista, n, largest)",
87
+        "detail": "heap",
88
+        "documentation": {}
89
+    },
90
+    {
91
+        "label": "merge",
92
+        "kind": 2,
93
+        "importPath": "merge",
94
+        "description": "merge",
95
+        "peekOfCode": "def merge(arr, l, m, r):\n\tn1 = m - l + 1\n\tn2 = r - m\n\t# create temp arrays\n\tL = [0] * (n1)\n\tR = [0] * (n2)\n\t# Copy data to temp arrays L[] and R[]\n\tfor i in range(0, n1):\n\t\tL[i] = arr[l + i]\n\tfor j in range(0, n2):",
96
+        "detail": "merge",
97
+        "documentation": {}
98
+    },
99
+    {
100
+        "label": "\tn1",
101
+        "kind": 5,
102
+        "importPath": "merge",
103
+        "description": "merge",
104
+        "peekOfCode": "\tn1 = m - l + 1\n\tn2 = r - m\n\t# create temp arrays\n\tL = [0] * (n1)\n\tR = [0] * (n2)\n\t# Copy data to temp arrays L[] and R[]\n\tfor i in range(0, n1):\n\t\tL[i] = arr[l + i]\n\tfor j in range(0, n2):\n\t\tR[j] = arr[m + 1 + j]",
105
+        "detail": "merge",
106
+        "documentation": {}
107
+    },
108
+    {
109
+        "label": "\tn2",
110
+        "kind": 5,
111
+        "importPath": "merge",
112
+        "description": "merge",
113
+        "peekOfCode": "\tn2 = r - m\n\t# create temp arrays\n\tL = [0] * (n1)\n\tR = [0] * (n2)\n\t# Copy data to temp arrays L[] and R[]\n\tfor i in range(0, n1):\n\t\tL[i] = arr[l + i]\n\tfor j in range(0, n2):\n\t\tR[j] = arr[m + 1 + j]\n\t# Merge the temp arrays back into arr[l..r]",
114
+        "detail": "merge",
115
+        "documentation": {}
116
+    },
117
+    {
118
+        "label": "\tL",
119
+        "kind": 5,
120
+        "importPath": "merge",
121
+        "description": "merge",
122
+        "peekOfCode": "\tL = [0] * (n1)\n\tR = [0] * (n2)\n\t# Copy data to temp arrays L[] and R[]\n\tfor i in range(0, n1):\n\t\tL[i] = arr[l + i]\n\tfor j in range(0, n2):\n\t\tR[j] = arr[m + 1 + j]\n\t# Merge the temp arrays back into arr[l..r]\n\ti = 0\t # Initial index of first subarray\n\tj = 0\t # Initial index of second subarray",
123
+        "detail": "merge",
124
+        "documentation": {}
125
+    },
126
+    {
127
+        "label": "\tR",
128
+        "kind": 5,
129
+        "importPath": "merge",
130
+        "description": "merge",
131
+        "peekOfCode": "\tR = [0] * (n2)\n\t# Copy data to temp arrays L[] and R[]\n\tfor i in range(0, n1):\n\t\tL[i] = arr[l + i]\n\tfor j in range(0, n2):\n\t\tR[j] = arr[m + 1 + j]\n\t# Merge the temp arrays back into arr[l..r]\n\ti = 0\t # Initial index of first subarray\n\tj = 0\t # Initial index of second subarray\n\tk = l\t # Initial index of merged subarray",
132
+        "detail": "merge",
133
+        "documentation": {}
134
+    },
135
+    {
136
+        "label": "\t\tL[i]",
137
+        "kind": 5,
138
+        "importPath": "merge",
139
+        "description": "merge",
140
+        "peekOfCode": "\t\tL[i] = arr[l + i]\n\tfor j in range(0, n2):\n\t\tR[j] = arr[m + 1 + j]\n\t# Merge the temp arrays back into arr[l..r]\n\ti = 0\t # Initial index of first subarray\n\tj = 0\t # Initial index of second subarray\n\tk = l\t # Initial index of merged subarray\n\twhile i < n1 and j < n2:\n\t\tif L[i] <= R[j]:\n\t\t\tarr[k] = L[i]",
141
+        "detail": "merge",
142
+        "documentation": {}
143
+    },
144
+    {
145
+        "label": "\t\tR[j]",
146
+        "kind": 5,
147
+        "importPath": "merge",
148
+        "description": "merge",
149
+        "peekOfCode": "\t\tR[j] = arr[m + 1 + j]\n\t# Merge the temp arrays back into arr[l..r]\n\ti = 0\t # Initial index of first subarray\n\tj = 0\t # Initial index of second subarray\n\tk = l\t # Initial index of merged subarray\n\twhile i < n1 and j < n2:\n\t\tif L[i] <= R[j]:\n\t\t\tarr[k] = L[i]\n\t\t\ti += 1\n\t\telse:",
150
+        "detail": "merge",
151
+        "documentation": {}
152
+    },
153
+    {
154
+        "label": "\ti",
155
+        "kind": 5,
156
+        "importPath": "merge",
157
+        "description": "merge",
158
+        "peekOfCode": "\ti = 0\t # Initial index of first subarray\n\tj = 0\t # Initial index of second subarray\n\tk = l\t # Initial index of merged subarray\n\twhile i < n1 and j < n2:\n\t\tif L[i] <= R[j]:\n\t\t\tarr[k] = L[i]\n\t\t\ti += 1\n\t\telse:\n\t\t\tarr[k] = R[j]\n\t\t\tj += 1",
159
+        "detail": "merge",
160
+        "documentation": {}
161
+    },
162
+    {
163
+        "label": "\tj",
164
+        "kind": 5,
165
+        "importPath": "merge",
166
+        "description": "merge",
167
+        "peekOfCode": "\tj = 0\t # Initial index of second subarray\n\tk = l\t # Initial index of merged subarray\n\twhile i < n1 and j < n2:\n\t\tif L[i] <= R[j]:\n\t\t\tarr[k] = L[i]\n\t\t\ti += 1\n\t\telse:\n\t\t\tarr[k] = R[j]\n\t\t\tj += 1\n\t\tk += 1",
168
+        "detail": "merge",
169
+        "documentation": {}
170
+    },
171
+    {
172
+        "label": "\tk",
173
+        "kind": 5,
174
+        "importPath": "merge",
175
+        "description": "merge",
176
+        "peekOfCode": "\tk = l\t # Initial index of merged subarray\n\twhile i < n1 and j < n2:\n\t\tif L[i] <= R[j]:\n\t\t\tarr[k] = L[i]\n\t\t\ti += 1\n\t\telse:\n\t\t\tarr[k] = R[j]\n\t\t\tj += 1\n\t\tk += 1\n\t# Copy the remaining elements of L[], if there",
177
+        "detail": "merge",
178
+        "documentation": {}
179
+    },
180
+    {
181
+        "label": "\t\t\tarr[k]",
182
+        "kind": 5,
183
+        "importPath": "merge",
184
+        "description": "merge",
185
+        "peekOfCode": "\t\t\tarr[k] = L[i]\n\t\t\ti += 1\n\t\telse:\n\t\t\tarr[k] = R[j]\n\t\t\tj += 1\n\t\tk += 1\n\t# Copy the remaining elements of L[], if there\n\t# are any\n\twhile i < n1:\n\t\tarr[k] = L[i]",
186
+        "detail": "merge",
187
+        "documentation": {}
188
+    },
189
+    {
190
+        "label": "\t\t\tarr[k]",
191
+        "kind": 5,
192
+        "importPath": "merge",
193
+        "description": "merge",
194
+        "peekOfCode": "\t\t\tarr[k] = R[j]\n\t\t\tj += 1\n\t\tk += 1\n\t# Copy the remaining elements of L[], if there\n\t# are any\n\twhile i < n1:\n\t\tarr[k] = L[i]\n\t\ti += 1\n\t\tk += 1\n\t# Copy the remaining elements of R[], if there",
195
+        "detail": "merge",
196
+        "documentation": {}
197
+    },
198
+    {
199
+        "label": "\t\tarr[k]",
200
+        "kind": 5,
201
+        "importPath": "merge",
202
+        "description": "merge",
203
+        "peekOfCode": "\t\tarr[k] = L[i]\n\t\ti += 1\n\t\tk += 1\n\t# Copy the remaining elements of R[], if there\n\t# are any\n\twhile j < n2:\n\t\tarr[k] = R[j]\n\t\tj += 1\n\t\tk += 1\n# l is for left index and r is right index of the",
204
+        "detail": "merge",
205
+        "documentation": {}
206
+    },
207
+    {
208
+        "label": "\t\tarr[k]",
209
+        "kind": 5,
210
+        "importPath": "merge",
211
+        "description": "merge",
212
+        "peekOfCode": "\t\tarr[k] = R[j]\n\t\tj += 1\n\t\tk += 1\n# l is for left index and r is right index of the\n# sub-array of arr to be sorted",
213
+        "detail": "merge",
214
+        "documentation": {}
215
+    },
216
+    {
217
+        "label": "mergeSort",
218
+        "kind": 2,
219
+        "importPath": "sorting",
220
+        "description": "sorting",
221
+        "peekOfCode": "def mergeSort(lista, l, r):\n\tif l < r:\n      # Same as (l+r)//2, but avoids overflow for\n      # large l and h\n\t\tm = l+(r-l)//2\n      # Sort first and second halves\n\t\tmergeSort(lista, l, m)\n\t\tmergeSort(lista, m+1, r)\n\t\tmerge(lista, l, m, r)\n''' Luis Andrés López Mañán",
222
+        "detail": "sorting",
223
+        "documentation": {}
224
+    },
225
+    {
226
+        "label": "heapSort",
227
+        "kind": 2,
228
+        "importPath": "sorting",
229
+        "description": "sorting",
230
+        "peekOfCode": "def heapSort(lista):\n\t# Se busca el tamaño de la lista\n\tn = len(lista)\n\theap.heapify(lista,n,0)\n\t''' Se crea un heap máximo y el último padre estará en\n\t\tla posición h1, i.e., la mitad del tamaño de la lista.\n\t\tPor lo tanto, ese sería el comienzo. \n\t'''\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):",
231
+        "detail": "sorting",
232
+        "documentation": {}
233
+    },
234
+    {
235
+        "label": "quickSort",
236
+        "kind": 2,
237
+        "importPath": "sorting",
238
+        "description": "sorting",
239
+        "peekOfCode": "def quickSort(lista):\n\t# definan el algoritmo de ordenamiento quicksort\n\telements = len(lista)\n    # Base case\n\tif elements < 2:\n\t\treturn lista\n\tcurrent_position = 0 #Position of the partitioning element\n\tfor i in range(1, elements): #Partitioning loop\n\t\tif lista[i] <= lista[0]:\n\t\t\tcurrent_position += 1",
240
+        "detail": "sorting",
241
+        "documentation": {}
242
+    },
243
+    {
244
+        "label": "shellSort",
245
+        "kind": 2,
246
+        "importPath": "sorting",
247
+        "description": "sorting",
248
+        "peekOfCode": "def shellSort(lista):\n\t# definan el algoritmo de ordenamiento shellsort\n\t# determening the size of the list and calculates the gap value. \n\tn = len(lista)\n\tgap = n // 2\n\t# this algorithm will run until gap reaches 1 \n\twhile gap > 0:\n\t\tfor i in range(gap, n):\n\t\t\ttemp = lista[i] # storing all items from the list into temp \n\t\t\tj = i ",
249
+        "detail": "sorting",
250
+        "documentation": {}
251
+    },
252
+    {
253
+        "label": "\t\tm",
254
+        "kind": 5,
255
+        "importPath": "sorting",
256
+        "description": "sorting",
257
+        "peekOfCode": "\t\tm = l+(r-l)//2\n      # Sort first and second halves\n\t\tmergeSort(lista, l, m)\n\t\tmergeSort(lista, m+1, r)\n\t\tmerge(lista, l, m, r)\n''' Luis Andrés López Mañán\n    Program written by hand as a draft on 09/19/2022\n    Credit goes to GeeksForGeeks\n    Link: https://www.geeksforgeeks.org/python-program-for-heap-sort/\n    Last access on 09/19/2022",
258
+        "detail": "sorting",
259
+        "documentation": {}
260
+    },
261
+    {
262
+        "label": "\tn",
263
+        "kind": 5,
264
+        "importPath": "sorting",
265
+        "description": "sorting",
266
+        "peekOfCode": "\tn = len(lista)\n\theap.heapify(lista,n,0)\n\t''' Se crea un heap máximo y el último padre estará en\n\t\tla posición h1, i.e., la mitad del tamaño de la lista.\n\t\tPor lo tanto, ese sería el comienzo. \n\t'''\n\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\t# Se extrae los elementos uno a uno",
267
+        "detail": "sorting",
268
+        "documentation": {}
269
+    },
270
+    {
271
+        "label": "\th1",
272
+        "kind": 5,
273
+        "importPath": "sorting",
274
+        "description": "sorting",
275
+        "peekOfCode": "\th1 = (n // 2) - 1\n\tfor i in range(h1, -1, -1):\n\t\theap.heapify(lista, n, i)\n\t# Se extrae los elementos uno a uno\n\th2 = n - 1\n\tfor i in range(h2, 0, -1):\n\t\t# Se intercambia, luego se hace heapify\n\t\tlista[i], lista[0] = lista[0], lista[i]\n\t\theap.heapify(lista, 0, i)\n\treturn lista",
276
+        "detail": "sorting",
277
+        "documentation": {}
278
+    },
279
+    {
280
+        "label": "\th2",
281
+        "kind": 5,
282
+        "importPath": "sorting",
283
+        "description": "sorting",
284
+        "peekOfCode": "\th2 = n - 1\n\tfor i in range(h2, 0, -1):\n\t\t# Se intercambia, luego se hace heapify\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\telements = len(lista)\n    # Base case",
285
+        "detail": "sorting",
286
+        "documentation": {}
287
+    },
288
+    {
289
+        "label": "\telements",
290
+        "kind": 5,
291
+        "importPath": "sorting",
292
+        "description": "sorting",
293
+        "peekOfCode": "\telements = len(lista)\n    # Base case\n\tif elements < 2:\n\t\treturn lista\n\tcurrent_position = 0 #Position of the partitioning element\n\tfor i in range(1, elements): #Partitioning loop\n\t\tif lista[i] <= lista[0]:\n\t\t\tcurrent_position += 1\n\t\t\ttemp = lista[i]\n\t\t\tlista[i] = lista[current_position]",
294
+        "detail": "sorting",
295
+        "documentation": {}
296
+    },
297
+    {
298
+        "label": "\tcurrent_position",
299
+        "kind": 5,
300
+        "importPath": "sorting",
301
+        "description": "sorting",
302
+        "peekOfCode": "\tcurrent_position = 0 #Position of the partitioning element\n\tfor i in range(1, elements): #Partitioning loop\n\t\tif lista[i] <= lista[0]:\n\t\t\tcurrent_position += 1\n\t\t\ttemp = lista[i]\n\t\t\tlista[i] = lista[current_position]\n\t\t\tlista[current_position] = temp\n\ttemp = lista[0]\n\tlista[0] = lista[current_position] \n\tlista[current_position] = temp #Brings pivot to it's appropriate position",
303
+        "detail": "sorting",
304
+        "documentation": {}
305
+    },
306
+    {
307
+        "label": "\t\t\ttemp",
308
+        "kind": 5,
309
+        "importPath": "sorting",
310
+        "description": "sorting",
311
+        "peekOfCode": "\t\t\ttemp = lista[i]\n\t\t\tlista[i] = lista[current_position]\n\t\t\tlista[current_position] = temp\n\ttemp = lista[0]\n\tlista[0] = lista[current_position] \n\tlista[current_position] = temp #Brings pivot to it's appropriate position\n\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista",
312
+        "detail": "sorting",
313
+        "documentation": {}
314
+    },
315
+    {
316
+        "label": "\t\t\tlista[i]",
317
+        "kind": 5,
318
+        "importPath": "sorting",
319
+        "description": "sorting",
320
+        "peekOfCode": "\t\t\tlista[i] = lista[current_position]\n\t\t\tlista[current_position] = temp\n\ttemp = lista[0]\n\tlista[0] = lista[current_position] \n\tlista[current_position] = temp #Brings pivot to it's appropriate position\n\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' ",
321
+        "detail": "sorting",
322
+        "documentation": {}
323
+    },
324
+    {
325
+        "label": "\t\t\tlista[current_position]",
326
+        "kind": 5,
327
+        "importPath": "sorting",
328
+        "description": "sorting",
329
+        "peekOfCode": "\t\t\tlista[current_position] = temp\n\ttemp = lista[0]\n\tlista[0] = lista[current_position] \n\tlista[current_position] = temp #Brings pivot to it's appropriate position\n\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort ",
330
+        "detail": "sorting",
331
+        "documentation": {}
332
+    },
333
+    {
334
+        "label": "\ttemp",
335
+        "kind": 5,
336
+        "importPath": "sorting",
337
+        "description": "sorting",
338
+        "peekOfCode": "\ttemp = lista[0]\n\tlista[0] = lista[current_position] \n\tlista[current_position] = temp #Brings pivot to it's appropriate position\n\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort \n\tand was adapted in order to work for this assigment.",
339
+        "detail": "sorting",
340
+        "documentation": {}
341
+    },
342
+    {
343
+        "label": "\tlista[0]",
344
+        "kind": 5,
345
+        "importPath": "sorting",
346
+        "description": "sorting",
347
+        "peekOfCode": "\tlista[0] = lista[current_position] \n\tlista[current_position] = temp #Brings pivot to it's appropriate position\n\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort \n\tand was adapted in order to work for this assigment.\n'''",
348
+        "detail": "sorting",
349
+        "documentation": {}
350
+    },
351
+    {
352
+        "label": "\tlista[current_position]",
353
+        "kind": 5,
354
+        "importPath": "sorting",
355
+        "description": "sorting",
356
+        "peekOfCode": "\tlista[current_position] = temp #Brings pivot to it's appropriate position\n\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort \n\tand was adapted in order to work for this assigment.\n'''\ndef shellSort(lista):",
357
+        "detail": "sorting",
358
+        "documentation": {}
359
+    },
360
+    {
361
+        "label": "\tleft",
362
+        "kind": 5,
363
+        "importPath": "sorting",
364
+        "description": "sorting",
365
+        "peekOfCode": "\tleft = quickSort(lista[0:current_position]) #Sorts the elements to the left of pivot\n\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort \n\tand was adapted in order to work for this assigment.\n'''\ndef shellSort(lista):\n\t# definan el algoritmo de ordenamiento shellsort",
366
+        "detail": "sorting",
367
+        "documentation": {}
368
+    },
369
+    {
370
+        "label": "\tright",
371
+        "kind": 5,
372
+        "importPath": "sorting",
373
+        "description": "sorting",
374
+        "peekOfCode": "\tright = quickSort(lista[current_position+1:elements]) #sorts the elements to the right of pivot\n\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort \n\tand was adapted in order to work for this assigment.\n'''\ndef shellSort(lista):\n\t# definan el algoritmo de ordenamiento shellsort\n\t# determening the size of the list and calculates the gap value. ",
375
+        "detail": "sorting",
376
+        "documentation": {}
377
+    },
378
+    {
379
+        "label": "\tlista",
380
+        "kind": 5,
381
+        "importPath": "sorting",
382
+        "description": "sorting",
383
+        "peekOfCode": "\tlista = left + [lista[current_position]] + right #Merging everything together\n\treturn lista\n''' \n\tThis algorithm was taken from: https://www.programiz.com/dsa/shell-sort \n\tand was adapted in order to work for this assigment.\n'''\ndef shellSort(lista):\n\t# definan el algoritmo de ordenamiento shellsort\n\t# determening the size of the list and calculates the gap value. \n\tn = len(lista)",
384
+        "detail": "sorting",
385
+        "documentation": {}
386
+    },
387
+    {
388
+        "label": "\tn",
389
+        "kind": 5,
390
+        "importPath": "sorting",
391
+        "description": "sorting",
392
+        "peekOfCode": "\tn = len(lista)\n\tgap = n // 2\n\t# this algorithm will run until gap reaches 1 \n\twhile gap > 0:\n\t\tfor i in range(gap, n):\n\t\t\ttemp = lista[i] # storing all items from the list into temp \n\t\t\tj = i \n\t\t\t# compares the number in temp with the 0th possition (start of list)\n\t\t\t# if temp is larger than the first element then we swap them\n\t\t\twhile j >= gap and lista[j - gap] > temp: ",
393
+        "detail": "sorting",
394
+        "documentation": {}
395
+    },
396
+    {
397
+        "label": "\tgap",
398
+        "kind": 5,
399
+        "importPath": "sorting",
400
+        "description": "sorting",
401
+        "peekOfCode": "\tgap = n // 2\n\t# this algorithm will run until gap reaches 1 \n\twhile gap > 0:\n\t\tfor i in range(gap, n):\n\t\t\ttemp = lista[i] # storing all items from the list into temp \n\t\t\tj = i \n\t\t\t# compares the number in temp with the 0th possition (start of list)\n\t\t\t# if temp is larger than the first element then we swap them\n\t\t\twhile j >= gap and lista[j - gap] > temp: \n\t\t\t\tlista[j] = lista[j - gap]",
402
+        "detail": "sorting",
403
+        "documentation": {}
404
+    },
405
+    {
406
+        "label": "\t\t\ttemp",
407
+        "kind": 5,
408
+        "importPath": "sorting",
409
+        "description": "sorting",
410
+        "peekOfCode": "\t\t\ttemp = lista[i] # storing all items from the list into temp \n\t\t\tj = i \n\t\t\t# compares the number in temp with the 0th possition (start of list)\n\t\t\t# if temp is larger than the first element then we swap them\n\t\t\twhile j >= gap and lista[j - gap] > temp: \n\t\t\t\tlista[j] = lista[j - gap]\n\t\t\t\tj -= gap\n\t\t\tlista[j] = temp\n\t\tgap = gap // 2 # decreases the gap to continue the loop \n\treturn lista",
411
+        "detail": "sorting",
412
+        "documentation": {}
413
+    },
414
+    {
415
+        "label": "\t\t\tj",
416
+        "kind": 5,
417
+        "importPath": "sorting",
418
+        "description": "sorting",
419
+        "peekOfCode": "\t\t\tj = i \n\t\t\t# compares the number in temp with the 0th possition (start of list)\n\t\t\t# if temp is larger than the first element then we swap them\n\t\t\twhile j >= gap and lista[j - gap] > temp: \n\t\t\t\tlista[j] = lista[j - gap]\n\t\t\t\tj -= gap\n\t\t\tlista[j] = temp\n\t\tgap = gap // 2 # decreases the gap to continue the loop \n\treturn lista\nmaxValor=1000 \t#define el valor maximo de los elementos de la lista",
420
+        "detail": "sorting",
421
+        "documentation": {}
422
+    },
423
+    {
424
+        "label": "\t\t\t\tlista[j]",
425
+        "kind": 5,
426
+        "importPath": "sorting",
427
+        "description": "sorting",
428
+        "peekOfCode": "\t\t\t\tlista[j] = lista[j - gap]\n\t\t\t\tj -= gap\n\t\t\tlista[j] = temp\n\t\tgap = gap // 2 # decreases the gap to continue the loop \n\treturn lista\nmaxValor=1000 \t#define el valor maximo de los elementos de la lista\nlargoLista=1000 #define el largo de las listas a ordenar\nveces=100 \t\t#define las veces que se va a hacer el ordenamiento \nacumulaMerge=0 \t#variable para acumular el tiempo de ejecucion del mergesort\nacumulaHeap=0 \t#variable para acumular el tiempo de ejecucion del heapsort",
429
+        "detail": "sorting",
430
+        "documentation": {}
431
+    },
432
+    {
433
+        "label": "\t\t\tlista[j]",
434
+        "kind": 5,
435
+        "importPath": "sorting",
436
+        "description": "sorting",
437
+        "peekOfCode": "\t\t\tlista[j] = temp\n\t\tgap = gap // 2 # decreases the gap to continue the loop \n\treturn lista\nmaxValor=1000 \t#define el valor maximo de los elementos de la lista\nlargoLista=1000 #define el largo de las listas a ordenar\nveces=100 \t\t#define las veces que se va a hacer el ordenamiento \nacumulaMerge=0 \t#variable para acumular el tiempo de ejecucion del mergesort\nacumulaHeap=0 \t#variable para acumular el tiempo de ejecucion del heapsort\nacumulaQuick=0 \t#variable para acumular el tiempo de ejecucion del quicksort\nacumulaShell=0 \t#variable para acumular el tiempo de ejecucion del shellsort",
438
+        "detail": "sorting",
439
+        "documentation": {}
440
+    },
441
+    {
442
+        "label": "\t\tgap",
443
+        "kind": 5,
444
+        "importPath": "sorting",
445
+        "description": "sorting",
446
+        "peekOfCode": "\t\tgap = gap // 2 # decreases the gap to continue the loop \n\treturn lista\nmaxValor=1000 \t#define el valor maximo de los elementos de la lista\nlargoLista=1000 #define el largo de las listas a ordenar\nveces=100 \t\t#define las veces que se va a hacer el ordenamiento \nacumulaMerge=0 \t#variable para acumular el tiempo de ejecucion del mergesort\nacumulaHeap=0 \t#variable para acumular el tiempo de ejecucion del heapsort\nacumulaQuick=0 \t#variable para acumular el tiempo de ejecucion del quicksort\nacumulaShell=0 \t#variable para acumular el tiempo de ejecucion del shellsort\nfor i in range(veces):",
447
+        "detail": "sorting",
448
+        "documentation": {}
449
+    },
450
+    {
451
+        "label": "\tmergelista",
452
+        "kind": 5,
453
+        "importPath": "sorting",
454
+        "description": "sorting",
455
+        "peekOfCode": "\tmergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar\n\theaplista=list(mergelista)\n\tquicklista=list(mergelista)\n\tsearchlista=list(mergelista)\n\tt1 = time.process_time()\t\t\t\t\t#tomamos el tiempo inicial\n\tmergeSort(mergelista,0,len(mergelista)-1) \t#ejecutamos el algoritmo mergeSort\n\tacumulaMerge+=time.process_time() - t1\t\t#acumulamos el tiempo de ejecucion\n\t# print(mergelista)\t\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\theapSort(heaplista)\t\t\t\t\t    #ejecutamos el algoritmo heapSort",
456
+        "detail": "sorting",
457
+        "documentation": {}
458
+    },
459
+    {
460
+        "label": "\tt1",
461
+        "kind": 5,
462
+        "importPath": "sorting",
463
+        "description": "sorting",
464
+        "peekOfCode": "\tt1 = time.process_time()\t\t\t\t\t#tomamos el tiempo inicial\n\tmergeSort(mergelista,0,len(mergelista)-1) \t#ejecutamos el algoritmo mergeSort\n\tacumulaMerge+=time.process_time() - t1\t\t#acumulamos el tiempo de ejecucion\n\t# print(mergelista)\t\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\theapSort(heaplista)\t\t\t\t\t    #ejecutamos el algoritmo heapSort\n\tacumulaHeap+=time.process_time() - t1 \t#acumulamos el tiempo de ejecucion\n\t# print(heaplista)\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tquickresult = quickSort(quicklista)\t\t\t\t\t#ejecutamos el algoritmo quickSort",
465
+        "detail": "sorting",
466
+        "documentation": {}
467
+    },
468
+    {
469
+        "label": "\tt1",
470
+        "kind": 5,
471
+        "importPath": "sorting",
472
+        "description": "sorting",
473
+        "peekOfCode": "\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\theapSort(heaplista)\t\t\t\t\t    #ejecutamos el algoritmo heapSort\n\tacumulaHeap+=time.process_time() - t1 \t#acumulamos el tiempo de ejecucion\n\t# print(heaplista)\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tquickresult = quickSort(quicklista)\t\t\t\t\t#ejecutamos el algoritmo quickSort\n\tacumulaQuick+=time.process_time() - t1\t#acumulamos el tiempo de ejecucion\n\t# print(quicklista)\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tshellSort(searchlista)\t\t\t\t\t#ejecutamos el algoritmo shellSort",
474
+        "detail": "sorting",
475
+        "documentation": {}
476
+    },
477
+    {
478
+        "label": "\tt1",
479
+        "kind": 5,
480
+        "importPath": "sorting",
481
+        "description": "sorting",
482
+        "peekOfCode": "\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tquickresult = quickSort(quicklista)\t\t\t\t\t#ejecutamos el algoritmo quickSort\n\tacumulaQuick+=time.process_time() - t1\t#acumulamos el tiempo de ejecucion\n\t# print(quicklista)\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tshellSort(searchlista)\t\t\t\t\t#ejecutamos el algoritmo shellSort\n\tacumulaShell+=time.process_time() - t1\t#acumulamos el tiempo de ejecucion\n\t# print(searchlista)\t\t\t\t\t\t#desplegamos la lista\n# imprimos los resultados\nprint (\"Promedio de tiempo de ejecucion de \"+ str(veces) +\" listas de largo \" + str(largoLista))",
483
+        "detail": "sorting",
484
+        "documentation": {}
485
+    },
486
+    {
487
+        "label": "\tquickresult",
488
+        "kind": 5,
489
+        "importPath": "sorting",
490
+        "description": "sorting",
491
+        "peekOfCode": "\tquickresult = quickSort(quicklista)\t\t\t\t\t#ejecutamos el algoritmo quickSort\n\tacumulaQuick+=time.process_time() - t1\t#acumulamos el tiempo de ejecucion\n\t# print(quicklista)\t\t\t\t\t\t#desplegamos la lista\n\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tshellSort(searchlista)\t\t\t\t\t#ejecutamos el algoritmo shellSort\n\tacumulaShell+=time.process_time() - t1\t#acumulamos el tiempo de ejecucion\n\t# print(searchlista)\t\t\t\t\t\t#desplegamos la lista\n# imprimos los resultados\nprint (\"Promedio de tiempo de ejecucion de \"+ str(veces) +\" listas de largo \" + str(largoLista))\nprint (\"MergeSort \" + str(acumulaMerge/veces) + \" segundos\")",
492
+        "detail": "sorting",
493
+        "documentation": {}
494
+    },
495
+    {
496
+        "label": "\tt1",
497
+        "kind": 5,
498
+        "importPath": "sorting",
499
+        "description": "sorting",
500
+        "peekOfCode": "\tt1 = time.process_time()\t\t\t\t#tomamos el tiempo inicial\n\tshellSort(searchlista)\t\t\t\t\t#ejecutamos el algoritmo shellSort\n\tacumulaShell+=time.process_time() - t1\t#acumulamos el tiempo de ejecucion\n\t# print(searchlista)\t\t\t\t\t\t#desplegamos la lista\n# imprimos los resultados\nprint (\"Promedio de tiempo de ejecucion de \"+ str(veces) +\" listas de largo \" + str(largoLista))\nprint (\"MergeSort \" + str(acumulaMerge/veces) + \" segundos\")\nprint (\"HeapSort \" + str(acumulaHeap/veces) + \" segundos\")\nprint (\"QuickSort \" + str(acumulaQuick/veces) + \" segundos\")\nprint (\"ShellSort \" + str(acumulaShell/veces) + \" segundos\")",
501
+        "detail": "sorting",
502
+        "documentation": {}
503
+    }
504
+]

+ 6
- 6
heap.py View File

16
 	l = 2 * i + 1 # left = 2*i + 1
16
 	l = 2 * i + 1 # left = 2*i + 1
17
 	r = 2 * i + 2 # right = 2*i + 2
17
 	r = 2 * i + 2 # right = 2*i + 2
18
 
18
 
19
-# See if left child of root exists and is
20
-# greater than root
19
+	# See if left child of root exists and is
20
+	# greater than root
21
 
21
 
22
 	if l < n and lista[i] < lista[l]:
22
 	if l < n and lista[i] < lista[l]:
23
 		largest = l
23
 		largest = l
24
 
24
 
25
-# See if right child of root exists and is
26
-# greater than root
25
+	# See if right child of root exists and is
26
+	# greater than root
27
 
27
 
28
 	if r < n and lista[largest] < lista[r]:
28
 	if r < n and lista[largest] < lista[r]:
29
 		largest = r
29
 		largest = r
30
 
30
 
31
-# Change root, if needed
31
+	# Change root, if needed
32
 
32
 
33
 	if largest != i:
33
 	if largest != i:
34
 		(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap
34
 		(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap
35
 
35
 
36
-# Heapify the root.
36
+		# Heapify the root.
37
 
37
 
38
 		heapify(lista, n, largest)
38
 		heapify(lista, n, largest)