in the mentioned suroutine i used print statement,,it is printing values like {4,7 4,2 3,4 2,4 7,4 7,4 2,3}....in what logic it is printing these values
Oh. Unless your goal is to experiment with Perl's sort builtin, you
should not print anything (or do anything with side-effects, other
than _perhaps_ cache computed values). Your sorting subroutine is
actually a _comparison_ subroutine, i.e., sort will call it each
time it needs to compare two of the items in the list to see which
should sort earlier. Each item might be compared multiple times
to different other items, but it will _not_ necessarily be compared
to _every_ other item (since that would be terribly inefficient).
If you want to print out the list of sorted values, you should
sort it first, then iterate over the list and print the values.
If you were just curious and experimenting, then like I said I
think it might be a heap sort, but I could be wrong about that.
The easiest way to find out would be to look in the source code
for perl. No, wait, perldoc might say... Ah, yes, it does:
Perl 5.6 and earlier used a quicksort algorithm to implement
sort. That algorithm was not stable, and could go quadratic.
(A stable sort preserves the input order of elements that com-
pare equal. Although quicksort's run time is O(NlogN) when
averaged over all arrays of length N, the time can be O(N**2),
quadratic behavior, for some inputs.) In 5.7, the quicksort
implementation was replaced with a stable mergesort algorithm
whose worst case behavior is O(NlogN). But benchmarks indi-
cated that for some inputs, on some platforms, the original
quicksort was faster. 5.8 has a sort pragma for limited con-
trol of the sort. Its rather blunt control of the underlying
algorithm may not persist into future perls, but the ability to
characterize the input or output in implementation independent
ways quite probably will.
So if you have a recent perl it's probably a mergesort. HTH.HAND
Sanity? Oh, yeah, I've got all kinds of sanity. Why, I've got so much sanity it's driving me crazy. In fact, I've developed whole new kinds of sanity.
| [reply] |