Advertisement
Dlisstnop

Untitled

Oct 28th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. x<-c(4,7,2,9,1,5,3,23,6)
  2.  
  3. temp<-c()
  4. n<-length(x)
  5. decreasing=T
  6. SelectSort<-function(x,decreasing=F){
  7. for(i in 1:(n-1)){
  8. nmin<-i
  9. for(j in (i+1):n){
  10. if(x[j]<x[nmin]){
  11. nmin<-j
  12. }
  13. }
  14. temp<-x[i]
  15. x[i]<-x[nmin]
  16. x[nmin]<-temp
  17. }
  18. else{
  19. nmax<-i
  20. for(j in (i+1):n){
  21. if(x[j]>x[nmax]){
  22. nmax<-j
  23. }
  24. temp<-x[i]
  25. x[i]<-x[nmax]
  26. x[nmax]<-temp
  27. }
  28. }
  29. } print(x)
  30.  
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement