Skip to content

Instantly share code, notes, and snippets.

@leonardovff
Created August 11, 2018 23:28
Show Gist options
  • Save leonardovff/2606ffe2414e23bab75de581af2784f8 to your computer and use it in GitHub Desktop.
Save leonardovff/2606ffe2414e23bab75de581af2784f8 to your computer and use it in GitHub Desktop.
My first permutation algorithm
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