PPT

advertisement
General Computer Science
for Engineers
CISC 106
Lecture 07
James Atlas
Computer and Information Sciences
06/29/2009
Lecture Overview
Linear Search (recursive)
 Binary Search
 Midterm 1 Review

◦
◦
◦
◦
◦
◦
◦
Unix commands
if statements
arrays (or matrices)
loops
scripts
functions
recursion
Linear search
Searching, extremely important in
computer science
If we have a list of unsorted numbers,
how could we search them?
Iterative strategy

Given [5 4 2 10 6 7] find which position 6
occupies

Alternatively, does the array contain the
number 6?
Recursive strategy
Binary search
Now, what if the array is sorted, can we
search it faster?
Group exercise:
Speed up our search process if we
know the array is already sorted
(smallest to greatest)
Hint: Try splitting the array in half
Binary Search
Find N in list
 Pick a number X halfway between the
start and end of the list
 If X == N we are done
else if X < N search top half of list
else search bottom half of list

Let’s do this in Matlab

Recursive solution
Binary Search
Flowchart diagram of the
algorithm
Note the two stopping
conditions (Exits)
Note the one loop
(Note: flowchart is equivalent to
pseudocode)
How much faster is this than linear
search?
If linear search takes roughly n steps for
n things,
Then binary search takes roughly
log2(n) steps for n things. This is
because we divide the n things by 2
each time.
Midterm and Review

Midterm review online
◦ http://www.udel.edu/CIS/106/jatlas/09Su/exams/09Su/mi
dterm1review.pdf

Midterm 1
◦ July 1 (Class Time : Wednesday!)
Important Notes on Exam
Write pseudo-code from memory
 Study labs
 Study Midterm review
 Bloom’s Taxonomy

Bloom’s Taxonomy
Unix Commands

When you log into a UNIX terminal
◦ You are in your home directory.
◦ To see the files in your directory.
 ls
◦ To make an new folder/directory.
 mkdir exampledir
◦ To change directories.
 cd exampledir
◦ To go back one directory.
 cd ..
◦ To go back to your home directory.
 cd
Basic if statements

IF statements allow program to make
choices whether a condition is met or not
if (expression1)
statements1;
end
if (expression2)
statements2;
end
IF/Elseif Statements
if (expression1)
statements1;
elseif (expression2)
statements2;
else
statements3;
end
Major Relational Operators
◦
◦
◦
◦
◦
◦
A<B
A>B
A <= B
A >= B
A == B
A ~= B
A is less than B
A is greater than B
A is less than or equal to B
A is greater than or equal to B
A is equal to B
A not equal B
If statements
print “blue” if N <= 5
 print “red” if N > 5 and N <= 10
 print “green” if N > 10

If statements (cont’d)
if (N <= 5)
fprintf('blue\n‘);
end
if (N > 5 & N <= 10)
fprintf('red\n‘);
end
if (N > 10)
fprintf('green\n‘);
end
Arrays (aka matrices)
All variables in matlab are arrays
 An array of one element is called a scalar
 A one dimension array is called a vector

x=3.14;
 scalar
a = [1,2,3,4,5];
 vector
Arrays (aka matrices)
x
= 1:0.5:5
Now x is an array of numbers;
x = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
Arrays (aka matrices)

A = [1, 2; 3, 4; 5, 6]
Creates a 3x2 array, 3 rows, 2 columns.
 semicolon creates a new row.

A= 1 2
3 4
5 6
For Loops


Used when you know how many times code is to be
executed.
Syntax
for <variable> = <start>:<increment>:<end>



Variable is initially the start value
At end of iteration variable changes by increment
If value is not greater than end the loop runs again.
Example Problem

I want to find the average # of widgets sold in 4 days
Day

1
2
# of widgets
sold
15
22
3
4
20
18
Widget(1) = 15
 Widget(2) = 22
 Widget(3) = 20
 Widget(4) = 18

Avg = (Widget(1) + Widget(2) + Widget(3) + Widget(4)) / 4
◦ This is easy for a small number of days.
◦ What if we had a 1000 days?
◦ We can use a for loop!
Example Problem

total = 0;
for i = 1:1:1000
total = total+widget (i);
end
avg = total / 1000;
loop starts at 1
loop increments by 1
loop ends at 1000
A Loop Analogy (for)
The runner executes a loop.
 If they know the distance they want to run
 For loop

for lapCount = start : 1 : end
runLap()
end
A Loop Analogy (while)
The runner executes a loop.
 If they don’t know the distance they want
to run (run until tired)
 While loop

tired = false;
while(~tired)
tired = runLap()
end
Scripts files
Store commands in
 Variables are global, available after
you call script file

Scripts files
sumIt=0;
for current=1:finish
if (mod(current,2)==1)
sumIt=sumIt+current;
end
end
Functions

Special type of m-file
◦ Function name same as file name
Contains a function name, arguments,
output, and “implementation”
 All variables in function are local

◦ They are not visible outside call!
Example Function
function sumIt=sumOddInt(finish)
sumIt=0;
for current=1:finish
if (mod(current,2)==1)
sumIt=sumIt+current;
end
end
end
% sumIt, current, and finish are local
When you call a function…
function bar2 executing
function bar2
function foo2 calls bar2
function foo2
function bar1 calls foo2
function bar1
function foo1 calls bar1
function foo1
function main calls foo1
function main
Recursion Example

Classic Example
◦ Function output = numbersSum(input)
if (input == 1)
output = 1;
else
output = input+numbersSum(input-1)
end
end
Download