Re: "Sorting" assignment
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Fri, 8 Feb 2008 11:50:50 -0000
"Bartc" <bc@xxxxxxxxxx> wrote in message
spinoza1111 wrote:j however is uninitalised, and should in fact be i + 1.On Feb 7, 7:17 pm, "Clive D. W. Feather" <cl...@on-the-
train.demon.co.uk> wrote:
bool bubble;
do
{
bubble = false;
for (int i = 0; i < arraySize - 1; i++)
if (array [i] > array [j])
{
exchange (i, j);
bubble = true;
}}
while (bubble);
(2) The last value for i is the arraySize minus one but this is wrong
insofar as I can determine your intent from bug number 3. Because of
zero-origin indexing that should be arraySize minus two.
(3) You failed to compute j at all.
No, the last value for i is arraySize-2 in the above loop.
The point about the calculation of arraySize -1 being in the test condition is maybe relevant, but only for a very bad compiler. Edward some years ago posted an example in which a call to strlen() was made in the test condition. A huge amount was made of this, which is why he is picking up on a similar inefficiency, but the situations are different. In the latter case it is much harder for the compiler to pick up on the fact that the string is unmodified, and the cost of the operation is greater, making the loop itself O(N^2).
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
.
- Follow-Ups:
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- References:
- "Sorting" assignment
- From: Ivica
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Clive D. W. Feather
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Clive D. W. Feather
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Bartc
- "Sorting" assignment
- Prev by Date: Re: printf() function
- Next by Date: Re: printf() function
- Previous by thread: Re: "Sorting" assignment
- Next by thread: Re: "Sorting" assignment
- Index(es):