Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- selection <- function(s) {
- minimo <- min(s)
- resto <- s[s != minimo]
- if (length(resto) > 1) {
- resto <- selection(s[s != minimo])
- }
- c(minimo, resto)
- }
- quicksort <- function(q) {
- meio <- sample(q, 1)
- esquerda <- c()
- direita <- c()
- lapply(q[q != meio], function(d) {
- if (d < meio) {
- esquerda <<- c(esquerda, d)
- }
- else {
- direita <<- c(direita, d)
- }
- })
- if (length(esquerda) > 1) {
- esquerda <- quicksort(esquerda)
- }
- if (length(direita) > 1) {
- direita <- quicksort(direita)
- }
- # Finally, return the sorted values.
- c(esquerda, meio, direita)
- }
- vetor <- rnorm(50, mean = 0, sd = 1)
- selection(vetor)
- quicksort(vetor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement