Skip to content

Instantly share code, notes, and snippets.

@slofurno
Created April 10, 2017 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slofurno/680d58bde27118b6d58ee4e4fc364335 to your computer and use it in GitHub Desktop.
Save slofurno/680d58bde27118b6d58ee4e4fc364335 to your computer and use it in GitHub Desktop.
function invert(xs) {
let j = 1
while(j < xs.length) {
let start = j
let end = 2*j
while(end > start) {
swap(xs, start, end)
end--
start++
}
j = 2*j+1
}
}
function swap(xs, i, j) {
let t = xs[i]
xs[i] = xs[j]
xs[j] = t
}
function print(arr, n) {
n = n || 0
if (n >= arr.length) { return }
console.log(arr.slice(n, n*2+1).reduce((a,c) => a + " " + c))
print(arr, n*2+1)
}
let xs = [4,2,7,1,3,6,9]
print(xs)
invert(xs)
print(xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment