This is a two-part question from a newbie.

First, I need an encoding for simple text (without the lowercase/caps distinction), and I need it to be more space-efficient than ASCII. So I have thought of creating my own 5-bit code, holding a range of 32 characters (the alphabet plus some punctuation marks).
As far as I understand, all modern computing 'thinks' in bytes, so I cannot actually define my own 5-bit encoding without actually resorting to a 8-bit encoding.

What I am thinking of doing is:
I define my own 5-bit code and I save the text in 3-character blocks, each block saved as 2 bytes. Every block will occupy total of 15 bits, which will be stored within two bytes (holding 16 bits). I might use the extra bit for parity check, even if I don't actually need it.
Does this approach make sense? Or is there any better approach?
Alternatively, I might define a 6-bit encoding, and save the text into blocks of 4 characters each, with each block being saved in 3 bytes.

The second part of the question is: assuming that the text will then be compressed (via a standard lossless algorithm for texts, such as zip for example), is it worth the trouble of creating my own encoding (as explained above)? Or will the compression algorithm take care of the space inefficiency of the 8-bit encoding, making the compressed as efficient as a compressed file which originally was encoded using a 5-bit or 6-bit encoding? If so, I would have no advantage of using a 5/6 bit encoding for pre-compression text, so I would simply skip altogether this step.
I need to know from experienced programmers, what is the case?

Thanks everyone
['https://Stackoverflow.com/questions/15588510', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2134205/']
This is precisely what they're doing. It's called recursion, and it's, well, hard to wrap your head around at first. Imagine you have strings:

A B C D

Effectively, what you do, is choose each string in turn and then find all permutations of the remaining strings, and prepend your chosen string.

```
Choose A, get all permutations of {B,C,D} and append A. 
Choose B, get all permutations of {A,C,D} and append B. 
Choose C, get all permutations of {A,B,D} and append C. 
Choose D, get all permutations of {A,B,C} and append D. 

```

Now, we have subproblems that look very similar, but are smaller. That's the heart of recursion. Take a problem, and find a way to turn it into a smaller version of the problem. Now, keep turning it into a smaller problem until it's trivial. Now we have to figure out how to generate permutations of 3 strings. 

```
Permute(A B C) 
Choose A, get all permutations of {B,C} and append A.
Choose B, get all permutations of {A,C} and append B.
Choose C, get all permutations of {A,B} and append C.

```

Same structure, but smaller problems. So take it a step further. How do we do permute(A B)

```
Permute(A B) 
Choose A, get all permutations of {B} and append A.
Choose B, get all permutations of {A} and append B.

```

So now we just have to permute one string. That's trivial.

```
Permute(A)
A

```

So now we have a way to Permute a list of strings of size 1, and we've defined how to permute a list of size N by Permuting a list of size N-1. So, we can permute any list of size >= 1, by calling ourselves with a slightly smaller version of the problem. That's the beauty of recursion. You just define how to solve the smallest problem, and how to use that solution to build a bigger solution, and it builds upon itself.
This is a recursive algorithm, it essentially takes each element in the array as the first one then adds to to it the output of calling itself without that element.

Take 3 elements A, B and C and walk through the logic, lets call our function `perm`.

so, we want

```
perm({A, B, C})

```

this equals

```
A + perm({B, C})
B + perm({A, C})
C + perm({A, B})

```

expand again

```
A + B + perm({C})
A + C + perm({B})
B + A + perm({C})
B + C + perm({A})
C + A + perm({B})
C + B + perm({A})

```

as `perm({X}) = X` we end up with

```
A + B + C
A + C + B
B + A + C
B + C + A
C + A + B
C + B + A

```