Professional Documents
Culture Documents
CO1401 Week 7 Lecture
CO1401 Week 7 Lecture
Programming
Week 7
Algorithms, Binary Search
Algorithms
2 Programming
Algorithms
4 Programming
Efficiency and Algorithms
• Speed
• Memory
5 Programming
Measuring Algorithms - Linear Search
6 Programming
Measuring Algorithms - Linear Search
7 Programming
Description of the algorithm
• It is common to consider
• best case: what's the best thing that could happen.
The best performance.
• worst case: what the worst thing that could
happen. The worst performance.
• average case: on average what happens. We
consider performing the search time and time
again. We then calculate what happens when we
average all of the tests out.
• Our measurement will be the number of times that
we have to examine the list.
9 Programming
Efficiency of linear search
1+2+3=6
6/3=2
• On average we find
the number the
second time we
examine the list
10 Programming
Efficiency of linear search
11 Programming
Binary search
12 Programming
• The algorithm is quite simple in concept.
• Searching for a value in an ordered list. An example of an
ordered list would be a telephone directory or dictionary.
• First Pass: Open the directory at the half way point and
choose a word from the page.
• If our target (the search term) is alphabetically lower
than the chosen word then our target must be in the
first half of the directory.
• If our target is alphabetically higher than the chosen
word then our target must be in the second half of the
directory.
13 Programming
• Let's say it was in the first half of the directory.
• Second Pass: Using just the first half of the directory,
divide it into two and choose another word.
• Again if our target is alphabetically lower than the
chosen the word then our target must be in the first half
of the section. (In other words, in the first quarter of the
whole directory).
• If our target is alphabetically higher than the chosen the
word then our target must be in the second half of the
section. (In other words, in the second quarter of the
whole directory).
16 Programming
int BinarySearch(int a[], int low, int high, int target)
{
while (low <= high)
{
int middle = ( low + high ) / 2;
if ( target < a[middle] )
{
high = middle - 1;
}
else if ( a[middle] < target )
{
low = middle + 1;
}
else
{
return middle;
}
}
return -1; // not found, what to do?
}
int main()
{
int arr[10] = { 0, 2, 3, 5, 7, 8, 9, 11, 14, 15 };
18 Programming
• Should check for array bounds at every stage of the
algorithm.
• In every pass of the algorithm we need to divide the
range of elements we're interested in into two, i.e. we
need to find the index half way between a minimum
value and a maximum.
20 Programming
21 Programming
22 Programming
Global variables and constants
23 Programming
• A global variable is a variable whose scope is the whole
of the program.
• Global variables are not considered good practice.
• In order to understand a program you need to know
what the variables are doing. You need to know the
current state of the program.
• This becomes difficult with global variables because
every function could change the state of the program
since every function can change a global variable.
24 Programming
• Constants are not considered variables because their
value is fixed.
• A function cannot use a constant to change the state of
the program.
• Constants may be faster than non-constants.
• For future reference: constants should really be
declared inside a namespace.
25 Programming
C-style character arrays
and printf
26 Programming
• I was struck by how many examples people were finding used
printf.
• It also turns up a lot in the MSDN help files.
• You need to know a little about c-style character arrays in
order to use printf.
• When people talk about strings in the context of printf they
mean the old style C character array.
• This is an array of characters.
• The array is always 1 size longer than the characters in the
character array.
• This extra space is for the addition of a 0 (zero) at the end of
the character array. The last "character" in the array is a 0. It's
called a terminating 0.
27 Programming
character arrays and Arrays
28 Programming
character array Elements
• The individual elements of the character array can be
accessed in the same way as normal character
variables.
char str[3];
str[0] = ‘h’;
str[1] = ‘i’;
str[2] = 0;
31 Programming
• You can initialise c-style character arrays:
char str[10] = "cat";
• You cannot assign values to them in the way you can
with C++ strings. Instead you need to use the function
strcpy:
strcpy( str, "dog" );
32 Programming
• printf is part of the older way of dealing with strings.
• It is an output function.
• It is not a class.
• The 'f' stands for "formatted", i.e. a function which
does formatted output.
• It does not use the string class.
• It uses old c-style character arrays.
33 Programming
Formatted output - printf
• escape sequence
• \n newline
• \t tab
35 Programming
Examples
int i = 12;
printf ( "The number is %d.\n", i );
• The number is 12.
char ch = 'k';
printf( "mixed: %c and %d %s!\n", ch, i, str );
• mixed: k and 12 and hello!
36 Programming
Code for example
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
int i = 12;
printf ( "The number is %d.\n", i );
char ch = 'k';
printf( "mixed: %c and %d %s!\n", ch, i, str );
}
37 Programming
Input / Output Functions
•In C++ there are functions called form and scan which
do the same thing as printf and scanf:
• cout.form("%d%s", i, s); // C++
• cin.scan("%d%s", &i, s); // C++
38 Programming