Array Data Structures Algorithms

- Lecture 5
- One-Dimensional Arrays, Searching Sorting

Array Data Structures Algorithms

- Concepts of Data Collections
- Arrays in C
- Syntax
- Usage
- Array based algorithms

5A Concepts of Data Collections

- Sets, Lists, Vectors, Matrices and beyond

Sets Lists

- The human language concept of a collection of

items is expressed mathematically using Sets - Sets are not specifically structured, but

structure may be imposed on them - Sets may be very expressive but not always easily

represented - Example The set of all human emotions.
- Lists are representations of sets that simply

list all of the items in the set - Lists may be ordered or unordered.
- Some useful lists include data values and the

concept of position within the list. - Example representations 0, 5, -2, 4

TOM, DICK

First Second

Vectors, Matrices and beyond

- Many examples of lists arise in mathematics and

they provide a natural basis for developing

programming syntax and grammar - Vectors are objects that express the notion of

direction and size (magnitude) - The 3-dimensional distance vector D has

components Dx, Dy, Dz along the respective x,

y and z axes, with magnitude D

sqrt(Dx2Dy2Dz2) - We use descriptive language terminology such as
- The xth component of D
- ... Or, D-sub-x

Vectors, Matrices and beyond

- Higher dimensional objects are often needed to

represent lists of lists. - Matrices are one example that sometimes can be

represented in tabular form - 2-dimensional tables (row, column)
- 3- and higher are hard to visualize, but are

meaningful - Beyond this, mathematics works with objects

called tensors and groups (and other things), and

expresses the access to object members (data

values) using properties and indices based on

topologies (loosely put, shape structures)

Vectors, Matrices and beyond

- In this course we focus on an introduction to the

basic properties and algorithms associated with

one-dimensional arrays, or vectors - These objects are mathematically defined as

ordered, structured lists - The ordering derives from the property that each

element of the list exists at a specific position - Enumerated starting at 0 and incrementing by 1 to

a maximum value - The structure determines the mechanism and method

of access to the list and its member elements

5B Arrays in C

- Syntax
- Memory structure
- Usage
- Functions

Arrays in C

- Syntax
- How to declare and reference 1D arrays using

subscript notation - Memory structure
- How is RAM allocated the meaning of direct

access through subscripting - Usage
- Some simple illustrative examples
- Functions
- How to deal with arrays as function arguments

Arrays in C - Syntax

- Consider the array declarations
- int StudentID 1000 float Mark

1000 char Name 30 - Each of the declarations defines a storage

container for a specific maximum number of

elements - Up to 1000 Student (integer) ID values
- Up to 1000 (real) Marks
- A Name of up to 30 characters

Arrays in C - Syntax

- Each array is referred to by its declared name
- float A 100 ... where A refers to the

entire collection of 100 storages - On the other hand, each separate element of A is

referenced using the subscript notation - A0 12.34 / assign 12.34 to the first

element / / NOTE subscripts always

start from 0 / - AK 0.0 / assign 0 to the (K1)th

element /

Note Although a bit clumsy in human natural

language, we can change our use of language so

that AK always refers to the Kth element (not

K1), always starting from 0 as the 0th element.

Arrays in C - Syntax

- It is not necessary to initialize or reference

all elements of an array in a program - Unlike scalar variables declared as primitive

data types, these uninitialized, non-referenced

array elements are not flagged as warnings by the

compiler - There are good reasons to declare arrays of

larger size than might be required in a

particular execution run of a program - At the outset, design the program to accommodate

various sizes of data sets (usually acquired as

input), up to a declared maximum size - This avoids the need to modify and recompile the

program each time it is used.

Arrays in C - Syntax

define directives are normally located at the

beginning of the program source code, after

include directives, and before function

prototypes and the main function. By using the

defined symbol MAX_SIZE, changes to SID and Mark

array sizes can be accomplished by simply

changing the value assigned to MAX_SIZE and

recompiling.

- This is a good time to introduce another compiler

pre-processor directive, define - define is used to define constant expression

symbols in C programs. The value of such symbols

is that they localize positions of program

modification. - Example
- define MAX_SIZE 1000
- int main ( ) int SID MAX_SIZE

float Mark MAX_SIZE .....

Arrays in C Memory structure

- Now consider the declaration
- int A 9
- The entire allocation unitis called A the

array name - There must be 9 integersized allocations in RAM
- Each element is locatedcontiguously (in

sequenceand touching)

RAM

A

A0 A8

Arrays in C Memory structure

The sizeof, operator is a compile-time operator

(not an executable instruction or operator) that

determines the RAM storage size allocated to a

data structure. When sizeof is applied to a

primitive data type, it provides the size of

allocated storage, in bytes. Try running a

program with statements such as printf( The

size of int is d bytes\n, sizeof int )

- Arrays are often calleddirect access

storagecontainers - The reference to AKis translated by

thecompiler to - First, calculate the relative address offsetK

sizeof int - Second, add RAO tobase address of A, or

simplyA0 - AK A0 Ksizeof int

RAM

sizeof int

A0 A K

0 1 K

RAO

Direct Access Since the cost of the address

computation is always the same (constant) and it

provides the actual RAM address location where

the data is stored.

Arrays in C Usage

- Referencing arrays is straightforward using the

subscript notation - B A 5 / assign 6th element of A to B /
- A J lt A K / relational expression /
- B 0.5( AJ AJ-1 ) / finite

difference / - printf( d d d\n, A0, Amid, AN-1 )
- scanf ( dlflf, N, RK, R ) / Note /

Arrays in C Average vs Median

- Problem Input N real numbers and find their

average and median. - Assume the values are already sorted from lowest

to highest - Assume no more than 1000 values will be inputted
- Solution
- Declarations
- float X 1000, Sum, Ave, Median int N,

Mid

Arrays in C Average vs Median

- Declarations
- float A 1000, Sum 0.0, Ave, Median

int N, Mid, K - Input Data
- printf( Enter number of values in list )

scanf( d, N ) - / Enter all real values into array X /

for( K0 K lt N K ) scanf( f,

AK ) / NOTE must be used /

Sum AK

Arrays in C Average vs Median

- Compute Average and Median
- Ave Sum / (float) N / real division

/ - Mid N / 2 / (integer) midpoint of

list / Median A Mid - Report results
- printf( Average , Ave ) printf( Median

f\n, Median )

Arrays in C Related arrays

- Problem Obtain student marks from testing and

store the marks along with ID numbers - Assume marks are float and IDs are int data types
- Solution Related arrays
- Define two arrays, one for IDs and one for marks
- int SID 100 float Mark 100
- Coordinate input of data (maintain relationships)
- for( K 0 K lt N K ) scanf(

df, SIDK, MarkK )

Arrays in C Functions

U

- Passing arrays as parameters in functions

requires some care and some understanding. We

begin with an example. - Calculate the dot product of two 3-vectors U and

V. - Components U0, U1, U2 V0,

V1, V2 - Mathematics The dot product is defined as

DotProd( U, V ) U0V0 U1V1

U2V2 - Since the dot product operation is required

often, it would make a useful function.

V

U . V

Arrays in C Functions

- Solution function
- double DotProd3 ( double U3, double V3 )

return U0 V0 U1 V1 U2

V2 - Note the arguments which specify that arrays of

type double with exactly three (3) elements will

be passed. - Note that the limitation to 3 elements is

reflected in the design of the function name

DotProd3

Arrays in C Functions

- Extend this to dot product of N-dimensional

vectors - double DotProdN ( double U , double V , int

N ) double DPN 0.0 int K

for( K 0 K lt N K ) DPN UK VK

return DPN - Note the array arguments do not specify a maximum

array size. - This provides flexibility of design since now the

function can handle any value of N. It is up to

the programmer to ensure that the actual input

arrays and N conform to the assumptions.

Arrays in C Functions

- An alternative to the same code is to use pointer

references - double DotProdN ( double U, double V, int N

) double DPN 0.0 int K

for( K 0 K lt N K ) DPN UK VK

return DPN - Note the array arguments are now expressed as

pointer references. - This maintains the same flexibility as previously.

Arrays in C Functions

Pointers are not the same as ints ! If A is an

int (say, 5), then A always evaluates to the

next (or successor) value in sequence (ie.

6). On the other hand, if P is a pointer (say,

int , with value AK), then P evaluates to

the next (or successor) value in sequence, which

is usually the next element of an array (ie.

AK1).

- A final alternative to the same code is to use

pointer references altogether - double DotProdN ( double U, double V, int N

) double DPN 0.0 int K

for( K 0 K lt N K, U, V ) DPN U

V return DPN - The U and V variables are address pointers to the

array components. - U and V perform the action of updating the

pointers by an amount equal to the size of the

array data type (in this case double is usually 8

bytes), thus pointing to the next array component

in sequence.

Arrays in C Functions

- The previous examples have illustrated the

various ways that are used to pass array

arguments to functions. - double DotProd3 ( double U3, double V3 )
- double DotProdN ( double U , double V , int N

) - double DotProdN ( double U, double V, int N

) - There are important differences
- When the size of the array is specified

explicitly (eg. double U3) , some C compilers

will allocate storage space for all array

elements within the function stack frame - If arrays are declared within the function body,

they are almost always allocated within the stack

frame - When the array size is not stated explicitly, a

pointer to the array is allocated (much smaller

in size than the entire array)

Arrays in C Functions

- C compilers may perform code and storage

optimization - Create the most efficient executable code
- Create the most efficient use of RAM storage
- By allocating array storage within stack frames,

a significant amount of wasted space occurs due

to avoidable duplication of storage allocations - It also follows that a wastage of time occurs

since it is necessary to copy data from arrays

declared in the calling point code to arrays

declared in the called point. - Pointers solve most of these problems (with a

small, but acceptable, increase in processing

time) - Optimization is a difficult problem and is still

the subject of much research

Theoretical

5C Array Based Algorithms

- Searching
- Sorting

Very practical !

Array Based Algorithms

- Searching
- How to locate items in a list
- Simplicity versus speed and list properties
- Sorting
- Putting list elements in order by relative value
- Promoting efficient search

Search Algorithms

- Searching is a fundamentally important part of

working with arrays - Example Given a student ID number, what is the

Mark they obtained on the test? Do this for all

students who enquire. - Constructing a good, efficient algorithm to

perform the search is dependent on whether the

IDs are in random order or sorted. - Random order use sequential search
- Sorted order use divide-and-conquer approach

Search Algorithms - Random

PROBLEM 1 No guarantee that rand() will

produce a result and exit the for loop,

especially if the item does not exist. PROBLEM

2 It is possible that an array element position

will be accessed that has not had data stored

(will stop the program as an error

uninitialized data access violation).

- If a list is stored in random order a possible

search technique is to look at the list elements

in random order search - int srchID, K
- printf( Enter your SID ) scanf( d,

srchID ) - for( Krand() N srchID ! SID K

Krand() N ) - printf( SID d, Mark f\n, SIDK,

MarkK )

Search Algorithms - Linear

The Linear Search algorithm starts at the

beginning of the list and proceeds in sequence

K 0 / start at beginning of list / do

/ search the list / if ( srchID SID K

) break / exit loop if found / K

/ move to next position / while ( K lt N )

/ stop at end of list / These can be combined

in a single for structure as shown below.

- Note that the loop is controlled by two

conditions - KltN
- This demands that K be initialized at the

beginning of the list (K0) - It also ensures that traversal of the list stops

at the end of the logical list (since the actual

array size may be larger) - (2) srchID ! SIDK
- This ensures that as soon as the value is found,

the loop is exited immediately, thereby avoiding

unnecessary work

- If a list is stored in random order a better

search technique is to look at the list elements

in order, from the beginning of the list until

the element is found or the list elements are

exhausted - int srchID, K, N 100 / Assume 100

elements / - printf( Enter your SID ) scanf( d,

srchID ) - / Perform the search / for( K0 KltN

srchID ! SID K K ) - if( KltN ) printf( SID d, Mark

f\n, SIDK, MarkK )

Search Algorithms - Linear

- Since this search approach considers each element

of the list in sequence, it is called Sequential,

or Linear, search. - In any situation where the list being searched

does not contain the value being searched for,

all N elements must be considered - This is called the worst case scenario
- However, when the element will be found ...
- The best case occurs when we actually find the

value looked for in the first position considered - The average case refers to the statistical

average obtained over many searches of the list - This is clearly just N/2 elements considered

The term Linear derives from the fact that the

actual runtime performance of the algorithm, in

terms of numbers of distinct CPU operations, is

expressed as a formula like T A N B

This is just the equation for a line (recall

y mx c)

Search Algorithms - Linear

To appreciate the improvement, consider cases

where srchID will not be located in SID. For a

statistical spread of srchID values (some big,

some small) on average, only N/2 comparisons are

required to eliminate the need to search further.

The previous algorithm always requires N

comparisons. The complexity is still O(N),

however.

- In cases where the list is sorted, the search

algorithm can be improved upon .... - Assume that SID is sorted in ascending order
- / Perform the search / for( K0 KltN

srchID lt SID K K ) - if( KltN srchID SID K )

printf( SID d, Mark f\n, SIDK, MarkK

) - Always examine code carefully to ensure that the

logic properly accounts for all circumstances

that can arise, both positive and negative.

Search Algorithms - Efficiency

- The time complexity (efficiency, or cost) of an

algorithm is important in programming design - Algorithmic efficiencies can be divided into

several categories, depending on various

properties of the problem under consideration - NP-complete (computable in finite time)
- NP-hard (computable in principle, but requires

special care and may not actually execute in

reasonable time) - NP-incomplete (not guaranteed to execute in

finite time, contains unknowable aspects) - NP refers to the way that an algorithm performs

and is expressed as a polynomial which may also

include functions (such as exponential or

logarithm)

Search Algorithms - Efficiency

- Many problems are characterized by parameters

that describe the nature of the data set, or the

number of operations that must be performed to

complete the algorithm - Search problems assume a data set of size N
- For the Sequential Search algorithm (over N

values) - K 0 1 storedo if ( Value

SearchValue ) break 2 fetch, 1 compare, 1

branch K 1 fetch, 1 increment, 1

store while ( K lt N ) 2 fetch, 1

compare, 1 branch/ Report result / R

(constant) operations - / In the worst case, all N loops are completed

/ - Cost N ( 5 fetch 1 store 1 increment

2 compare 2 branch ) R 1 store

Search Algorithms - Efficiency

The Big O notation in this case is expressed

as Time Complexity O( NK ) for

F(N) Alternatively, we say the Order of F(N)

is NK Note that we ignore the leading

coefficient (aK) of NK, regardless of its

magnitude.

- Assume that the behaviour of an algorithm (ie.

how long it takes to execute) is described by a

function F(N) that counts the number of

operations required to complete the algorithm. - Consider the polynomial in N
- F(N) aK NK aK-1 NK-1 ... a1 N a0

... - As N increases to very large values, the smallest

terms, those with smaller powers (exponent) of N

become less relevant (regardless of the size of

coefficient aK) - Rather than using complicated polynomial formulas

to describe algorithmic time cost, a more

convenient notation has been developed the BIG

O (Order) notation.

Search Algorithms - Binary

- Let us now consider a list V of N values where

the elements VK are sorted in ascending order

(from smallest to largest) - V0 lt V1 lt ..... lt VN-2 lt VN-1
- Problem Find if/where the search value VS is in

V - We develop the Binary Search algorithm
- Our design technique is based on the principle of

Divide and Conquer - Also called Binary Subdivision

Search Algorithms - Binary

- Our strategy involves the idea of sub-list. A

sub-list is simply a smaller part of a list. - By dividing a list into two sub-lists (where each

sub-list contains contiguous values that are

sorted) it is possible to quickly eliminate one

of the sub-lists with a single comparison. - Thereafter, we focus attention on the remaining

sub-list but, we reapply the same divide and

conquer technique.

Search Algorithms - Binary

ignore

- We assume that all data has been inputted to

array V, N has been initialized with the number

of input values, and the search value VS has been

inputted. - We use the following declarations
- float V 10000 , VS int Lo, Hi, Mid, N
- Binary Search algorithm uses the variables Lo and

Hi as array subscripts - Lo refers to the first value position in a

sub-list - Hi refers to the last value position in a

sub-list - Mid is used as the midpoint subscript Mid

(LoHi)/2

ignore

Search Algorithms - Binary

- Binary Search algorithm
- Lo 0 Hi N-1 / Use full list as

sub-list / - do
- Mid ( Lo Hi ) / 2 / int

division / - if( VS VMid ) break
- if( VS gt VMid ) Lo Mid 1
- else Hi Mid 1
- while ( Lo lt Hi )
- printf( Search value f , VS ) if (

VS VMid ) printf( found at

position d\n, Mid ) else printf(

not found\n )

VS

VS

VS

?????

Search Algorithms - Binary

- To understand the complexity assume a data set of

256 values. We consider the worst case scenario. - Size of data set Step 256 1
- 128 2
- 64 3
- 32 4
- 16 5
- 8 6
- 4 7
- 2 8
- 1 9
- Once we split the sub-list to size 1 we clearly

determine that the list cannot contain the search

value.

N 256 28, so it has taken 81 9 steps to

prove that VS does not exist in V. In general,

for a list of size N 2K, it takes K1 steps, or

O( log2 N )

Search Algorithms - Binary

- In general, for a list of size N 2K, it takes

K1 steps, or O( log2 N ) time complexity - K is just the logarithm (base-2) of N
- The efficiency of the Binary Search algorithm is

logarithmic, or O( log N ). - Some people prefer to say O ( log2 N), but they

are mathematically identical

Search Algorithms

- The relative efficiencies, or complexities, of

the various search algorithms is clearly

established when N is large, and for the worst

case scenarios. - Random O( gtN ? )
- Sequential (Linear) O( N )
- Divide Conquer (Binary) O( log N )
- In the best case, any search algorithm may be

successful after only one (1) probe - Usually, one is interested in worst case and

average case in choosing an algorithm.

Sorting Algorithms

.... Putting things in order ....

.... order things Putting in ....

Sorting Algorithms

- From our discussion of binary search we

understand the need for the data to be ordered

within lists in order to promote fast searching

using Binary Search - It is also important to understand that not every

list should be sorted study each case carefully - Sorting algorithms are designed to perform the

ordering required - There are literally hundreds of specialized

sorting algorithms, with varying efficiencies - We will focus on a sorting algorithm called

Selection Sort

Sorting Algorithms - Selection

- Selection Sort relies on being able to find the

largest (or smallest) element in a sublist - Each time the proper value is found, it is

exchanged with the element at the end of the

sublist - We re-apply this technique by shrinking the size

of the sublist, until there is no remaining

sublist (or a sublist of size 1 element which

is already sorted). - We consider the example ..........

Sorting Algorithms - Selection

1

2

3

4

Sorting Algorithms - Selection

5

8

6

7

Sorting Algorithms - Selection

- From the example we note that the final step 8 is

not actually required, since a sub-list of one

(1) element is automatically sorted (by

definition). - Hence, it took 7 steps to complete the sort. In

general, for a list of size N elements, it takes

N-1 steps. - Each step consists of two parts
- First, search an unordered sub-list for the

largest element - The size of the sub-list is N-K for step K

(starting from K0) - Second, exchange (swap) the largest element with

the last element in the sub-list - Upon completion of each step it should be noted

that the sorted sub-list grows by one element

while the unsorted sub-list shrinks by one

element.

Sorting Algorithms - Selection

- Start with a sub-list of N unsorted values, and a

sub-list of 0 sorted values. - The unsorted sub-list has subscripts in the

subrange 0..N-1 - The sorted sub-list has subscripts in the

subrange N..N which is not occupied physically

(hence, it does not exist, it is the empty set) - For K from 0 to N-1, in increments of 1, perform
- Search the unordered sub-list 0..N-1-K for the

largest value and store its position P - Exchange the largest element with the last

element in the sub-list using positions P and

N-1-K. - / The exchange adds the largest element to the

beginning of the sorted sub-list, while removing

it from the end of the unsorted sub-list /

Sorting Algorithms - Selection

Swapping of array elements must be done with some

care and thought. Three statements are required,

and a temporary storage variable must be used.

- void SelectionSort ( double List , int N )
- int J, K, P double Temp
- for( J 0 J lt N-1 J )
- P 0
- for( K 0 K lt N - J K )
- if( ListP lt ListK ) P K
- Temp ListP ListP

ListN-1-J ListN-1-J Temp

Temp ListP ListN-1-J

62

1

62

56

3

2

56

62

Sorting Algorithms - Selection

/ Define a dswap function / void dswap (

double A, double B ) double T

T A A B B T

return

- void SelectionSort ( double List , int N )
- int J, K, P
- double Temp
- for( J 0 J lt N-1 J )
- P 0
- for( K 0 K lt N - J K )
- if( ListP lt ListK ) P K
- Temp ListP ListP

ListN-1-J ListN-1-J Temp

dswap ( ListP, ListN-1-J )

Sorting Algorithms - Selection

- void SelectionSort ( double List , int N )
- int J, K, P double Temp
- for( J 0 J lt N-1 J )
- P 0
- for( K 0 K lt N - J K )
- if( ListP lt ListK ) P K
- Temp ListP ListP

ListN-1-J ListN-1-J Temp

How many operations must be performed? Sum from

J 0 to N-2 Sum from K 0 to N-1-J

Core loop logic (CoreOps) Gauss dealt with this

problem and developed several formulae which bear

his name. In this case the answer is ½ N ( N

1 ) CoreOps

Sorting Algorithms - Selection

- The maximum number of operations required to sort

a list of N elements in ascending order is - ½ N ( N 1 ) CoreOps
- The CoreOps consist of several fetches, one

comparison and either 1 or 2 assignments (stores) - This is, essentially, a constant value
- Thus, the time complexity of the Selection Sort

algorithm is - O( N 2 )

Sorting Algorithms

- There are many other sorting algorithms
- InsertionSort
- QuickSort
- MergeSort
- Some of these are iterative, while others are

recursive. - A number of sorting algorithms exhibit time

complexities of O( N2 ), but some achieve better

efficiencies (eg. O( N log N ) ) under certain

circumstances. - Additional algorithms will be discussed in 60-141

and other computer science courses

Other Array Applications

Recommendation for Learning Some students

experience difficulties learning mathematical

concepts, from algebra to statistics.

Programming the techniques will greatly improve

understanding and help to cement the concepts

with a foundation based on application and

practice.

- One-dimensional arrays are used in many fields of

science, mathematics, engineering and almost any

subject for which computational simulation is

involved - Computer Graphics and Gaming
- Physics and Chemistry
- Financial modeling
- Business accounting
- In all cases it is important to understand the

role that vectors play (conceptually and

theoretically) in addition to simply using arrays

as storage containers - Typically, mathematical properties are used to

develop algorithms and also to determine which

algorithms are the best (optimal) choice in

particular circumstances.

Recursive Binary Search

Example usage for calling P BinSearch( VS,

V, 0, N-1 ) if( P -1 ) printf(

Value lf not found, VS ) else

printf( Value lf found at position d\n, VS, P

)

- Binary Search can be expressed very elegantly

using recursion - int BinSearch ( float VS, float V , int Lo,

int Hi ) - int Mid
- if( Lo gt Hi ) return -1
- Mid ( Lo Hi ) / 2
- if( VS VMid ) return Mid
- if( VS lt VMid ) return BinSearch( VS, V, Lo,

Mid-1 ) - else return BinSearch( VS, V, Mid1, Hi )

Recursive Functions

- Many algorithms are beautifully and elegantly

expressed using recursion - Programming recursion requires some experience to

perfect, however it is considered a more natural

way of thinking to most humans - It is true, in general, that any iterative

algorithm can be expressed as a recursive

algorithm and vice versa. - In practice, it is not so obvious !
- Students may be tested on recursion, but examples

and problems will be straightforward.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates. - !! DONE !!

Towers of Hanoi

- Base Case 1 Move plate from A to B

Towers of Hanoi

- Base Case 1 Move plate from A to B
- Hanoi ( A, B, C, 1 ) / Move from A to B, C not

used /

Towers of Hanoi

- Base Case 2

Towers of Hanoi

- Base Case 2 Move top plate from A to C

Towers of Hanoi

- Base Case 2 Move top plate from A to C

Move bottom plate from A to B

Towers of Hanoi

- Base Case 2 Move top plate from A to C

Move bottom plate from A to B Move top plate

from C to B - Hanoi ( A, B, C, 2 ) / Move from A to B, but

use C /

An intriguing idea starts to emerge .... Hanoi (

A, B, C, 2 ) was accomplished by First applying

Hanoi ( A, C, B, 1 ) Then applying Hanoi

( A, B, C, 1 ) And, finally Hanoi ( C,

B, A, 1 )

Towers of Hanoi

- Base Case 3 Move top plate from A to B

Move middle plate from A to C Move top plate

from B to C Move bottom plate from A to B

Move top plate from C to A Move middle plate

from C to B Move top plate from A to B

Towers of Hanoi

- Base Case 3 Move top plate from A to B

Move middle plate from A to C Move top plate

from B to C Move bottom plate from A to B

Move top plate from C to A Move middle plate

from C to B Move top plate from A to B

Towers of Hanoi

This seems to be getting complicated, until we

realize that Hanoi ( A, B, C, 3 ) can be

expressed as Hanoi ( A, C, B, 2 ) Hanoi (

A2, B2, C, 1 ) Hanoi ( C, B, A, 2 )

- Base Case 3 Move top plate from A to B

Move middle plate from A to C Move top plate

from B to C Move bottom plate from A to B Move

top plate from C to A Move middle plate from C

to B Move top plate from A to B

Towers of Hanoi

In general, applying mathematical induction, we

find that Hanoi ( A, B, C, N ) can be expressed

as void Hanoi ( int A , int B , int C ,

int N ) if( N 1 ) B0 A0 return

if( N 2 ) C0 A0 A0

B0 C0 B0 return Hanoi ( A, C,

B, N-1 ) Hanoi ( AN-1, BN-1, C, 1 )

Hanoi ( C, B, A, N-1 ) return

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates.

Towers of Hanoi

Most people are able to grasp the sequence of

movements to solve the Towers of Hanoi

problem. The solution is recursive, built up

from handling base cases of N1, 2 and 3 plates.

Once the pattern is understood, it is then

reapplied to arbitrary N. Programming an

iterative algorithm is much harder, however. (

Try it but in your spare time )

- Problem Move all plates from A to either B or

C, such that at all times smaller plates are on

top of larger plates. - !! DONE !!

5D Summary

- Concepts Mechanisms
- Searching
- Sorting

Summary

- Concepts Mechanisms
- Algorithms
- Searching
- Sorting
- Reading Chapter 6 , 7.1 7.4
- Ignoring those parts that discuss technical

aspects relating specifically to

multi-dimensional arrays - Simple aspects of pointers and call-by-reference
- The course may have been challenging for some

students and straightforward for others - Remember that this is what is expected of

professional experts (ie. University graduates)

Into the Future

- A look ahead ...

Into the Future

- Looking ahead to the 60-141 course, these topics

will be introduced or advanced. - Pointers
- Multi-dimensional Arrays
- String Character processing
- Advanced Treatment of Functions
- Abstract Data Structures
- Dynamic Storage Allocation Techniques
- File Based I/O
- Design Programming Paradigms
- Reading - Chapters 7-14 and beyond.

Into the Future

- Dont forget to complete and submit the last

assignment - Due the day after the Final Examination
- Study hard for the Final Examination
- Review all slides, review the assigned textbook

readings - Review all labs, assignments and examples (slides

and textbook) - Focus on material covered from second midterm to

end of lectures

Into the Future

- I wish you all a safe and satisfying summer,

whether vacationing or working. - Good luck to those taking additional courses
- Work hard and make your own good luck!
- - Dr. Bob Kent

The end of the beginning