Browse Source

Merge branch 'sortingFinal' of https://git.ccom.uprrp.edu/CCOM4030/AlAlRy into sortingFinal

ryanbarreto1 1 year ago
parent
commit
13ca9b085d
5 changed files with 99 additions and 40 deletions
  1. BIN
      __pycache__/sorting.cpython-38.pyc
  2. 25
    22
      sorting.py
  3. 64
    9
      test_sortAlgo.py
  4. BIN
      utils/__pycache__/qsortUtils.cpython-38.pyc
  5. 10
    9
      utils/qsortUtils.py

BIN
__pycache__/sorting.cpython-38.pyc View File


+ 25
- 22
sorting.py View File

@@ -16,7 +16,8 @@ def mergeSort(lista):
16 16
 	# If the size of the list is greater than 1 then it enters here
17 17
 	if len(lista) > 1:
18 18
 		# Gets the half of the list
19
-		Midd = len(lista) / 2
19
+		Midd = len(lista) // 2
20
+
20 21
 		# Takes only the elements that are in the left of the list
21 22
 		Left = lista[Midd:]
22 23
 		# If the size of left still is not 1 then
@@ -31,35 +32,35 @@ def mergeSort(lista):
31 32
 			mergeSort(Right)
32 33
 
33 34
 		# Variables define for while and getting space in the list
34
-        i = j = k = 0
35
+		i = j = k = 0
35 36
 
36 37
 		# While i and j are less than the comparation it enters here
37
-        while i < len(Left) and j < len(Right):
38
+		while i < len(Left) and j < len(Right):
38 39
 			# If the compared number in i is less than j it enters here
39
-            if Left[i] < Right[j]:
40
+			if Left[i] < Right[j]:
40 41
 				# The less variable is stored in the list again in order
41
-                lista[k] = Left[i]
42
-                i += 1 # Increments i
42
+				lista[k] = Left[i]
43
+				i += 1 # Increments i
43 44
 			# If the compared number in i is greater than j it enters here
44
-            else:
45
+			else:
45 46
 				# The less variable is stored in the list again in order
46
-                lista[k] = Right[j]
47
-                j += 1 # Increments j
48
-            k += 1 # Increments k
47
+				lista[k] = Right[j]
48
+				j += 1 # Increments j
49
+			k += 1 # Increments k
49 50
 
50 51
 		# If there are elements remaining in Left the they are put here
51
-        while i < len(Left):
52
+		while i < len(Left):
52 53
 			# The variable that was remaining is put here
53
-            lista[k] = Left[i]
54
-            i += 1 # Increments i
55
-            k += 1 # Increments k
54
+			lista[k] = Left[i]
55
+			i += 1 # Increments i
56
+			k += 1 # Increments k
56 57
 
57 58
 		# If there are elements remaining in Right the they are put here
58
-        while j < len(Right):
59
+		while j < len(Right):
59 60
 			# The variable that was remaining is put here
60
-            lista[k] = Right[j]
61
-            j += 1 # Increments j
62
-            k += 1 # Increments k
61
+			lista[k] = Right[j]
62
+			j += 1 # Increments j
63
+			k += 1 # Increments k
63 64
 
64 65
 	return lista
65 66
 	# Code was base and taken from GeeksforGeeks
@@ -110,13 +111,15 @@ def heapSort(lista):
110 111
 	return lista
111 112
 
112 113
 def quickSort(lista):
113
-	 # Se aplica quicksort a la lista desde el primer elemento hasta el último
114
-	return qsortUtils.qSort(lista, 0, len(lista) - 1)
114
+	# Se aplica quicksort a la lista desde el primer elemento hasta el último
115
+	qsortUtils.qSort(lista, 0, len(lista) - 1)
116
+	
117
+	return lista
115 118
 
116 119
 def shellSort(lista):
117 120
 	#definan el algoritmo de ordenamiento shellsort
118 121
 	Size = len(lista) # Contains the complete size of the list
119
-	Diff = Size/2 # Contains the number of half of the list
122
+	Diff = Size//2 # Contains the number of half of the list
120 123
 
121 124
 	# Does a insertion sort by ordering in base of the Diff.
122 125
 	while Diff > 0:
@@ -134,7 +137,7 @@ def shellSort(lista):
134 137
 			# Puts the Tmp variable in is correct location
135 138
 			lista[j] = Tmp
136 139
 		# Reduces again the list
137
-		Diff /= 2
140
+		Diff //= 2
138 141
 
139 142
 	return lista
140 143
 	# Code was taken from GeeksforGeeks

+ 64
- 9
test_sortAlgo.py View File

@@ -1,14 +1,69 @@
1
-from email import utils
1
+import sorting
2
+from random import randint
2 3
 
4
+DEBUG_OUTPUT = True
3 5
 
4
-from utils import qsortUtils
5
-from random import randint
6
+def test_mergeSort(pruebas):
7
+    FAIL = False
8
+
9
+    print("\nProbando MergeSort")
10
+
11
+    for nombre, lista in pruebas.items():
12
+        if sorting.mergeSort(lista) == sorted(lista):
13
+            print(f"{nombre}: " + "Pass")
14
+        else:
15
+            print(f"{nombre}: " + "Fail")
16
+            FAIL = True
17
+
18
+    if FAIL == True and DEBUG_OUTPUT == True:
19
+        print("\n Test FAIL OUTPUT for MergeSort")
20
+        for nombre, lista in pruebas.items():
21
+            print(f"{nombre}: Obtained -> {sorting.mergeSort(lista)} | Expected -> {sorted(lista)}")    
22
+
23
+def test_quickSort(pruebas):
24
+    FAIL = False
25
+
26
+    print("\nProbando QuickSort")
27
+
28
+    for nombre, lista in pruebas.items():
29
+        if sorting.quickSort(lista) == sorted(lista):
30
+            print(f"{nombre}: " + "Pass")
31
+        else:
32
+            print(f"{nombre}: " + "Fail")
33
+            FAIL = True
34
+
35
+    if FAIL == True and DEBUG_OUTPUT == True:
36
+        print("\n Test FAIL OUTPUT for QuickSort")
37
+        for nombre, lista in pruebas.items():
38
+            print(f"{nombre}: Obtained -> {sorting.quickSort(lista)} | Expected -> {sorted(lista)}")   
39
+
40
+def test_shellSort(pruebas):
41
+    FAIL = False
42
+
43
+    print("\nProbando ShellSort")
44
+
45
+    for nombre, lista in pruebas.items():
46
+        if sorting.shellSort(lista) == sorted(lista):
47
+            print(f"{nombre}: " + "Pass")
48
+        else:
49
+            print(f"{nombre}: " + "Fail")
50
+            FAIL = True
6 51
 
7
-enteros = [0,7,3,1,4,5,2]
8
-negativos = [8,-1,3,2,-4,9]
9
-aleatoria =[randint(-49,50) for r in range(10)]
52
+    if FAIL == True and DEBUG_OUTPUT == True:
53
+        print("\n Test FAIL OUTPUT for ShellSort")
54
+        for nombre, lista in pruebas.items():
55
+            print(f"{nombre}: Obtained -> {sorting.shellSort(lista)} | Expected -> {sorted(lista)}")   
10 56
 
11
-print(qsortUtils.qSort(enteros,0,len(enteros)-1) == enteros.sort())
12
-print(qsortUtils.qSort(negativos,0,len(negativos)-1) == negativos.sort())
13
-print(qsortUtils.qSort(aleatoria,0,len(aleatoria)-1) == aleatoria.sort())
57
+def main():
58
+    PRUEBAS = {"Enteros":[0,7,3,1,4,5,2],
59
+               "Negativos":[8,-1,3,2,-4,9],
60
+               "Aleatorios":[randint(-49,50) for r in range(10)],
61
+               "Vacia":[],
62
+               "Ordenada":[1,2,3,4,5,6,7]}
63
+    
64
+    test_mergeSort(PRUEBAS)
65
+    test_quickSort(PRUEBAS)
66
+    test_shellSort(PRUEBAS)
14 67
 
68
+if __name__ == "__main__":
69
+    main()

BIN
utils/__pycache__/qsortUtils.cpython-38.pyc View File


+ 10
- 9
utils/qsortUtils.py View File

@@ -1,24 +1,25 @@
1 1
 # Función partition: Selecciona el pivote y divide la lista en dos conjuntos: Elementos menores o iguales al pivote y elementos mayores al pivote
2
-def partition(lista, p, r): 
2
+def partition(A, p, r): 
3 3
 
4
-  x = lista[r] # Selección del último elemento en la lista como el pivote
4
+  x = A[r] # Selección del último elemento en la lista como el pivote
5 5
   i = p - 1 # El conjunto de elementos menores o iguales al pivote está inicialmente vacío
6 6
 
7 7
   for j in range (p, r):
8 8
 
9
-    if lista[j] <= x: # Comparación de elemento en la lista con el pivote
9
+    if A[j] <= x: # Comparación de elemento en la lista con el pivote
10 10
       i += 1 # Hacer espacio para el elemento A[j], se coloca un espacio luego del último elemento del conjunto de elementos menores o iguales al pivote.
11
-      lista[j], lista[i] = lista[i], lista[j] #Swap A[j] con A[i]
11
+      A[j], A[i] = A[i], A[j] #Swap A[j] con A[i]
12 12
 
13
-  lista[i+1], lista[r] = lista[r], lista[i+1] # Colocar el pivote como último elemento del conjunto de menores o iguales
13
+  A[i+1], A[r] = A[r], A[i+1] # Colocar el pivote como último elemento del conjunto de menores o iguales
14 14
 
15 15
   return i + 1 # Devuelve el índice del pivote actual
16 16
 
17 17
 # Función qSort: Aplica el algoritmo de Quicksort a una lista
18
-def qSort(lista, p, r):
18
+def qSort(A, p, r):
19 19
 
20 20
   if p < r: # Caso Base: Se llegó a un elemento individual. La lista es un solo elemento
21
-    q = partition(lista, p, r) # Divide/Combine: Particiona la lista y se obtiene el índice del pivote
22
-    qSort(lista, p, q - 1) # Conquer: Se aplica qSort al conjunto de elementos menores o iguales al pivote actual
23
-    qSort(lista, q + 1, r) # Conquer: Se aplica qSort al conjunto de elementos mayores al pivote actual
21
+    q = partition(A, p, r) # Divide/Combine: Particiona la lista y se obtiene el índice del pivote
22
+    qSort(A, p, q - 1) # Conquer: Se aplica qSort al conjunto de elementos menores o iguales al pivote actual
23
+    qSort(A, q + 1, r) # Conquer: Se aplica qSort al conjunto de elementos mayores al pivote actual
24
+
24 25