Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.72 KB | None | 0 0
  1. selection <- function(s) {
  2.   minimo <- min(s)
  3.   resto <- s[s != minimo]
  4.     if (length(resto) > 1) {
  5.     resto <- selection(s[s != minimo])
  6.   }
  7.   c(minimo, resto)
  8. }
  9.  
  10. quicksort <- function(q) {
  11.   meio <- sample(q, 1)
  12.  
  13.   esquerda <- c()
  14.   direita <- c()
  15.  
  16.   lapply(q[q != meio], function(d) {
  17.     if (d < meio) {
  18.       esquerda <<- c(esquerda, d)
  19.     }
  20.     else {
  21.       direita <<- c(direita, d)
  22.     }
  23.   })
  24.  
  25.   if (length(esquerda) > 1) {
  26.     esquerda <- quicksort(esquerda)
  27.   }
  28.  
  29.   if (length(direita) > 1) {
  30.     direita <- quicksort(direita)
  31.   }
  32.  
  33.   # Finally, return the sorted values.
  34.   c(esquerda, meio, direita)
  35. }
  36.  
  37. vetor <- rnorm(50, mean = 0, sd = 1)
  38. selection(vetor)
  39. quicksort(vetor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement