Skip to content

Instantly share code, notes, and snippets.

@dogterbox
Last active January 25, 2019 13:56
Show Gist options
  • Save dogterbox/7524aeb1b87850eeb7980780a2f2814e to your computer and use it in GitHub Desktop.
Save dogterbox/7524aeb1b87850eeb7980780a2f2814e to your computer and use it in GitHub Desktop.
selectionSort <- function(num) {
for (i in seq(1, (length(num) - 1))) {
index <- i # index of min number
for (j in seq(i, length(num))) {
if (num[j] < num[index]) {
index <- j
}
}
# swap number
minNum <- num[index]
num[index] <- num[i]
num[i] <- minNum
}
return(num)
}
randNum <- function(n, start, stop) {
rand <- round(runif(n, start, stop))
return(c(rand))
}
num <- randNum(100, -50, 50)
cat("num: ", num, "\t")
cat("num sorted: ", selectionSort(num))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment