CSE1301 Computer Programming Lecture 30 List Processing
Document Sample


CSE1301
Computer Programming:
Lecture 30
List Processing: Search
Topics
• An Array as a List
• Searching
– Linear Search
– Binary Search (sorted list)
• Algorithmic Efficiency
Arrays as Lists
• An array stores a number of the same type
of element.
• Can be thought of as a list:
13
int a[8]
5
19
10 13 5 19 10 7 27 17 1
7
27
17
1
Linear Search
• Task: determine if an element is present in
an array.
• Method:
– Start at one end,
– look at each element until element found.
• Also called sequential search.
Linear Search:
Algorithm and Code
See Lecture Notes Algorithm 40.
How would you modify the program so that it
returns the position? How would you
indicated “not found”?
What does efficiency mean?
• Algorithm: a set of instructions describing
how to do a task.
• Program: an implementation of an algorithm.
• Complexity theory: what computer resources
does an algorithm use?
– Note: nothing to do with how complex or
complicated the code is.
• Resources: How efficient is an algorithm?
Types of computer resources
• Time: elapsed period from start to finish of
execution of an algorithm.
• Space (memory):
– amount of storage required by an algorithm.
– need arises when partial results calculated
during algorithm are required later.
• Hardware: amount of physical mechanism
required for execution of algorithm.
• This lecture will focus mainly on time.
How to measure efficiency?
• Use your watch? Use computer clock?
• Not a good idea, because:
– What if you run program on different computers?
– Program may also wait for I/O or other
resources.
– While running a program, computer performs
many other computations.
– Depends on programming/coding skill.
Abstract notion of efficiency
• We are interested in the number of steps
executed by the algorithm.
– step execution of an instruction.
• The running time of an algorithm is
proportional to the number of steps it takes
to execute the algorithm.
• Running time: given as a function of the
size of the input data: “Big-O Notation.”
Notes on the Big-O Notation
• Simplify and choose the highest term.
• Examples:
2 + 3N + 100N + 3N2 + 100000
= 3N2 + 103N + 100002 O(N2)
40000000N + N3 O(N3)
25 O(1)
Notes on the Big-O Notation
• Constant • O(1)
• Logarithmic • O(log(n))
• Linear • O(n)
• n log(n) • O(n log(n))
• Quadratic • O(n2)
• Cubic • O(n3)
• Exponential • O(2n)
Linear Search Efficiency
• What is the size of the input data?
– The size of the array being search, say N
• What is the running time of this algorithm?
– Each time through loop do 2 comparisons and 1
increment.
Linear Search Efficiency
• Best case?
– Wanted item is at start of list
– 1 assignment + 2 comparisons
• Worst case?
– Wanted item is at end of list
– 1 + N x (2 + 1) + 1= 2+ 3N O(N)
• Average case?
– Wanted items is in the middle of the list
– 1 + (N/2) x (2 + 1) = 1 + 3N/2 O(N)
Binary Search
• Can we do any better than linear search?
• Example: how do you find a word in the
dictionary, or a number in the phone
directory?
• Assume that the array is sorted.
Binary Search
• Use bisection.
If value == middle element
value is found
else if value < middle element
search left-half of list with the same method
else
search right-half of list with the same method
Case 1: val == a[mid]
Example: val = 10
N = 9, low = 0, high = 8
mid = (0 + 8) / 2 = 4
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
low mid high
Case 2: val > a[mid]
Example: val = 19
N = 9, low = 0, high = 8
mid = (0 + 8) / 2 = 4
new low = mid + 1 = 5
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
low mid New high
low
Case 3: val < a[mid]
Example: val = 7
N = 9, low = 0, high = 8
mid = (0 + 8) / 2 = 4
new high = mid - 1 = 3
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
low New mid high
high
(continued...)
Example:
val = 7
low = 0, new high = 3
new mid = (0 + 3) / 2 = 1
new low = mid + 1 = 2
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
low mid New high
low
Example:
val = 7
new low = 2, high = 3
new mid = (2 + 3) / 2 = 2
a: 1 5 7 9 10 13 17 19 27
0 1 2 3 4 5 6 7 8
low high
mid
Binary Search:
Algorithm and Code
• See Algorithm 41 in Lecture notes
• What happens if value is not in the list?
• How would you modify the code so that it
returns the position of the item (ie
findPostion rather than isPresent)?
• Note: Binary Search is usually implemented
recursively (CSE1303).
Binary Search Efficiency
• What is the running time for this algorithm?
• Each time through loop do
– 2 comparisons,
– 3 arithmetic operations,
– 2 assignments.
Binary Search Efficiency
• Best case?
– item is in the middle
– 6 operations, O(1)
• Worst case?
– Not found (e.g. look for number 8)
– Theoretical complexity in terms of N?
– Find x so that N 2x x O(log N)
• Average case: O(log N)
Exercise
struct studentRec
{
int IDNumber;
char name[NAMELEN];
float mark;
};
typedef struct studentRec Student;
Student class[MAX_STUDENTS];
How would you implement linear search and binary
search over an array of structs?
The array must be sorted by ID, name, or mark,
depending on the search key.
Notes on Searching
• Linear Search can be done on any (i.e.
sorted or unsorted) list, but it's inefficient.
• Binary Search
– requires list to be sorted
– is more efficient
• Sorting Algorithm: Lecture 32.
Group Project (cont’d)
• Sample Bingo Program
– Randomization (also: Dietel & Dietel 5.9)
– Avoiding duplicates
– Validating numerical input
– Use of #include and #define
• Tip: No need to load the whole dictionary
file into memory
• Need for small test programs which pass test
data into a function, and show the results.
• Group coordination: Contact details!
Related docs
Get documents about "