function (N = 13) { if (N == 2) return(c(1, 2)) if (N == 3) return(c(1, 3, 2)) if (N == 4) return(c(1, 4, 3, 2)) if (N == 5) return(c(1, 4, 5, 3, 2)) FLAG = F if (N%%2 == 0) { FLAG = T N = N + 1 } k = N - 1 z = rep(c(1, 3, 3, 1), length.out = k) z = matrix(z, byrow = T, ncol = 2) z = apply(z, 2, cumsum) z01 = matrix(rep(c(1, 0), length.out = k), byrow = T, ncol = 2) y = z + z01 y = t(apply(y, 1, rev)) if (k%%2 == 0) { m = k/2 if (m%%2 == 0) { mat = matrix(1:m, ncol = 2) } else { mat = matrix(rep(1:m, length.out = m + 1), ncol = 2) } mat = mat[, c(2, 1)] mx = nrow(mat) mat[, 1] = rev(mat[, 1]) if (m%%2 != 0) { mat[1:(mx - 1), 1] = mat[2:mx, 1] mat[mx, 1] = NA } xord = as.vector(t(mat)) xord = xord[!is.na(xord)] } else { k = k + 1 m = k/2 mat = matrix(1:m, ncol = 2) mat = mat[, c(2, 1)] mat[, 1] = rev(mat[, 1]) xord = as.vector(t(mat)) xord = xord[-length(xord)] } y[xord, ] = y y = as.vector(t(y)) if (N%%2 == 1) y = c(1, y) if (FLAG == T) y = y[y < max(y)] y }