CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms:

advertisement
CS107
Introduction to Computer Science
Lecture 5, 6
An Introduction to Algorithms:
List variables
Exercise
Write an algorithm that asks the user for 10 temperature
measurings, and prints out the temperatures entered and
their average. For example:
Enter 10 temperatures: 21, 32, 34, 56, 67, 89, 21,
45, 67, 54
The recorded temperatures are: 21, 32, 34, 56, 67,
89, 21, 45, 67, 54. The average is: …
Exercise
• Change your previous program so that it handles
–
–
–
–
20 temperatures
50 temperatures
100 temperatures
1000 temperatures
List variables
• How to represent inputs of arbitrary size?
• Suppose that we need to read 100 numbers from the user, or
1000, or..
– we could give each variable a different name…tedious!!
• Use a list variable:
–
–
–
–
–
Variable: list a of size n
This means that a is a list of n elements: a1, a2, a3,…,an
To read the list from the user use a loop to read each element
To print the list use use a loop to print each element
We can treat each element in the list as a variable
• Set a3 to 5
• Set a4 to a3 +2
• If (a4 == a3 ) then print “equal”
List variables
• We used to write
– Variable: a, b, c, sum, avg, i etc
• If we want to use a list we’ll write
– Variable: list a of size 100
– This tells the “computer” that a is a list variable which can hold
100 items
– Now we can access any item in the list as a normal variable, by
specifying its index
• Set a2 to a9
• Set i = 10
• Set ai to a3 +2
List examples
• Reading a list of 100 elements from the user
Variables: i, n, list a of size 100
Print “Enter 100 elements: ”
n = 100
i=1
while (i <= n)
print “enter next element”
get ai
i = i+1
Print “Great, thanks.”
List examples
• Printing a list to the user
Variables: i, n, list a of size 100
n = 100
i=1
Print “The list is: “
while (i <= n)
print ai
i = i+1
Print “Done”
List examples
• What does the following code do?
n = 10
i=1
while (i <= n)
ai = i
i = i+1
What does the following code do?
x=0
i =1
while (i<= n)
x = x + ai
i = i+1
print x
Example: putting it together..
• What does the following do?
variables: I,n,x, list a of size 10
n = 10
print “Enter “ n “ numbers:”
i=1
while (i <= n)
get ai
i = i+1
x=0
i =1
while (i<= n)
x = x + ai
i = i+1
print x
Searching
• Problem: Write an algorithm that reads from the user a list
of 100 numbers and a target value, and searches to see if
any element in the list is equal to the target value. If not,
prints “target not found”. If yes, prints “target found”.
Searching, variations
• Modify your search algorithm so that:
– It prints the location (i.e. index in the list) where it finds the target
– It finds only the first occurence of target
– It finds all occurences of target (and prints their locations)
– It counts the number of occurences of target in the list
– It counts how many elements in the list are larger than target
More exercises
• Write an algorithm that reads a list of 100 numbers
from the user and
– prints out the average of all numbers in the list.
– prints out the largest element in the list
– prints out the smallest element in the list
Iterating through a list
• In general, an algorithm that explores every single
element in the list in order will look something
like this:
Set i = 1
while (i <= 100)
<do something with element ai>
Set i = i+1
jargon: this is called iterating through the list
Searching
•
•
•
•
•
•
•
•
The following algorithm finds the first occurrence of target and prints
out its position.
Variables: i, target, list a of 100 elements
i=1
while (i <= 100)
– Print “enter number: “ i “:”
– Get ai
– i = i+1
Get target
Set found = 0, i = 1
While ((i <= 100) and (found == 0))
– If ai == target set found = 1
– i = i+1
If (found ==1) print “Target found at position” i-1
Else print “ Target not found.”
Searching
• Assume a list of 100 elements
– What is the fastest that our search algorithm can finish?
• This is called best-case time of the algorithm
– What is the best-case input like?
– What is the slowest that our search algorithm can finish?
• This is called worst-case time of the algorithm
– What is the worst-case input like?
• Can we do better?
– That is, faster?
– In other words, can we find an algorithm that can decide whether
the target is in the list without checking every single element?
Phone book search
• How do we search in a phone book?
• Check the middle value
• If smaller than target, go right
• Otherwise go left
• Problem: find a target in a sorted list
– How can we exploit that the list is sorted?
– Can we come up with an algorithm?
Download