"In order to understand recursion, one must first understand recursion; after that, it's easy." -Andrew Koenig [3]The reader's knowledge of arrays, binary trees, character-string comparison and exchange operations, and our zip-code-sorting operation's variant of bubble-sort algorithm-abbreviated as "BS" henceforth-is assumed.
"Wait a minute...," cautions the reader. "Why don't we simplify those sub-tasks into yet smaller sub-tasks? Doing so would allow for faster completion each sub-task and thus increase the efficiency of the overall task."Why, of course! Each sub-task can indeed be simplified once more, each of its resulting sub-tasks can be simplified yet again, and this recursive process can continue indefinitely. However, businesses, and customers alike, require that an algorithm compute its result within a finite period of time. In other words, algorithms that compute results in a shorter amount of time are preferred. Therefore, a recursive algorithm must only simplify sub-tasks until a termination-condition is reached; that is, until the task at hand is so simple that its result can be computed without further simplification. Now, suppose the algorithm has finished computing the results of all maximally-simplified sub-tasks. The independent result of any one of these sub-tasks is an insufficient substitute for the original task's result. Therefore, a method of combination is necessary to transform these independent results into a collective-result sufficient for original task. Likewise, a method of division is necessary to help the algorithm decide how each task should be simplified. These methods are detailed, with respect to our zip-code-sorting operation, in Section .
|
|
| P(x)= |
1
|
| \mathbbR∩ |
\mathbbZ
|
| Algorithm | Best-Case | Worst-Case | Average-Case |
| Bubble-Sort | Θ(n) | Θ(n2) | Θ(n2) |
| Heap-Sort | Θ(n*log(n)) | Θ(n*log(n)) | Θ(n*log(n)) |