Title: Insertion Sort
1Insertion Sort
2Insertion Sort
- Consider a deck of cards face down on the table.
- Pick one and hold in left hand.
- Then iteratively pick a card from the top of the
deck and insert it at the right place among the
cards in your left hand. - When inserting, go from right to left looking for
an appropriate place to insert the card.
3Pseudocode of insertion sort
- j ? 2
- while j lengthA do
- key ? Aj
- ? Insert Aj into the sorted sequence A1j-1
- i ? j-1
- while igt0 and Aigtkey do
- Ai1 ? Ai
- i ? i-1
- Ai1 ? key
- j ? j1
4Analysis of running time
We can adopt a convention that a constant number
of primitive operations is executed for each line
of our pseudocode. For example line i consists of
ci primitive operations.
5Evaluation of running time for insertion sort
Running time of an algorithm on an input is the
total number of primitive operations executed by
the algorithm on this input until the algorithm
halts the algorithm may not halt then running
time is defined as infinity. For different
inputs, the algorithm may have different running
time randomized algorithms can have different
running times even for the same input.
6Evaluation of running time for insertion sort
Fix an array A of length at least 1, and let n be
the length. Let T(A) be the running time of
insertion sort an array A. Let tj be the number
of times the condition of the while loop in line
5 is evaluated for the j so t2 is the number
of times during the first execution of the body
of the external while loop lines 2 to 9. We
can calculate how many times each line of
pseudocode is executed
7worst-case running time of insertion sort
- Assume array A has size n. We want to develop a
tight upper bound on T(A) - Note that i starts with j-1 and is decreased each
iteration as long as igt0. So tj j. In fact this
value can be achieved when the array is initially
sorted decreasingly ltn,,3,2,1gt, because each key
will trigger shifting by one to the right all
keys to the left of the key. Recall that - When A has size n, T(A) an2bnc for some
positive constants a, b and c. - Let T(n) denote the worst case running time of
insertion sort for an input of size n array of
size n. - From the preceding discussion T(n) an2bnc .
- So the worst-case running time is a quadratic
function. - What is the best case running time?
- linear function when input array is sorted.
8Insertion Sort
Starting near the top of the array in Figure (a),
we extract the 3. Then the above elements
are shifted down until we find the correct place
to insert the 3. This process repeats in Figure
2-1(b) with the next number. Finally, in Figure
(c), we complete the sort by inserting 2 in
the correct place.