R/OneSwap.R

Defines functions oneswap

Documented in oneswap

# Check if a vector can be sorted with at most one swap function

#' Title
#'
#' @param x a numeric vector
#'
#' @return True if vector can be ordered with at most one swap
#' @export
#'
#' @examples  oneswap(c(1,2,3,4,5))
#'
#'
#'
oneswap <- function(x) {
  for (i in (1:length(x))){
    for (j in (i:length(x))){
      y<-x
      y[j]<-x[i]
      y[i]<-x[j]
      if (isTRUE(all.equal(sort(x),y))){
        return(TRUE)
      }
    }
  }
  return(FALSE)
}
Michael-Whitehouse/OneSwapSC1 documentation built on Oct. 30, 2019, 9:27 p.m.