2 Incheckningar

Upphovsman SHA1 Meddelande Datum
  Carla Ramos 60c0cfb1a0 Add new comments 1 år sedan
  Carla Ramos fc259adbfe Implementation of MergeSort algorithm -Carla Ramos 1 år sedan
1 ändrade filer med 44 tillägg och 1 borttagningar
  1. 44
    1
      sorting.py

+ 44
- 1
sorting.py Visa fil

@@ -6,12 +6,55 @@ La variable largoLista define el largo de las listas a ordenar
6 6
 La variable veces define las veces que se va a hacer el ordenamiento 
7 7
 Al final se imprimen los promedios de cada algortimo
8 8
 """
9
+from heapq import merge
9 10
 from random import randint
10 11
 import time
11 12
 
13
+
12 14
 def mergeSort(lista):
13 15
 	#definan el algoritmo de ordenamiento mergesort
14
-	return lista
16
+	# Carla Ramos Bezares
17
+	# Para realizar este código leí las explicaciones e implementaciones que ofrecen
18
+	# GeeksforGeeks y Progamiz
19
+
20
+	# check if the array has more than one element
21
+    if len(lista) > 1:
22
+        # divide the array in two halves
23
+        middle = len(lista)//2
24
+        leftHalf = lista[:middle]
25
+        rightHalf = lista[middle:]
26
+        
27
+        mergeSort(leftHalf)
28
+        mergeSort(rightHalf)
29
+        # declare pointers
30
+
31
+        i = j = k = 0
32
+
33
+		# using our "smaller" arrays, place elements in the correct position of our array
34
+
35
+        while i < len(leftHalf) and j < len(rightHalf):
36
+            if leftHalf[i] < rightHalf[j]:
37
+                lista[k] = leftHalf[i]
38
+                i = i + 1
39
+
40
+            else:
41
+                lista[k] = rightHalf[j]
42
+                j = j + 1
43
+           
44
+            k = k + 1
45
+
46
+        # continue updating array grabbing any elements that were left
47
+        while i < len(leftHalf):
48
+            lista[k] = leftHalf[i]
49
+            i = i + 1
50
+            k = k + 1
51
+
52
+        while j < len(rightHalf):
53
+            lista[k] = rightHalf[j]
54
+            j = j + 1
55
+            k = k + 1
56
+
57
+    return lista
15 58
 
16 59
 def heapSort(lista):
17 60
 	#definan el algoritmo de ordenamiento heapsort