浏览代码

Implementar Shell sort; corregir copias de listas

父节点
当前提交
b911d8b007
共有 1 个文件被更改,包括 45 次插入7 次删除
  1. 45
    7
      sorting.py

+ 45
- 7
sorting.py 查看文件

10
 from random import randint
10
 from random import randint
11
 import time
11
 import time
12
 
12
 
13
+def isSorted(lista):
14
+    return lista == sorted(lista)
15
+
13
 def mergeSort(lista):
16
 def mergeSort(lista):
14
 	#definan el algoritmo de ordenamiento mergesort
17
 	#definan el algoritmo de ordenamiento mergesort
15
 	return lista
18
 	return lista
22
 	#definan el algoritmo de ordenamiento quicksort
25
 	#definan el algoritmo de ordenamiento quicksort
23
 	return lista
26
 	return lista
24
 
27
 
28
+def insertionSort(lista):
29
+
30
+    i = 1
31
+    while i < len(lista):
32
+        if lista[i - 1] > lista[i]:
33
+            j = i - 1
34
+            while j >= 0 and lista[j] > lista[j + 1]:
35
+                lista[j], lista[j + 1] = lista[j + 1], lista[j]
36
+                j -= 1
37
+        i += 1
38
+
39
+    return lista
40
+
25
 def shellSort(lista):
41
 def shellSort(lista):
26
-	#definan el algoritmo de ordenamiento shellsort
27
-	return lista
42
+
43
+    gap = len(lista) / 2
44
+    while gap >= 1:
45
+        i = gap
46
+        while i < len(lista):
47
+            if lista[i - gap] > lista[i]:
48
+                j = i - gap
49
+                while j >= 0 and lista[j] > lista[j + gap]:
50
+                    lista[j], lista[j + gap] = lista[j + gap], lista[j]
51
+                    j -= gap
52
+            i += gap
53
+        gap /= 2
54
+
55
+    #if isSorted(lista):
56
+        #print "Lista is sorted"
57
+    #else:
58
+        #print "Lista is not sorted"
59
+
60
+    return lista
28
 
61
 
29
 maxValor=1000 	#define el valor maximo de los elementos de la lista
62
 maxValor=1000 	#define el valor maximo de los elementos de la lista
30
 largoLista=1000 #define el largo de las listas a ordenar
63
 largoLista=1000 #define el largo de las listas a ordenar
38
 for i in range(veces):
71
 for i in range(veces):
39
 	lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
72
 	lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
40
 
73
 
74
+        # creamos copias de la lista para cada algoritmo
75
+        listaMerge = lista[:]
76
+        listaHeap = lista[:]
77
+        listaQuick = lista[:]
78
+        listaShell = lista[:]
79
+
41
 	t1 = time.clock() 				#seteamos el tiempo al empezar
80
 	t1 = time.clock() 				#seteamos el tiempo al empezar
42
-	mergeSort(lista) 				#ejecutamos el algoritmo mergeSort
81
+	mergeSort(listaMerge) 				#ejecutamos el algoritmo mergeSort
43
 	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
82
 	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
44
 	
83
 	
45
 	t1 = time.clock()				#seteamos el tiempo al empezar
84
 	t1 = time.clock()				#seteamos el tiempo al empezar
46
-	heapSort(lista)					#ejecutamos el algoritmo heapSort
85
+	heapSort(listaHeap)					#ejecutamos el algoritmo heapSort
47
 	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
86
 	acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
48
 	
87
 	
49
 	t1 = time.clock()				#seteamos el tiempo al empezar
88
 	t1 = time.clock()				#seteamos el tiempo al empezar
50
-	quickSort(lista)				#ejecutamos el algoritmo quickSort
89
+	quickSort(listaQuick)				#ejecutamos el algoritmo quickSort
51
 	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
90
 	acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
52
 	
91
 	
53
 	t1 = time.clock()				#seteamos el tiempo al empezar
92
 	t1 = time.clock()				#seteamos el tiempo al empezar
54
-	shellSort(lista)				#ejecutamos el algoritmo shellSort
93
+	shellSort(listaShell)				#ejecutamos el algoritmo shellSort
55
 	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
94
 	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
56
 
95
 
57
 #imprimos los resultados
96
 #imprimos los resultados
60
 print "HeapSort " + str(acumulaHeap/veces) + " segundos"
99
 print "HeapSort " + str(acumulaHeap/veces) + " segundos"
61
 print "QuickSort " + str(acumulaQuick/veces) + " segundos"
100
 print "QuickSort " + str(acumulaQuick/veces) + " segundos"
62
 print "ShellSort " + str(acumulaShell/veces) + " segundos"
101
 print "ShellSort " + str(acumulaShell/veces) + " segundos"
63
-