I want to make simple sorting algorithm.
given the input "abcde", I would like the output below. could you tell me the algorithm for that?
arr[0] = "a"
arr[1] = "ab"
arr[2] = "ac"
arr[3] = "ad"
arr[4] = "ae"
arr[5] = "abc"
arr[6] = "abd"
arr[7] = "abe"
...
arr[n] = "abcde"
arr[n+1] = "b"
arr[n+2] = "bc"
arr[n+3] = "bd"
arr[n+4] = "be"
arr[n+5] = "bcd"
arr[n+5] = "bce"
arr[n+5] = "bde"
...
arr[n+m] = "bcde"
...
...
The algorithm assumes that we have a set containing elements: {0, 1, … , }. Then, we generate the subsets containing elements in the form , starting from the smallest lexicographic order: The algorithm visits all -combinations. It recursively represents multiple nested for loops.
Traditional formula of r-combination (or n choose r) is: C(n, r) = n! / (r! . (n-r)!)
* 3!) = 10 different ways. This generalises to other combinations too and gives us the formula #combinations = n! / ((n - r)! * r!)
I.e. there are 4 objects, so the total number of possible combinations that they can be arranged in is 4! = 4 x 3 x 2 x 1 = 24.
An algorithm for "generating Power Set" from an array is what you are looking for. You can try Google or some other search engine to find the algorithm that best fits your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With