Introduction to Computers and
Programming
Prof. I. K. Lundqvist
Lecture 10
April 8 2004
2
Today
• How to determine Big-O
• Compare data structures and
algorithms
• Sorting algorithms

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
3
How to determine Big-O
• Partition algorithm into known pieces
• Identify relationship between pieces
– Sequential code (+)
– Nested code (*)
• Drop constants
• Only keep the most dominant factors
4
Does Big-O tell the whole story?
• T
x
(n) = T
y
(n) = O(lg n)
setup of algorithm
-- takes 50 time units
read n elements into array
-- 3 units/element
for
i
in
1..n
loop
do operation1 on A[i]
-- takes 10 units
do operation2 on A[i]
-- takes
5 units
do operation3 on A[i]
-- takes 15 units
setup of algorithm
-- takes 200 time units
read n elements into array
-- 3 units/element
for
i
in
1..n
loop
do operation1 on A[i]
-- takes 10 units
do operation2 on A[i]
-- takes
5 units
T
1
(n)=50+3n+(10+5+15)n = 50+33n
T
2
(n)=200+3n+(10+5)n = 200+18n

5
F&B BST
BST
Binary Tree
Sorted Array
Unsorted Array
Sorted L List
Unsorted L List
Insert
Search
Traversal
Data structure
N
N
N
N
N
N
N
6
Searching
•
Linear
(sequential) search
– Checks every element of a list until a match
is found
– Can be used to search an unordered list
•
Binary
search
– Searches a set of
sorted
data for a
particular data
– Considerable faster than a linear search
– Can be implemented using recursion or
iteration

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*