Unformatted text preview: on Sort Insertion Sort
Applied Programming
Applied 34 6 1 7 8 9 5 1 2 Applied Programming 3 4 6 7 8 9 5 1 2
Insertion Sort Insertion Sort
Applied Programming 34 6 7 8 9 5 1 Applied Programming 234 6 7 8 9 5 1 2
Insertion Sort Insertion Sort
Applied Programming Applied Programming 234 6 7 8 1 9 234 5 6 7 8 9 5 Insertion Sort Insertion Sort
Applied Programming
Applied 234 5 6 7 1 8 9 1 Applied Programming 234 5 6 7 8 9
1 Insertion Sort Insertion Sort
Applied Programming 234 5 6 7 8 9 Applied Programming 1 234 5 6 7 8 9 1 Insertion Sort Insertion Sort
Applied Programming Applied Programming Guarantees So, how good is this algorithm?
On average, the value being inserted will go in the
On
middle. So we need only about half of the
comparisons of Selection Sort.
comparisons W e have to be sure that, if we follow the procedure
We
described, by the completion of the process the
data will be sorted.
data
Just because some test runs produce the correct
Just
result is not a proof of correctness.
result That is, about n(n1)/4 on average.
That
n(n
Now let's look at how we generally compare the
Now
efficiency of algorithms
efficiency
. . . and how we must ensure that the algorithm
and
actually works.
actually
Applied Programming
Applied The advantage of standard algorithms – triedandThe
tested methods – iis that we can be assured that
s
tested
the procedure will work.
the Efficiency We said Selection Sort needed
n(n1)/2 comparisons (always). What algorithm is best for a particular purpose?
How does runtime change as the size of the
How
dataset increases?
dataset
For sorting, the number of comparisons of the
For
data is a good measure.
data Applied Programming W e say that such an expression is of order n2
We
order
It behaves like a constant times n2 when n is
when
large (the n2 term overpowers the n term so the
large
term
latter is ignored).
latter
* the expression is bounded above by a
the
bounded
by
constant times n2 for n > some value.
constant Start with Selection Sort.
Applied Programming W...
View
Full Document
 Spring '14
 Sort, Bubble Sort, Insertion Sort, Selection Sort, Comparison sort

Click to edit the document details