全排列算法 --javascript 实现

var Ann = function a(arr){

if(arr.length == 1){return arr;}

var rr = new Array();
for(var i = 0; i<arr.length;i++){

//get a copy
var ar = new Array();
for(var j = 0; j < arr.length;j++){ar[j] = arr[j];}

//assume i
var current = ar[i];
ar.splice(i,1);

var childRet = a(ar);

for(var k = 0 ;k < childRet.length;k++){
rr.push(current + "," + childRet[k]);
}

}

return rr;
}

var ret = Ann(new Array(1,2,3,4));
console.log(ret);

posted on 2014-09-29 12:07  gcczhongduan  阅读(221)  评论(0编辑  收藏  举报