Created
August 11, 2018 23:28
-
-
Save leonardovff/2606ffe2414e23bab75de581af2784f8 to your computer and use it in GitHub Desktop.
My first permutation algorithm
This file contains 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
var data = []; | |
permutation = (arr, prefix = []) => { | |
if(arr.length == 2){ | |
let comb1 = [...prefix], | |
comb2 = [...prefix]; | |
comb1.push(arr[0]); | |
comb1.push(arr[1]); | |
data.push(comb1); | |
comb2.push(arr[1]); | |
comb2.push(arr[0]); | |
data.push(comb2); | |
} | |
for(let i = 0; i < arr.length; i++){ | |
let current = [...prefix]; | |
current.push(arr[i]); | |
let arr_temp = [...arr]; | |
arr_temp.splice(i, 1); | |
permutation(arr_temp, current); | |
} | |
} | |
permutation([1,2,3,4,5,6,7,8]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment