P
Phil Carmody
Keith Thompson said:Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
You seem to be overlooking the 'size of the input' aspect.
Phil
Keith Thompson said:Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
Phil Carmody said:You seem to be overlooking the 'size of the input' aspect.
[email protected] (Richard Harter) said:[...]
Many years ago there was a crackpot posting here who claimed that
sorting was O(n). It turns out that if you think about it
properly, it is.
Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
True, but no cigar. A more precise statement is that the time
required for sorting is O(n) - omega(n) if you prefer.
I don't see how your statement is more precise than mine; it's just
a different statement. (My statement is admittedly silly.)
My apologies, the relevant measure is big theta. That is, there
are algorithms such that the worst case cost of sorting is
THETA(n), i.e., if C(n) is the worst case sorting time cost for
data sets of size n, there are constants c1, c2, and n0 such that
c1*n < C(n) <c2*n for n>n0.
Will that do?
[email protected] (Richard Harter) said:On Sat, 03 Oct 2009 10:55:15 -0700, Keith Thompson
(e-mail address removed) (Richard Harter) writes:
[...]
Many years ago there was a crackpot posting here who claimed that
sorting was O(n). It turns out that if you think about it
properly, it is.
Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
True, but no cigar. A more precise statement is that the time
required for sorting is O(n) - omega(n) if you prefer.
I don't see how your statement is more precise than mine; it's just
a different statement. (My statement is admittedly silly.)
My apologies, the relevant measure is big theta. That is, there
are algorithms such that the worst case cost of sorting is
THETA(n), i.e., if C(n) is the worst case sorting time cost for
data sets of size n, there are constants c1, c2, and n0 such that
c1*n < C(n) <c2*n for n>n0.
Will that do?
Not for me. Lots of things are missing: the model of computation; the
unit of measure used for "cost"; the representation of the problem
and, by implication, of the solution; what n denotes...[*] Lots of
apparently surprising remarks can be made about computations when
these sorts of things are omitted.
Well no, you don't need all of those things; you only need one of
them and you've actually been given the one you need, albeit with
no attention called to it. Think of this as a challenge: there
is a quite natural interpretation of the statement, sorting is
O(n).
The challenge is to realize what that interpretation is.
There is an important principle involved.
It's a way of dissing O(1) claims.
[*] Typing this was like the Monty Python sketch. I started by
stating that two things were missing, then I added a third... and then
a fourth. I've opted for "lots" and a trailing ellipsis because I bet
I've forgotten some others.
But you see only one of all your things are relevant, just as
they aren't relevant to the claim that sorting is O(n*log n);
Tim said:[...]
Heap sort isn't easy to understand, isn't nearly so easy
to program, is easy to get wrong, and /always/ is doing
non-local operations; it also isn't stable. Heap sort
is a great sorting algorithm.![]()
Merge sort is usually dismissed because of needing extra
space. Yes it's possible to do a stable merge sort on an
array and use only O(1) extra space, but programming that is
horrendous.
After all, we need to establish whether sorting actually is
Theta(n) where n is the number of bits in the data set. The
lower bound is clear enough - whatever the algorithm an oracle
can force it to examine every bit. The upper bound is a bit
murkier. The simple argument runs like this:
Let m be the number of elements and n be the total data set size.
Comparison sorts require O(m * log m) operations. However if
there are m distinct elements then the average size of an element
has to be >= log m.
Ergo n >= m * log m. Ergo O(n) is an upper bound.
On Sun, 04 Oct 2009 13:52:48 +0100, Ben Bacarisse
[snip a lot of stuff - look at the previous postings if you want
background]
That's another matter altogether. Sorting is a problem not an
algorithm.
None-the-less it makes sense to speak of order functions for
problems. Given a problem, there will be a wide variety of
algorithms that can be used to solve it. The intrinsic cost of
solving that problem is the cost of the best algorithms. Thus it
makes sense to say that the traveling salesman problem is
exponential. There is an implicit assumption here that the
algorithms run on a Turing machine equivalent.
Personally, I'd never say that sorting is O(f) for any f
since I don't know of any results of that kind for this class of
problem (I am sure there are some, I just don't know any). Whilst I
agree one can allow some of the things I listed to be unstated for an
/algorithm/[1], I don't think any can left out when discussing a
/problem/.
But they can - it's just a matter of abstraction.BTW, I will be mildly peeved if the big assumption you think I am
making is to assume that all sorting algorithms are comparison sorts.
Your posting history suggest to me that you might have something more
interesting in mind, but I honestly have no idea what is encompassed
by your notion of "natural".
I'm wasn't but prepare to be disappointed - I expect your
reaction will be "Is that all?". You asked, quite properly, what
does n denote. And the answer is, and I quote, "data sets of
size n". But what how do you measure size? The natural
measurement of the size of a data set is the amount of storage it
occupies. For specialized data sets such as arrays of elements
it is convenient to use the number of elements as a measure of
size, but it the actual number of bits that is primary. (Don't
argue with me on this point; I'm right by definition.)
Eric Sosman said:Tim said:[...]
Heap sort isn't easy to understand, isn't nearly so easy
to program, is easy to get wrong, and /always/ is doing
non-local operations; it also isn't stable. Heap sort
is a great sorting algorithm.![]()
Something you forgot to mention is that Heapsort is
O(N*logN) even in the worst case. Heapsort's worst case
isn't "degenerate," unlike that of Quicksort, say.
On the other hand, merge sort is flat-out beautiful for
linked lists, where the O(N) space for the links is "free"
because it's already there.
Keith Thompson said:(e-mail address removed) (Richard Harter) writes:
[...]Many years ago there was a crackpot posting here who claimed that
sorting was O(n). It turns out that if you think about it
properly, it is.Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
You seem to be overlooking the 'size of the input' aspect.
user923005 said:Keith Thompson said:(e-mail address removed) (Richard Harter) writes:
[...]
Many years ago there was a crackpot posting here who claimed that
sorting was O(n). Â It turns out that if you think about it
properly, it is.Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
You seem to be overlooking the 'size of the input' aspect.
FTSI:
;-)
P.S. (for the seriously smiley impaired):
Calls to qsort() will undoubtably be O(log(N)) for random data, unless
qsort() has been converted from recursion to iteration.
user923005 said:(e-mail address removed) (Richard Harter) writes:
[...]
Many years ago there was a crackpot posting here who claimed that
sorting was O(n). It turns out that if you think about it
properly, it is.
Sorting is O(1) if, rather than counting comparisons or swaps,
you count calls to qsort().
You seem to be overlooking the 'size of the input' aspect.FTSI:
;-)
Googling "FTSI" doesn't give me anything useful. I'm probably missing
an obvious joke.
user923005 said:(F)or (T)he (S)miley (I)mpaired
Similar to:
FTSSI (for the seriously smiley impaired)
Eric Sosman said:Tim Rentsch wrote:
On the other hand, merge sort is flat-out beautiful for
linked lists, where the O(N) space for the links is "free"
because it's already there.
pete said:I don't know what you mean by
"it needs at least one extra head node per level of recursion".
For the links, yes; but even for linked lists, it needs at least one
extra head node per level of recursion, unless I've been missing a
trick. This is not a lot, though, and it's still by far the most elegant
way I know of sorting a linked list.
Eric Sosman said:He means that a list merge sort operates by merging short
ordered sublists into longer sublists, and then merging those
into still longer sublists, and so on until the final merge
puts the entire original list into order. Keeping track of
all those sublists (whether by recursion or by other means)
needs O(log N) additional storage.
But since O(N) + O(log N) = O(N), this makes no difference
at all asymptotically. [snip practical comments.]
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.