5 Commits

Author SHA1 Message Date
  Alex Ortiz 213c5676e6 Merge branch 'ShellSort' into sortingFinal 2 years ago
  Alex Ortiz 41bd4aea93 Added comments to the code 2 years ago
  Alex Ortiz b2916b9a1a Added comments and modified a bit the code 2 years ago
  Alex Ortiz ccdcae09c7 ShellSort function code added 2 years ago
  Alex Ortiz 9719acd64b MergeSort function code added 2 years ago
1 changed files with 76 additions and 6 deletions
  1. 76
    6
      sorting.py

+ 76
- 6
sorting.py View File

@@ -3,7 +3,7 @@ Carlos J Corrada Bravo
3 3
 Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
4 4
 La variable maxValor define el valor maximo de los elementos de la lista
5 5
 La variable largoLista define el largo de las listas a ordenar
6
-La variable veces define las veces que se va a hacer el ordenamiento 
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 9
 from random import randint
@@ -11,7 +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
15
+	if len(lista) > 1:
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
21
+		if len(Left) != 1:
22
+			# it will do a recursive call to the function again
23
+			mergeSort(Left)
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
27
+		if len(Right) != 1:
28
+			# it will do a recursive call to the function again
29
+			mergeSort(Right)
30
+
31
+		# Variables define for while and getting space in the list
32
+        i = j = k = 0
33
+
34
+		# While i and j are less than the comparation it enters here
35
+        while i < len(Left) and j < len(Right):
36
+			# If the compared number in i is less than j it enters here
37
+            if Left[i] < Right[j]:
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
42
+            else:
43
+				# The less variable is stored in the list again in order
44
+                lista[k] = Right[j]
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
61
+
14 62
 	return lista
63
+	# Code was base and taken from GeeksforGeeks
15 64
 
16 65
 def heapSort(lista):
17 66
 	#definan el algoritmo de ordenamiento heapsort
@@ -23,11 +72,33 @@ def quickSort(lista):
23 72
 
24 73
 def shellSort(lista):
25 74
 	#definan el algoritmo de ordenamiento shellsort
75
+	Size = len(lista) # Contains the complete size of the list
76
+	Diff = Size/2 # Contains the number of half of the list
77
+
78
+	# Does a insertion sort by ordering in base of the Diff.
79
+	while Diff > 0:
80
+		# Begins a loop to sort the elements added to sorted array.
81
+		for i in range(Diff, Size):
82
+			# Saves the element sorted in a temporary variable
83
+			Tmp = lista[i]
84
+			j = i # Shifts the location of the elements
85
+			# Looks for the locations
86
+			while j >= Diff and lista[j - Diff] > Tmp:
87
+				# Gives the new element to the location
88
+				lista[j] = lista[j - Diff]
89
+				# The size of the array is ajusted
90
+				j -= Diff
91
+			# Puts the Tmp variable in is correct location
92
+			lista[j] = Tmp
93
+		# Reduces again the list
94
+		Diff /= 2
95
+
26 96
 	return lista
97
+	# Code was taken from GeeksforGeeks
27 98
 
28 99
 maxValor=1000 	#define el valor maximo de los elementos de la lista
29 100
 largoLista=1000 #define el largo de las listas a ordenar
30
-veces=100 		#define las veces que se va a hacer el ordenamiento 
101
+veces=100 		#define las veces que se va a hacer el ordenamiento
31 102
 
32 103
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
33 104
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
@@ -43,15 +114,15 @@ for i in range(veces):
43 114
 	t1 = time.clock() 				#seteamos el tiempo al empezar
44 115
 	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
45 116
 	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
46
-	
117
+
47 118
 	t1 = time.clock()				#seteamos el tiempo al empezar
48 119
 	heapSort(heaplista)					#ejecutamos el algoritmo heapSort
49 120
 	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
50
-	
121
+
51 122
 	t1 = time.clock()				#seteamos el tiempo al empezar
52 123
 	quickSort(quicklista)				#ejecutamos el algoritmo quickSort
53 124
 	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
54
-	
125
+
55 126
 	t1 = time.clock()				#seteamos el tiempo al empezar
56 127
 	shellSort(searchlista)				#ejecutamos el algoritmo shellSort
57 128
 	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
@@ -62,4 +133,3 @@ print "MergeSort " + str(acumulaMerge/veces) + " segundos"
62 133
 print "HeapSort " + str(acumulaHeap/veces) + " segundos"
63 134
 print "QuickSort " + str(acumulaQuick/veces) + " segundos"
64 135
 print "ShellSort " + str(acumulaShell/veces) + " segundos"
65
-