Home  >  Article  >  Web Front-end  >  Six algorithms for full permutation in JavaScript

Six algorithms for full permutation in JavaScript

高洛峰
高洛峰Original
2016-11-26 10:02:481636browse

Total permutation is an algorithm with a time complexity of: O(n!). I was giving a lecture to students two days ago. I accidentally thought of this problem. I came back and summarized it. It can be solved by 7 algorithms, among which dynamic loops are similar to backtracking algorithms. , it is relatively cumbersome to implement, so 6 types are summarized for the convenience of readers. All algorithms are written in JavaScript and can be run directly.
Algorithm 1: Exchange (recursion)
1.
2.
3. " content="text/html; charset=utf-8" />
4. Full Permutation(Recursive Swap) - Mengliao Software
5.
6. < body>
7.

Full Permutation(Recursive Swap)

8. Mengliao Software Studio - Bosun Network Co., Ltd.

9. 2011.05.2410.
42.
43.
Algorithm 2: Link (recursive)
1. < html xmlns="http://www.php.cn">
2.
3.  
4. " Full Permutation(Recursive Link) - Mengliao Software
5.
6.
7.

Full Permutation(Recursive Link)

8. Mengliao Software Studio - Bosun Network Co., Ltd.

9. 2012.03.29


10.  
33.  
34. 
算法三:回溯(递归)
1.  
2.  
3.      
4.     Full Permutation(Recursive Backtrack) - Mengliao Software 
5.  
6.  
7. 

Full Permutation(Recursive Backtrack)
 
8. Mengliao Software Studio - Bosun Network Co., Ltd.
 
9. 2012.03.29

 
10. 
59.
60.
Algorithm 4: Backtracking (non-recursive)
1. 2.
3.
4. Full Permutation(Non -recursive Backtrack) – Mengliao Software
5.
6.
7.
9. Mengliao Software Studio - Bosun Network Co., Ltd.

10. 2012.03.29


11.  
54.  
55. 
算法五:排序(非递归)
1.  
2.  
3.      
4.     Full Permutation(Non-recursive Sort) - Mengliao Software 
5.  
6.  
7. 

 
8. Full Permutation(Non-recursive Sort)
 
9. Mengliao Software Studio - Bosun Network Co., Ltd.
 
10. 2012.03.30

 
11.  
66.  
67. 
算法六:求模(非递归)
1.  
2.  
3.      
4.     Full Permutation(Non-recursive Modulo) - Mengliao Software 
5.  
6.  
7. 

Full Permutation(Non-recursive Modulo)
 
8. Mengliao Software Studio - Bosun Network Co., Ltd.
 
9. 2012.03.29

 
10. 
54. < /body>
55.
Some of the above six algorithms are to arrange positions, such as backtracking, sorting, etc., because this can adapt to various types of elements, rather than requiring that the elements to be arranged must be numbers or letters etc.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn