Pārlūkot izejas kodu

Revert "Adding mergeSort code and merge.py which contains some extra code for mergeSort. Removing time.clock() because it was depreciated in favor of time.process_time()."

This reverts commit 32db0cd933.
Jeann-Carlos 2 gadus atpakaļ
vecāks
revīzija
6cbd05724d
2 mainītis faili ar 25 papildinājumiem un 89 dzēšanām
  1. 0
    45
      merge.py
  2. 25
    44
      sorting.py

+ 0
- 45
merge.py Parādīt failu

@@ -1,45 +0,0 @@
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

+ 25
- 44
sorting.py Parādīt failu

@@ -8,25 +8,10 @@ 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
12 11
 
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)
12
+def mergeSort(lista):
13
+	#definan el algoritmo de ordenamiento mergesort
14
+	return lista
30 15
 
31 16
 def heapSort(lista):
32 17
 	#definan el algoritmo de ordenamiento heapsort
@@ -41,8 +26,8 @@ def shellSort(lista):
41 26
 	return lista
42 27
 
43 28
 maxValor=1000 	#define el valor maximo de los elementos de la lista
44
-largoLista=10 	#define el largo de las listas a ordenar
45
-veces=1    		#define las veces que se va a hacer el ordenamiento
29
+largoLista=1000 #define el largo de las listas a ordenar
30
+veces=100 		#define las veces que se va a hacer el ordenamiento 
46 31
 
47 32
 acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
48 33
 acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
@@ -55,30 +40,26 @@ for i in range(veces):
55 40
 	quicklista=list(mergelista)
56 41
 	searchlista=list(mergelista)
57 42
 
58
-	t1 = time.process_time()					#tomamos el tiempo inicial
59
-	mergeSort(mergelista,0,len(mergelista)-1) 	#ejecutamos el algoritmo mergeSort
60
-	acumulaMerge+=time.process_time() - t1		#acumulamos el tiempo de ejecucion
61
-	print(mergelista)							#desplegamos la lista
62
-
63
-	t1 = time.process_time()				#tomamos el tiempo inicial
64
-	heapSort(heaplista)					    #ejecutamos el algoritmo heapSort
65
-	acumulaHeap+=time.process_time() - t1 	#acumulamos el tiempo de ejecucion
66
-	print(heaplista)						#desplegamos la lista
67
-
68
-	t1 = time.process_time()				#tomamos el tiempo inicial
69
-	quickSort(quicklista)					#ejecutamos el algoritmo quickSort
70
-	acumulaQuick+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
71
-	print(quicklista)						#desplegamos la lista
72
-
73
-	t1 = time.process_time()				#tomamos el tiempo inicial
74
-	shellSort(searchlista)					#ejecutamos el algoritmo shellSort
75
-	acumulaShell+=time.process_time() - t1	#acumulamos el tiempo de ejecucion
76
-	print(searchlista)						#desplegamos la lista
43
+	t1 = time.clock() 				#seteamos el tiempo al empezar
44
+	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
45
+	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
46
+	
47
+	t1 = time.clock()				#seteamos el tiempo al empezar
48
+	heapSort(heaplista)					#ejecutamos el algoritmo heapSort
49
+	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
50
+	
51
+	t1 = time.clock()				#seteamos el tiempo al empezar
52
+	quickSort(quicklista)				#ejecutamos el algoritmo quickSort
53
+	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
54
+	
55
+	t1 = time.clock()				#seteamos el tiempo al empezar
56
+	shellSort(searchlista)				#ejecutamos el algoritmo shellSort
57
+	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
77 58
 
78 59
 #imprimos los resultados
79
-print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
80
-print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
81
-print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
82
-print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
83
-print ("ShellSort " + str(acumulaShell/veces) + " segundos")
60
+print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
61
+print "MergeSort " + str(acumulaMerge/veces) + " segundos"
62
+print "HeapSort " + str(acumulaHeap/veces) + " segundos"
63
+print "QuickSort " + str(acumulaQuick/veces) + " segundos"
64
+print "ShellSort " + str(acumulaShell/veces) + " segundos"
84 65