We will use the divide and conquer method to implement a full sorting algorithm. First, let's take a look at the effect of the algorithm: ['A', 'B', 'C']. permutation # => ["a", "B", "c"],
# ["A", "c", "B"],
# ["B", "a", "c"],
# ["B", "c", "a"],
# ["C", "a", "B"],
# ["C", "B", "a"]
Algorithm Description
There are three steps to solve the issue:
-Decomposition: divides the problem into several subproblems.
-Solution: recursively solve each subproblem.
-Merge: Merge the solutions of each subproblem into the solution of the entire problem.
Now we need to require the full arrangement of array A with n elements. For example, for the array A = [a, B, c] with A size of 3 (for convenience, I omit all the quotation marks. In fact, it should be a = ['A ', 'B', 'C']. ), And its full row column is:
[[A, B, c],
[A, c, B],
[B, a, c],
[B, c, a],
[C, a, B],
[C, B, a]
This is a value of n! * A two-dimensional array of n.
The process of using the grouping algorithm to solve the full arrangement is as follows:
-Decomposition: divides the array into A subarray A [1 .. k-1] and an element A [k]. (1 ≤ k ≤ n)
-Solution: recursively solve the full arrangement of each sub-array A [1 .. k-1] until the sub-array A [1 .. k-1] Is null and end recursion.
-Merge: Merge the result of the previous step --- A [1 .. the full arrangement of k-1] (A two-dimensional array) is combined with element A [k] to obtain A [1 .. k. For example:
[[] Merge with a to obtain [[a]
[[A] merged with B to obtain [[a, B], [B, a]
[[A, B], [B, a] and c are combined to obtain [[a, B, c], [a, c, B], [c,, b], [B, c, a], [c, a, B], [c, B, a]
The figure below is more intuitive.
1. Decomposition Process [a, B, c]
/\
[A, B] c
/\
[A] B
/\
[]
2. merge process []
\/
[[A] B
\/
[[A, B], [B, a] c
\/
[[A, B, c],
[A, c, B],
[C, a, B],
[B, a, c],
[B, c, a],
[C, B, a]
Source code
The following is the ruby source code (File Name "permutation. rb") class Array
# Returns permutation (a new two-dimentional array) of self.
# Example
#-[]. Permutation # => [[]
#-['A']. permutation # => ['a']
#-['A', 'B']. permutation # => [['A', 'B'], ['B', 'a']
#-['A', 'B', 'C']. permutation # => [['A', 'B', 'C'],
# ['A', 'C', 'B'],
# ['B', 'A', 'C'],
# ['B', 'C', 'a'],
# ['C', 'A', 'B'],
# ['C', 'B', 'a']
Def permutation ()
Permutation_ I (self. length-1). sort!
End
Private
Def permutation_ I (I)
If I> 0
Return Array. merge_permutation (permutation_ I (I-1), self [I])
Else
Return Array. merge_permutation ([[], self [I])
End
End
# Example
#-Array. merge_permutation ([[], 'A') # => [['a']
#-Array. merge_permutation ([['a'], 'B') # => [['B', 'a'], ['A', 'B']
#-Array. merge_permutation ([['B', 'a'], ['A', 'B'], 'C') # => [['C ', 'B', 'a'],
# ['B', 'C', 'a'],
# ['B', 'A', 'C'],
# ['C', 'B', 'a'],
# ['A', 'C', 'B'],
# ['A', 'B', 'C']
Def Array. merge_permutation (src, item)
Result = Array. new
Src. each do | each_array |
Result + = Array. item_permutation (each_array, item)
End
Return result
End
# Returns a new two-dimentional array. Inserts "item" in each pertential place in "src ".
# Example
#-Array. item_permutation ([], 'A') # => [['a']
#-Array. item_permutation (['a'], 'B') # => [['B', 'a'], ['A', 'B']
#-Array. item_permutation (['A', 'B'], 'C') # => [['C', 'A', 'B'], ['A ', 'C', 'B'], 'A', 'B', 'C']
Def Array. item_permutation (src, item)
Result = Array. new
(0 .. src. length). each do | I |
New_array = src. collect
New_array.insert (I, item)
Result <new_array
End
Return result
End
End
Require 'pp _ extension'
Pp (['A', 'B', 'C']. permutation)
Source code is used to output the "pp_extension" of a two-dimensional array. Let's look at this article to let ruby output a two-dimensional array in a matrix style.
The source code is as follows (File Name "pp_extension.rb") require 'pp'
# Outputs two-dimension array like matrix
Class Array
Def pretty_print (q)
Q. group (1, '[', ']') {
Q. seplist (self) {| v |
Q. current_group.break if v. is_a? (Array )&&! Q. current_group.first? # Added by me
Q. pp v
}
}
End
End
Running time
Algorithm runtime T (n) = T (n-1) + Runtime (n2) = Runtime (n3)
Note: This text is not copied from a textbook, but the result of my closed doors and car creation. It is very likely that there are some mistakes. Please kindly advise.