Skip to content

Instantly share code, notes, and snippets.

@kai5263499
Created November 27, 2014 00:37
Show Gist options
  • Save kai5263499/5ea926f6cea6a12242d7 to your computer and use it in GitHub Desktop.
Save kai5263499/5ea926f6cea6a12242d7 to your computer and use it in GitHub Desktop.
// Permute the string using Heap's algorithm https://en.wikipedia.org/wiki/Heap%27s_algorithm
var perm = function(list, n){
if(n==1){
var inputString = list.join('');
console.log(inputString);
} else {
for(var c=0;c<n;c++){
perm(list,n-1);
if(n%2===0){
var temp1=list[c];
list[c]=list[list.length-1];
list[list.length-1]=temp1;
}else{
var temp2=list[0];
list[0]=list[list.length-1];
list[list.length-1]=temp2;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment