Re: Factorial

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Russ Holsclaw (russ_at_holsclaw.nyet)
Date: 05/24/04


Date: Mon, 24 May 2004 15:17:21 -0600


> Oddly I quite like recursion ( or re-entrant ) routines
> - generally a Static watchdog variable solves development problems

FYI, the term re-entrant is not synonymous with recursive. It's a
commonly-held misconception. The term actually relates to code that does
not modify any memory within it's own code segment, or any other memory
except for addresses contained in registers passed to the routine or
buffers acquired via memory-management calls. These attributes enable a
single memory-resident copy of code to be concurrently used by several
processes/threads/tasks in a multi-tasking operating system.

It is true, however, that the programming techniques that enable
re-entrancy also permit recursive routines to be written as well, because
variables in the separate invocations of the routine, even
self-invocations, are kept isolated from each other.

>
> Those things are Ok if you know what you are dealing with, but not for
> idiotic algorithms like QuickSort ( Flames welcome )

You won't get flames from me here. Many years ago, it dawned on me that
books on algorithms, especially those written as academic textbooks, were
excessively obsessed with the specialized problem of sorting data *in
place*, i.e. sorting in an environment that is so memory-constrained that
there's room for only one copy of the list to be sorted, and little else.
This is rarely the case, and even rarer still that you would be able to
predict this situation at the time a program is being designed.

All in-place sorts are sub-optimal, of course. I've quit using them, except
for the most trivial situations (ones that are trivial enough that a simple
bubble-sort is adequate). Generally, I used a merge sort that shuttles the
data back-and-forth between two arrays. When sorting data structures, such
as VB UDT's, I generally don't sort the original array at all, but only a
integer (or Long-integer) array containing a list of array indexes to the
array containing the data. This procedure is much faster than any other
sort. If the data is too voluminous to be safely held in memory, I use
disk-file sorting techniques, or stash the data in a database table. I
don't mess with silly in-place sorting algorithms at all.

> <quote>
> a form of masturbatory software
> design. Some people, particularly academics who have nothing
> better to do, use recursion because they think it makes them look
> cool.
> </quote>

...and yes, in-place sorting belongs in the category of masturbatory
software.

>
> Too damn right, the first problem is getting the code to work
> - the second (and long term most time consuming problem) is
> maintaining the code.
>
> If it ain't simple then it ain't right
>
> Recursion is not the problem, code clarity /is/.

...and I would submit that about the only time recursive code is clearer is
when it's done in a situation that actually *calls* for recursion, such as
traversing tree-like data structures.



Relevant Pages

  • Re: Question on Fortran implementation of very large array heaps (optimal minimization)
    ... The heap array could become quite large. ... reactions would be impressive enough. ... > For sorting single words, ... > memory manager every time you add a value. ...
    (comp.lang.fortran)
  • Re: In-place algorithm
    ... Nobody has been talking about sorting an array. ... this has been about sorting lists from the ... memory is required. ...
    (comp.programming)
  • Re: random writing access to a file in Python
    ... the size of available memory for the sorting operation (making it ... possible to work on larger chunks of data in memory) has less impact ... Transfer speed and parallelism matters most, if the cpu can keep up. ... The number of levels of recursion also depends on the "merge order", ...
    (comp.lang.python)
  • Re: copymemory basic question
    ... >> but neither of them exhibit any memory leak: ... > We are not talking about sorting using CopyMemorybut using an index ... > in an additional VB array... ... > Array Index of the Strings is definitely the best solution - and almost ...
    (microsoft.public.vb.winapi)
  • Re: Recursive Divide and Conquer
    ... splitting 2 arrays of intin half until a single element exists in ... It requires Omemory for the array ... and O) memory for the recursion. ...
    (comp.programming)