-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast-sort.R
More file actions
193 lines (159 loc) · 4.39 KB
/
fast-sort.R
File metadata and controls
193 lines (159 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
### Mergesort ###
mergesort<-function(x){
n<-length(x)
if(n <= 1){
return(x)
}
medio<-round(n/2)
left<-x[1:medio]
right<-x[(medio+1):n]
left<-mergesort(left)
right<-mergesort(right)
if(left[length(left)] <= right[1])
return(c(left, right))
res<-merge(left, right)
return(res)
}
merge<-function(left, right){
result<-c(0)
while((length(left) > 0) && (length(right) > 0)){
if(left[1] <= right[1]){
result<-c(result, left[1])
left<-left[-1]
}else{
result<-c(result, right[1])
right<-right[-1]
}
}
if(length(left) > 0){
result<-c(result, left)
}
if(length(right) > 0){
result<-c(result, right)
}
return(result[-1])
}
### QuickSort ###
quicksort<-function(x, low, high){
if(low < high){
list_particion<-particion(x, low, high)
p<-list_particion[['p']]
x<-list_particion[['x']]
x<-quicksort(x, low, p-1)
x<-quicksort(x, p+1, high)
}
return(x)
}
particion<-function(x, low, high){
pivot <- x[high]
i <- low
for(j in low:high){
if(x[j] < pivot){
x<-swap(x, i, j)
i<-i+1
}
}
x<-swap(x, i, high)
return(list('x'=x, 'p'=i))
}
swap<-function(x, i, j){
temp<-x[i]
x[i]<-x[j]
x[j]<-temp
return(x)
}
# Funcion que crea el heap dado un array
heapbuilding<-function(arr){
n <- length(arr)
# Nos copiamos el array en un heap
heap <- arr
# Recorremos el array
for(j in n:1){
# Vamos insertando los elementos del array en el heap
heap<-modifyheap(heap, j)
}
return(heap)
}
# Funcion de reorganizacion del heap al eliminar o meter un elemento
modifyheap<-function(heap, root_i){
n <- length(heap)
# Flag para indicar cuando acabar el while
flag <- TRUE
while(((root_i * 2) <= n) & flag){
# Calculamos cuales serian los iondices de los hijos
left_i <- root_i * 2
right_i <- root_i * 2 + 1
# Ponemos el flag false para parar el while en caso de que este el heap bien construido
flag <- FALSE
# Cogemos a los hijos del root actual
son <- c(heap[left_i],heap[right_i])
son <- son[!is.na(son)]
# Buscamos los hijos en el heap y nos quedamos el menor
min_ind = which.min(son)
# Si el menor hijo es mayor que el root los intercambiamos y seguimos en el while para que se recoloquen el resto
# Tambien cambiamos de root al hijo menor
if(heap[root_i] > son[min_ind]){
flag <- TRUE
heap_ind <- c(left_i,right_i)[min_ind]
tmp <- heap[heap_ind]
heap[heap_ind] <- heap[root_i]
heap[root_i] <- tmp
root_i <- heap_ind
}
}
return(heap)
}
# Funcion que dado un heap lo devulve ordenado
heapsortutil<-function(heap){
# Inicializamos la ordenacion
sorted <- NULL
n <- length(heap)
# recorremos el heap entero
while(n > 0){
# Inseramos el primer elemento del heap
sorted <- c(sorted, heap[1])
# Sacamos el elemento del heap
n <- length(heap)
heap[1] <- heap[n]
heap <- heap[1:(n-1)]
# Reorganizamos el heap (siempre raiz el primer elemento)
heap<-modifyheap(heap, root_i = 1)
n <- n - 1
}
return(sorted)
}
heapsort<-function(arr){
# Insertamos todos los elementos en el heap
heap<-heapbuilding(arr)
# Devolvemos el " heap ordenado "
return(heapsortutil(heap))
}
### Medir tiempos ###
medir_tiempos<-function(lengths){
tiempos_mergesort<-c(0)
tiempos_quicksort<-c(0)
tiempos_heapsort <-c(0)
for(l in lengths){
min<-(-(l*10))
max<-(l*10)
print(paste('Generando vector de ',l,' enteros entre ', min ,' y ', max))
x<-round(runif(l, min, max))
print(paste('Generado'))
print(paste('Midiendo tiempo mergesort'))
time <- system.time(mergesort(x))[3]
print(paste('Tiempo medido mergesort: ', time, 's'))
tiempos_mergesort<-c(tiempos_mergesort, time)
print(paste('Midiendo tiempo quicksort'))
time <- system.time(quicksort(x, 1, l))[3]
print(paste('Tiempo medido quicksort: ', time, 's'))
tiempos_quicksort<-c(tiempos_quicksort, time)
print(paste('Midiendo tiempo heapsort'))
time <- system.time(heapsort(x))[3]
print(paste('Tiempo medido heapsort: ', time, 's'))
tiempos_heapsort<-c(tiempos_heapsort, time)
print('#########################################')
}
return(list("mergesort"=tiempos_mergesort[-1],
"quicksort"=tiempos_quicksort[-1],
"heapsort"=tiempos_heapsort[-1]))
}