Lect1 Introduction InsertionSort
Lect1 Introduction InsertionSort
Course Prerequisites:
Good working knowledge of data structures such as linked lists, trees, and
dynamically allocated structures is required.
CECS 302 Data Structures
CECS 310 Discrete Structures
Text Book:
“Introduction to Algorithms”, 3rd Edition.
Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest, Clifford Stein. (2009). MIT Press.
Record
Key Other data
i j n
sorted
Key
input array A 5 2 4 6 1 3
sorted unsorted
j=6 j-1 j
polynomial degree
# of elements in a matrix
25
How do we compare algorithms?
We need to define objective measures.
1. Compare execution times?
Not good: times are specific to a particular
computer !!
2. Count the number of statements executed?
Not good: number of statements vary with
the programming language as well as the
style of the individual programmer.
26
Ideal Solution
Express running time as a function of the
input size n (i.e., f(n)).
Compare different functions corresponding to
running times.
Such an analysis is independent of machine
time, programming style, etc.
27
Machine-independent time
What is insertion sort worst case time?
Depends on the speed of the computer
Relative speed (on the same machine)
Absolute speed (on different machines)
i = j-1 c3 n-1
A[i+1] = A[i] c5
i = i-1 c6
A[i+1] = key c7 n-1
c3 n-1
c4
c5
c6
c7 n-1
A B
Linear
B C
A
Quadratic
H. Frigui CECS 419/619: Lecture 1 33