瀏覽代碼

Modified sorting.py adn it works

luislopez66 2 年之前
父節點
當前提交
c90335fddc
共有 5 個文件被更改,包括 562 次插入79 次删除
  1. 504
    0
      .vscode/PythonImportHelper-v2-Completion.json
  2. 二進制
      __pycache__/heap.cpython-310.pyc
  3. 二進制
      __pycache__/merge.cpython-310.pyc
  4. 20
    20
      heap.py
  5. 38
    59
      sorting.py

+ 504
- 0
.vscode/PythonImportHelper-v2-Completion.json 查看文件

@@ -0,0 +1,504 @@
1
+[
2
+    {
3
+        "label": "randint",
4
+        "importPath": "random",
5
+        "description": "random",
6
+        "isExtraImport": true,
7
+        "detail": "random",
8
+        "documentation": {}
9
+    },
10
+    {
11
+        "label": "time",
12
+        "kind": 6,
13
+        "isExtraImport": true,
14
+        "importPath": "time",
15
+        "description": "time",
16
+        "detail": "time",
17
+        "documentation": {}
18
+    },
19
+    {
20
+        "label": "merge",
21
+        "importPath": "merge",
22
+        "description": "merge",
23
+        "isExtraImport": true,
24
+        "detail": "merge",
25
+        "documentation": {}
26
+    },
27
+    {
28
+        "label": "heap",
29
+        "kind": 6,
30
+        "isExtraImport": true,
31
+        "importPath": "heap",
32
+        "description": "heap",
33
+        "detail": "heap",
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# See if left child of root exists and is\n# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n# See if right child of root exists and is\n# 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# See if left child of root exists and is\n# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n# See if right child of root exists and is\n# 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# See if left child of root exists and is\n# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n# See if right child of root exists and is\n# 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# See if left child of root exists and is\n# greater than root\n\tif l < n and lista[i] < lista[l]:\n\t\tlargest = l\n# See if right child of root exists and is\n# greater than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r\n# 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# See if right child of root exists and is\n# greater than root\n\tif r < n and lista[largest] < lista[r]:\n\t\tlargest = r\n# Change root, if needed\n\tif largest != i:\n\t\t(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap\n# 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# Change root, if needed\n\tif largest != i:\n\t\t(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap\n# 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
+]

二進制
__pycache__/heap.cpython-310.pyc 查看文件


二進制
__pycache__/merge.cpython-310.pyc 查看文件


+ 20
- 20
heap.py 查看文件

@@ -6,33 +6,33 @@
6 6
     Program wrriten and edited on 10/10/2022
7 7
 '''
8 8
 
9
+# Python program for implementation of heap Sort (Part I)
10
+
9 11
 # This function heapifies a subtree rooted at an index
10 12
 # i. Also, n is the size of a heap and arr is array.
11 13
 
12 14
 def heapify(lista, n, i):
13
-        # largest is root for now
14
-		largest = i
15
+	largest = i # Initialize largest as root
16
+	l = 2 * i + 1 # left = 2*i + 1
17
+	r = 2 * i + 2 # right = 2*i + 2
18
+
19
+# See if left child of root exists and is
20
+# greater than root
21
+
22
+	if l < n and lista[i] < lista[l]:
23
+		largest = l
15 24
 
16
-		# left child of root
17
-		l = 2 * i + 1
25
+# See if right child of root exists and is
26
+# greater than root
18 27
 
19
-		# right child of root
20
-		r = 2 * i + 2
28
+	if r < n and lista[largest] < lista[r]:
29
+		largest = r
21 30
 
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
31
+# Change root, if needed
26 32
 
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
33
+	if largest != i:
34
+		(lista[i], lista[largest]) = (lista[largest], lista[i]) # swap
31 35
 
32
-		# If necessary, this changes root by swapping va-
33
-		# lues
34
-        if largest != i:
35
-            lista[i], lista[largest] = lista[largest], lista[i]
36
+# Heapify the root.
36 37
 
37
-		# This heapifies the root repeatedly
38
-		heapify(lista, n, largest)
38
+		heapify(lista, n, largest)

+ 38
- 59
sorting.py 查看文件

@@ -10,7 +10,7 @@ Al final se imprimen los promedios de cada algortimo
10 10
 from random import randint
11 11
 import time
12 12
 from merge import merge
13
-import heapq
13
+import heap
14 14
 
15 15
 # Python program for implementation of MergeSort
16 16
 
@@ -18,17 +18,16 @@ import heapq
18 18
 # First subarray is arr[l..m]
19 19
 # Second subarray is arr[m+1..r]
20 20
 
21
+
21 22
 def mergeSort(lista, l, r):
22 23
 	if l < r:
23
-
24
-	# Same as (l+r)//2, but avoids overflow for
25
-	# large l and h
26
-	m = l+(r-l)//2
27
-
28
-	# Sort first and second halves
29
-	mergeSort(lista, l, m)
30
-	mergeSort(lista, m+1, r)
31
-	merge(lista, l, m, r)
24
+      # Same as (l+r)//2, but avoids overflow for
25
+      # large l and h
26
+		m = l+(r-l)//2
27
+      # Sort first and second halves
28
+		mergeSort(lista, l, m)
29
+		mergeSort(lista, m+1, r)
30
+		merge(lista, l, m, r)
32 31
 
33 32
 ''' Luis Andrés López Mañán
34 33
     Program written by hand as a draft on 09/19/2022
@@ -38,55 +37,35 @@ def mergeSort(lista, l, r):
38 37
     Program wrriten and edited on 10/10/2022
39 38
 '''
40 39
 
41
-# This function heapifies a subtree rooted at an index
42
-# i. Also, n is the size of a heap and arr is array.
43
-
44
-def heapify(lista, n, i):
45
-
46
-		# largest is root for now
47
-		largest = i
48
-
49
-		# left child of root
50
-		l = 2 * i + 1
51
-
52
-		# right child of root
53
-		r = 2 * i + 2
54
-
55
-		# Checks if root has a left child and is greater than root
56
-		if l < n and lista[i] < lista[l] :
57
-			largest = l
58
-
59
-		# Checks if root has a right child and is greater than root
60
-		if r < n and lista[largest] < lista[r]:
61
-			largest = r
62
-
63
-		# If necessary, this changes root by swapping values
64
-    	if largest != i:
65
-        	lista[i], lista[largest] = lista[largest], lista[i]
66
-
67
-		# This heapifies the root repeatedly
68
-		heapify(lista, n, largest)
40
+# Python program for implementation of heap Sort (Part II)
69 41
 
70 42
 def heapSort(lista):
71
-
72
-		n = len(lista)
73
-		n2 = (n // 2) - 1
74
-		nMinus = n - 1
75
-
76
-		for i in range(n2, -1, -1):
77
-			heapify(lista, n, i)
78
-
79
-		for i in range(nMinus, -1, -1):
80
-			lista[i], lista[0] = lista[0], lista[i]
81
-			heapify(lista, i, 0)
82
-
83
-		return lista
43
+	# Se busca el tamaño de la lista
44
+	n = len(lista)
45
+	heap.heapify(lista,n,0)
46
+ 
47
+	""" Se crea un heap máximo y el último padre estará en
48
+		la posición h1, i.e., la mitad del tamaño de la lista.
49
+		Por lo tanto, ese sería el comienzo. 
50
+	"""
51
+	h1 = (n // 2) - 1
52
+	for i in range(h1, -1, -1):
53
+		heap.heapify(lista, n, i)
54
+  
55
+	# Se extrae los elementos uno a uno
56
+	h2 = n - 1
57
+	for i in range(h2, 0, -1):
58
+		# Se intercambia, luego se hace heapify
59
+		lista[i], lista[0] = lista[0], lista[i]
60
+		heap.heapify(lista, 0, i)
61
+  
62
+	return lista
84 63
 
85 64
 def quickSort(lista):
86
-	#definan el algoritmo de ordenamiento quicksort
65
+	# definan el algoritmo de ordenamiento quicksort
87 66
 	elements = len(lista)
88 67
     
89
-    #Base case
68
+    # Base case
90 69
 	if elements < 2:
91 70
 		return lista
92 71
     
@@ -115,7 +94,7 @@ def quickSort(lista):
115 94
 '''
116 95
 
117 96
 def shellSort(lista):
118
-	#definan el algoritmo de ordenamiento shellsort
97
+	# definan el algoritmo de ordenamiento shellsort
119 98
 
120 99
 	# determening the size of the list and calculates the gap value. 
121 100
 	n = len(lista)
@@ -156,24 +135,24 @@ for i in range(veces):
156 135
 	t1 = time.process_time()					#tomamos el tiempo inicial
157 136
 	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
158 137
 	acumulaMerge+=time.process_time() - t1		#acumulamos el tiempo de ejecucion
159
-	#print(mergelista)							#desplegamos la lista
138
+	# print(mergelista)							#desplegamos la lista
160 139
 
161 140
 	t1 = time.process_time()				#tomamos el tiempo inicial
162 141
 	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
163 142
 	acumulaHeap+=time.process_time() - t1 	#acumulamos el tiempo de ejecucion
164
-	#print(heaplista)						#desplegamos la lista
143
+	# print(heaplista)						#desplegamos la lista
165 144
 
166 145
 	t1 = time.process_time()				#tomamos el tiempo inicial
167 146
 	quickresult = quickSort(quicklista)					#ejecutamos el algoritmo quickSort
168 147
 	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
169
-	print(quicklista)						#desplegamos la lista
148
+	# print(quicklista)						#desplegamos la lista
170 149
 
171 150
 	t1 = time.process_time()				#tomamos el tiempo inicial
172 151
 	shellSort(searchlista)					#ejecutamos el algoritmo shellSort
173 152
 	acumulaShell+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
174
-	print(searchlista)						#desplegamos la lista
153
+	# print(searchlista)						#desplegamos la lista
175 154
 
176
-#imprimos los resultados
155
+# imprimos los resultados
177 156
 print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
178 157
 print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
179 158
 print ("HeapSort " + str(acumulaHeap/veces) + " segundos")