Created
April 10, 2017 18:04
-
-
Save slofurno/680d58bde27118b6d58ee4e4fc364335 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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