2 Commits

Autor SHA1 Mensagem Data
  Jeann-Carlos 71829ca50b Fixing merge print. 2 anos atrás
  Jeann-Carlos 46dfca5148 Adding mergeSort code and merge.py which contains some extra code for mergeSort. 2 anos atrás
2 arquivos alterados com 66 adições e 5 exclusões
  1. 45
    0
      merge.py
  2. 21
    5
      sorting.py

+ 45
- 0
merge.py Ver arquivo

@@ -0,0 +1,45 @@
1
+def merge(arr, l, m, r):
2
+	n1 = m - l + 1
3
+	n2 = r - m
4
+
5
+	# create temp arrays
6
+	L = [0] * (n1)
7
+	R = [0] * (n2)
8
+
9
+	# Copy data to temp arrays L[] and R[]
10
+	for i in range(0, n1):
11
+		L[i] = arr[l + i]
12
+
13
+	for j in range(0, n2):
14
+		R[j] = arr[m + 1 + j]
15
+
16
+	# Merge the temp arrays back into arr[l..r]
17
+	i = 0	 # Initial index of first subarray
18
+	j = 0	 # Initial index of second subarray
19
+	k = l	 # Initial index of merged subarray
20
+
21
+	while i < n1 and j < n2:
22
+		if L[i] <= R[j]:
23
+			arr[k] = L[i]
24
+			i += 1
25
+		else:
26
+			arr[k] = R[j]
27
+			j += 1
28
+		k += 1
29
+
30
+	# Copy the remaining elements of L[], if there
31
+	# are any
32
+	while i < n1:
33
+		arr[k] = L[i]
34
+		i += 1
35
+		k += 1
36
+
37
+	# Copy the remaining elements of R[], if there
38
+	# are any
39
+	while j < n2:
40
+		arr[k] = R[j]
41
+		j += 1
42
+		k += 1
43
+
44
+# l is for left index and r is right index of the
45
+# sub-array of arr to be sorted

+ 21
- 5
sorting.py Ver arquivo

@@ -8,10 +8,25 @@ Al final se imprimen los promedios de cada algortimo
8 8
 """
9 9
 from random import randint
10 10
 import time
11
+from merge import merge
11 12
 
12
-def mergeSort(lista,start,end):
13
-	#definan el algoritmo de ordenamiento mergesort
14
-	return lista
13
+# Python program for implementation of MergeSort
14
+
15
+# Merges two subarrays of arr[].
16
+# First subarray is arr[l..m]
17
+# Second subarray is arr[m+1..r]
18
+
19
+def mergeSort(lista, l, r):
20
+	if l < r:
21
+
22
+		# Same as (l+r)//2, but avoids overflow for
23
+		# large l and h
24
+		m = l+(r-l)//2
25
+
26
+		# Sort first and second halves
27
+		mergeSort(lista, l, m)
28
+		mergeSort(lista, m+1, r)
29
+		merge(lista, l, m, r)
15 30
 
16 31
 def heapSort(lista):
17 32
 	#definan el algoritmo de ordenamiento heapsort
@@ -26,8 +41,8 @@ def shellSort(lista):
26 41
 	return lista
27 42
 
28 43
 maxValor=1000 	#define el valor maximo de los elementos de la lista
29
-largoLista=1000 #define el largo de las listas a ordenar
30
-veces=100 		#define las veces que se va a hacer el ordenamiento 
44
+largoLista=1000 	#define el largo de las listas a ordenar
45
+veces=100    		#define las veces que se va a hacer el ordenamiento
31 46
 
32 47
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
33 48
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
@@ -66,3 +81,4 @@ print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
66 81
 print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
67 82
 print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
68 83
 print ("ShellSort " + str(acumulaShell/veces) + " segundos")
84
+