Re: Worst-case Performance of Insertion Sort



Perhaps the author means the worst case number of comparisons
(comparisons of array elements); these comparisons occur only in
the loop test. The apparent semantics of the test "(location >= 1
AND list[location] > newElement)" means that the second half will
not be tested when the first half is false (i.e. when location < 1).
Consequently, the worst case number of comparisons occurs precisely
when each successive element is small enough that it will get
compared to the first element in the list **regardless** of the result
of that comparison. For example, the following two lists will elicit
the maximum number of comparisons for the above code.

1 9 8 7 6 5 4 3 2
9 8 7 6 5 4 3 2 1

But there will be fewer data movements for the first list because
the inner loop will execute one fewer time per iteration of the outer
loop.

You may be right. However, in the worst and average case analysis
given in the text, the author is considering the condition of the
while loop as one comparison which is probably why I got confused.

.



Relevant Pages

  • Re: New style DO syntax?
    ... Joe Krahn wrote: ... I think that the DO-loop is the only "block construct" that does not contain the 'expression/attribute' part in parenthesis. ... Using parenthesis for delimiters should also allow for using array elements as do iterators. ... Capture the value of I at loop start and use that for ...
    (comp.lang.fortran)
  • Re: Word counting
    ... length so increment our word length to search for by 1. ... Loop through our array elements and hopefully we'll have the words ... This is an example input file ...
    (comp.lang.ada)
  • Re: array help needed
    ... You show 'add.item loop' in your latest post... ... exactly what are you planning to add your array elements to? ... my cell values store spreadsheet names and i wish to store them all in the ... "shg" wrote: ...
    (microsoft.public.excel.programming)
  • Re: converting .dat file to array
    ... array elements corresponding to indices greater than the number of data ... According to the standard, ... The actual behavior is compiler dependent. ... You need to so something with a loop, ...
    (comp.lang.fortran)
  • Re: Changing the name of a variable using code
    ... (The function would have to loop through the entire array looking for X, ... This would allow you to access the array elements much like the elements of ... It may be a coin toss depending on what you are doing. ... column is a name of a variable and the second column is the value of ...
    (microsoft.public.excel.programming)