Re: Worst-case Performance of Insertion Sort



On Feb 20, 11:58 pm, "eKo1" <berndlos...@xxxxxxxxxxxx> wrote:
Here is the version of insertion sort I'm using:

InsertionSort( list, N )
list the elements to be put into order
N the number of elements in the list

for i = 2 to N do
newElement = list[ i ]
location = i - 1
while (location >= 1) and (list[ location ] > newElement) do
// move the larger elements out of the way
list[ location + 1 ] = list[ location ]
location = location - 1
end while
list[ location + 1 ] = newElement
end for

The while loop will iterate at most i - 1 times, i = 2 to N, and this
happens when newElement is less than all the elements of list[1...(i -
1)]. This can only happen when the list is in decreasing order. I'm
asked to find another class of input that produces the worst-case
behaviour of this algorithm but I just can't think of another one. Any
hints or tips as to what that other input class might look like?

Yes, the worst case for the above code occurs only when the list
is in reverse order.

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.

.



Relevant Pages

  • Re: Uninterned symbols at macroexpansion time.
    ... (loop for somevar in '(some list here) ... so if we give the macro a gensym in ... collect (BACKQUOTE (UNQUOTE X)))) ... simple type (meaning eg symbols, lists, etc) not ...
    (comp.lang.lisp)
  • Re: Learning Lisp in Linux?
    ... To adapt the language to the task at hand? ... unless (member key excluded-keys) ... (loop (cddr property-list) ... correct property lists as results. ...
    (comp.lang.lisp)
  • Re: Optimize a function for speed
    ... Lists in Common Lisp are ... (loop while ... ... (setf mlist (rest mlist)). ... (setf (aref mtable ind 0) ...
    (comp.lang.lisp)
  • Re: Replacing subsequences
    ... >> different substring, of a different length (for quoting parameters to SQL ... fill-pointer that let me append sequences to my sequence at the fill ... Worth the price of avoiding loop, ... Is there anything besides lists and vectors to worry about? ...
    (comp.lang.lisp)
  • Re: loop: finally collect
    ... | Is it possible to avoid such a `finally' clause but still using a loop ... collect element into tmp-collection ... clause to finalize the result and successfully obscures what you seem ... Exactly how long are these lists?! ...
    (comp.lang.lisp)