Advertisement
Dlisstnop

Untitled

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