General Computer Science for Engineers CISC 106 Lecture 07 - PowerPoint PPT Presentation

About This Presentation
Title:

General Computer Science for Engineers CISC 106 Lecture 07

Description:

General Computer Science for Engineers CISC 106 ... Try splitting the array in half Binary Search Flowchart diagram of the algorithm Note the two stopping ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 34
Provided by: Sam3143
Learn more at: http://www1.udel.edu
Category:

less

Transcript and Presenter's Notes

Title: General Computer Science for Engineers CISC 106 Lecture 07


1
General Computer Science for EngineersCISC
106Lecture 07
  • James Atlas
  • Computer and Information Sciences
  • 06/29/2009

2
Lecture Overview
  • Linear Search (recursive)
  • Binary Search
  • Midterm 1 Review
  • Unix commands
  • if statements
  • arrays (or matrices)
  • loops
  • scripts
  • functions
  • recursion

3
Linear search
  • Searching, extremely important in computer
    science
  • If we have a list of unsorted numbers, how could
    we search them?

4
Iterative strategy
  • Given 5 4 2 10 6 7 find which position 6
    occupies
  • Alternatively, does the array contain the number
    6?

5
Recursive strategy
6
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

7
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 lt N search top half of list
  • else search bottom half of list

8
Lets do this in Matlab
  • Recursive solution

9
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.
10
(No Transcript)
11
Midterm and Review
  • Midterm review online
  • http//www.udel.edu/CIS/106/jatlas/09Su/exams/09Su
    /midterm1review.pdf
  • Midterm 1
  • July 1 (Class Time Wednesday!)

12
Important Notes on Exam
  • Write pseudo-code from memory
  • Study labs
  • Study Midterm review
  • Blooms Taxonomy

13
Blooms Taxonomy
14
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

15
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

16
IF/Elseif Statements
  • if (expression1)
  • statements1
  • elseif (expression2)
  • statements2
  • else
  • statements3
  • end

17
Major Relational Operators
  • A lt B A is less than B
  • A gt B A is greater than B
  • A lt B A is less than or equal to B
  • A gt B A is greater than or equal to B
  • A B A is equal to B
  • A B A not equal B

18
If statements
  • print blue if N lt 5
  • print red if N gt 5 and N lt 10
  • print green if N gt 10

19
If statements (contd)
  • if (N lt 5)
  • fprintf('blue\n)
  • end
  • if (N gt 5 N lt 10)
  • fprintf('red\n)
  • end
  • if (N gt 10)
  • fprintf('green\n)
  • end

20
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
  • x3.14 ? scalar
  • a 1,2,3,4,5 ? vector

21
Arrays (aka matrices)
  • x 10.55
  • 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

22
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
23
For Loops
  • Used when you know how many times code is to be
    executed.
  • Syntax
  • for ltvariablegt ltstartgtltincrementgtltendgt
  • 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.

24
Example Problem
  • I want to find the average of widgets sold in 4
    days

Day of widgets sold
1 15
2 22
3 20
4 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!

25
Example Problem
  • total 0
  • for i 111000 loop starts at 1
  • total totalwidget (i) loop increments by 1
  • end loop ends at 1000
  • avg total / 1000

26
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

27
A Loop Analogy (while)
  • The runner executes a loop.
  • If they dont know the distance they want to run
    (run until tired)
  • While loop
  • tired false
  • while(tired)
  • tired runLap()
  • end

28
Scripts files
  • Store commands in
  • Variables are global, available after you call
    script file

29
Scripts files
  • sumIt0
  • for current1finish
  • if (mod(current,2)1)
  • sumItsumItcurrent
  • end
  • end

30
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!

31
Example Function
  • function sumItsumOddInt(finish)
  • sumIt0
  • for current1finish
  • if (mod(current,2)1)
  • sumItsumItcurrent
  • end
  • end
  • end
  • sumIt, current, and finish are local

32
When you call a function
function bar2
function bar2 executing
function foo2 calls bar2
function foo2
function bar1
function bar1 calls foo2
function foo1
function foo1 calls bar1
function main
function main calls foo1
33
Recursion Example
  • Classic Example
  • Function output numbersSum(input)
  • if (input 1)
  • output 1
  • else
  • output inputnumbersSum(input-1)
  • end
  • end
Write a Comment
User Comments (0)
About PowerShow.com