JS implements the array array output algorithm

  • 2020-05-17 04:45:49
  • OfStack

The example of this article describes the array array output algorithm implemented by JS. Share with you for your reference. The specific analysis is as follows:

This js code performs the full array output, improving some of the old code
If you take any m (m≤n) elements from the n different elements, and arrange them in a definite order of 1, it is called to take one arrangement of m elements from the n different elements. All permutations are called full permutations when m=n.


function permute(input) {
  var permArr = [],
  usedChars = [];
  function main(input){
    var i, ch;
    for (i = 0; i < input.length; i++) {
      ch = input.splice(i, 1)[0];
      usedChars.push(ch);
      if (input.length == 0) {
        permArr.push(usedChars.slice());
      }
      main(input);
      input.splice(i, 0, ch);
      usedChars.pop();
    }
    return permArr
  }
  return main(input);
};
console.log(permute([5, 3, 7, 1]));

I hope this article is helpful to you in javascript programming.


Related articles: