


Detailed explanation of several recursive total permutation algorithms in JavaScript
Exchange (recursive)
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Full Permutation(Recursive Swap) - Mengliao Software</title> </head> <body> <p>Full Permutation(Recursive Swap)<br /> Mengliao Software Studio - Bosun Network Co., Ltd.<br /> 2011.05.24</p> <script type="text/javascript"> /* 全排列(递归交换)算法 1、将第一个位置分别放置各个不同的元素; 2、对剩余的位置进行全排列(递归); 3、递归出口为只对一个元素进行全排列。 */ function swap(arr,i,j) { if(i!=j) { var temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } var count=0; function show(arr) { document.write("P<sub>"+ ++count+"</sub>: "+arr+"<br />"); } function perm(arr) { (function fn(n) { //为第n个位置选择元素 for(var i=n;i<arr.length;i++) { swap(arr,i,n); if(n+1<arr.length-1) //判断数组中剩余的待全排列的元素是否大于1个 fn(n+1); //从第n+1个下标进行全排列 else show(arr); //显示一组结果 swap(arr,i,n); } })(0); } perm(["e1","e2","e3","e4"]); </script> </body> </html>
Link (recursive)
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Full Permutation(Recursive Link) - Mengliao Software</title> </head> <body> <p>Full Permutation(Recursive Link)<br /> Mengliao Software Studio - Bosun Network Co., Ltd.<br /> 2012.03.29</p> <script type="text/javascript"> /* 全排列(递归链接)算法 1、设定源数组为输入数组,结果数组存放排列结果(初始化为空数组); 2、逐一将源数组的每个元素链接到结果数组中(生成新数组对象); 3、从原数组中删除被链接的元素(生成新数组对象); 4、将新的源数组和结果数组作为参数递归调用步骤2、3,直到源数组为空,则输出一个排列。 */ var count=0; function show(arr) { document.write("P<sub>"+ ++count+"</sub>: "+arr+"<br />"); } function perm(arr) { (function fn(source, result) { if (source.length == 0) show(result); else for (var i = 0; i < source.length; i++) fn(source.slice(0, i).concat(source.slice(i + 1)), result.concat(source[i])); })(arr, []); } perm(["e1", "e2", "e3", "e4"]); </script> </body> </html>
Backtracking (recursive)
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Full Permutation(Recursive Backtrack) - Mengliao Software</title> </head> <body> <p>Full Permutation(Recursive Backtrack)<br /> Mengliao Software Studio - Bosun Network Co., Ltd.<br /> 2012.03.29</p> <script type="text/javascript"> /* 全排列(递归回溯)算法 1、建立位置数组,即对位置进行排列,排列成功后转换为元素的排列; 2、建立递归函数,用来搜索第n个位置; 3、第n个位置搜索方式与八皇后问题类似。 */ var count = 0; function show(arr) { document.write("P<sub>" + ++count + "</sub>: " + arr + "<br />"); } function seek(index, n) { if (n >= 0) //判断是否已回溯到了第一个位置之前,即已经找到了所有位置排列 if (index[n] < index.length - 1) { //还有下一个位置可选 index[n]++; //选择下一个位置 if ((function () { //该匿名函数判断该位置是否已经被选择过 for (var i = 0; i < n; i++) if (index[i] == index[n]) return true; //已选择 return false; //未选择 })()) return seek(index, n); //重新找位置 else return true; //找到 } else { //当前无位置可选,进行递归回溯 index[n] = -1; //取消当前位置 if (seek(index, n - 1)) //继续找上一个位置 return seek(index, n); //重新找当前位置 else return false; //已无位置可选 } else return false; } function perm(arr) { var index = new Array(arr.length); for (var i = 0; i < index.length; i++) index[i] = -1; //初始化所有位置为-1,以便++后为0 for (i = 0; i < index.length - 1; i++) seek(index, i); //先搜索前n-1个位置 while (seek(index, index.length - 1)) { //不断搜索第n个位置,即找到所有位置排列 var temp = []; for (i = 0; i < index.length; i++) //将位置之转换为元素 temp.push(arr[index[i]]); show(temp); } } perm(["e1", "e2", "e3", "e4"]); </script> </body> </html>
The above is the detailed content of Detailed explanation of several recursive total permutation algorithms in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.