0

Write bubblesort for R with a variation. This bubblesort should start sorting from the last element first. First describe this algorithm in your own words.

x<-c(21,45,55,67,13,11,2,3,81,44)
example <- function(x){
  n<-length(x)
  for(j in length(x)-1):1){
    for(i in (length(x)-1)){
      if(x[i]>x[i+1]){
        temp<-x[i]
        x[i]<-x[i+1]
        x[i+1]<-temp
      }
    }
  }
  return(x)
}
res<-example(x)
res 
  • Hello, it is better if you add code directly into your question, without the picture. Just paste it from R, select it, and press strg+k. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – desval May 01 '20 at 14:40
  • Thank you! I will update – Daniel Garaycochea May 01 '20 at 16:46

0 Answers0