Browse Source

Added comments and modified a bit the code

Alex Ortiz 1 year ago
parent
commit
b2916b9a1a
1 changed files with 36 additions and 7 deletions
  1. 36
    7
      sorting.py

+ 36
- 7
sorting.py View File

@@ -11,27 +11,56 @@ import time
11 11
 
12 12
 def mergeSort(lista):
13 13
 	#definan el algoritmo de ordenamiento mergesort
14
+	# If the size of the list is greater than 1 then it enters here
14 15
 	if len(lista) > 1:
15
-		Midd = len(lista) // 2
16
-		Left = lista[:Midd]
16
+		# Gets the half of the list
17
+		Midd = len(lista) / 2
18
+		# Takes only the elements that are in the left of the list
19
+		Left = lista[Midd:]
20
+		# If the size of left still is not 1 then
17 21
 		if len(Left) != 1:
22
+			# it will do a recursive call to the function again
18 23
 			mergeSort(Left)
19
-		Right = lista[Midd:]
24
+		# Takes only the elements that are in the right of the list
25
+		Right = lista[:Midd]
26
+		# If the size of right still is not 1 then
20 27
 		if len(Right) != 1:
28
+			# it will do a recursive call to the function again
21 29
 			mergeSort(Right)
22 30
 
31
+		# Variables define for while and getting space in the list
23 32
         i = j = k = 0
24 33
 
34
+		# While i and j are less than the comparation it enters here
25 35
         while i < len(Left) and j < len(Right):
36
+			# If the compared number in i is less than j it enters here
26 37
             if Left[i] < Right[j]:
27
-                lista[k] = Right[i]
28
-                i += 1
38
+				# The less variable is stored in the list again in order
39
+                lista[k] = Left[i]
40
+                i += 1 # Increments i
41
+			# If the compared number in i is greater than j it enters here
29 42
             else:
43
+				# The less variable is stored in the list again in order
30 44
                 lista[k] = Right[j]
31
-                j += 1
32
-            k += 1
45
+                j += 1 # Increments j
46
+            k += 1 # Increments k
47
+
48
+		# If there are elements remaining in Left the they are put here
49
+        while i < len(Left):
50
+			# The variable that was remaining is put here
51
+            lista[k] = Left[i]
52
+            i += 1 # Increments i
53
+            k += 1 # Increments k
54
+
55
+		# If there are elements remaining in Right the they are put here
56
+        while j < len(Right):
57
+			# The variable that was remaining is put here
58
+            lista[k] = Right[j]
59
+            j += 1 # Increments j
60
+            k += 1 # Increments k
33 61
 
34 62
 	return lista
63
+	# Code was base and taken from GeeksforGeeks
35 64
 
36 65
 def heapSort(lista):
37 66
 	#definan el algoritmo de ordenamiento heapsort