Array Data Structures & Algorithms

Download Report

Transcript Array Data Structures & Algorithms

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(Dx2+Dy2+Dz2)
 We use descriptive language terminology such as
 The x’th 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
Note:
referenced
the subscript
notation
Although using
a bit clumsy
in human natural
language,
we can change our use of language so that A[K]
always refers to the K’th element (not K+1),
 A[0] = 12.34 ; /* assign 12.34 to the first element */
always starting from 0 as the 0’th element.
/* NOTE: subscripts always start from 0 */

A[K] = 0.0 ;
/* assign 0 to the (K+1)’th 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
 This is a good time to introduce another compiler preprogram
source
code,#define:
after #include directives, and before
processor
directive,
function prototypes and the main function.
By#define
is used
to symbol
define constant
expression
in
using the
defined
MAX_SIZE,
changes symbols
to SID and
C programs.
value
of such symbols
is that
they the
Mark
array sizes The
can be
accomplished
by simply
changing
localize
positions
of program
modification.
value
assigned
to MAX_SIZE
and recompiling.
 Example:


#define MAX_SIZE 1000
int main ( ) {
int SID [ MAX_SIZE ] ;
float Mark [ MAX_SIZE ] ;
.....
}
Arrays in C – Memory structure
 Now consider the declaration
RAM
 int A [ 9 ] ;
 The entire allocation unit
is called A – the array name
 There must be 9 integer
sized allocations in RAM
 Each element is located
contiguously (in sequence
and “touching”)
A[0]
A[8]
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.
 Arrays
often called
When
sizeofare
is applied
to a primitive data type, it provides the
direct access
size of storage
allocated storage, in bytes. RAM
containers
Try running a program with statements such
as:int
sizeof
 The reference to A[K]
is printf(
translated
“The by
sizethe
of int is %d bytes\n”,
A[0] sizeof int ) ;
Arrays in C – Memory structure
compiler to
0
1
 First, calculate the
RAO
relative address offset
K * sizeof int
 Second, add RAO to
A[ K ]
base address of A, or simply
&A[0]
 &A[K] == &A[0] + K*sizeof int
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.
K
Arrays in C – Usage
 Referencing arrays is straightforward using the
subscript notation
 B = A[ 5 ] ; /* assign 6th element of A to B */
 A [ J ] < A [ K ] /* relational expression */
 B = 0.5*( A[J] – A[J-1] ); /* finite difference */
 printf( “%d %d %d\n”, A[0], A[mid], A[N-1] ) ;
 scanf ( “%d%lf%lf”, &N, &R[K], 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( K=0; K < N; K++ ) {
scanf( “%f”, &A[K] ) ; /* NOTE: & must be used */
Sum += A[K] ;
}
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 < N ; K++ )
scanf( “%d%f”, &SID[K], &Mark[K] ) ;
U
Arrays in C – Functions
 Passing arrays as parameters in functions
requires some
V
care and some understanding. We begin with an example.
U.V
 Calculate the dot product of two 3-vectors U and V.
 Components:
U[0], U[1], U[2]
V[0], V[1], V[2]
 Mathematics: The dot product is defined as
DotProd( U, V ) ::= U[0]*V[0] + U[1]*V[1] + U[2]*V[2]
 Since the dot product operation is required often, it would
make a useful function.
Arrays in C – Functions
 Solution function:
 double DotProd3 ( double U[3], double V[3] ) {
return U[0] * V[0] + U[1] * V[1] + U[2] * V[2] ;
}
 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 < N ; K++ ) DPN += U[K] * V[K] ;
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 < N ; K++ ) DPN += U[K] * V[K] ;
return DPN ;
}
 Note the array arguments are now expressed as pointer
references.
 This maintains the same flexibility as previously.
Pointers are not the same as int’s !
Arrays in C – Functions
If A is an int (say, 5), then A++ always evaluates to the next (or
 A final alternative
to the same
code
is to use
successor)
value in
sequence
(ie.pointer
6).
references altogether:
On the other hand, if P is a pointer (say, int *, with value
&A[K]), then
P++ evaluates
next (or *successor)
 double
DotProdN
( doubleto*the
U, double
V, int N ) {value in
sequence,
which
is usually
the next element of an array (ie.
double DPN
= 0.0
;
&A[K=1]).
int K ;
for( K = 0 ; K < 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 U[3], double V[3] );
 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
U[3]) , 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
5C: Array Based Algorithms
Searching
Sorting
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
If a list is1:stored in random order a possible
No
guarantee
that rand()
produce
a result
and exit the in
search
technique
is will
to look
at the
list elements
for loop, especially if the item does not exist.
random order search
PROBLEM 2:
int srchID,
K an
; array element position will be
Itis possible
that
accessed
has your
not had
 printf(that
“Enter
SIDdata
“ ) ; stored (will stop the
program
as “%d”,
an error
– uninitialized
data access violation).
scanf(
&srchID
);

for( K=rand() % N ; srchID != SID[ K ] ; K=rand() % N ) ;

printf( “SID = %d, Mark = %f\n”, SID[K], Mark[K] );
Search
Algorithms
Linear
The Linear Search algorithm starts at the beginning of
Note that the loop is controlled by two conditions:
proceeds
sequence:
 Ifthe
a list
list and
is stored
in in
random
order a better search
(1)
K<N
• technique
This demands
that
K beatinitialized
at
the beginning
of
is
to
look
the
list
elements
in
order,
K
=0
; (K=0)
/* start at beginning of list */
the
list
from
beginning
listtheuntil
the element is
{ the
/* ensures
search
the
list
*/of the of
• do
It also
that
traversal
list stops at the end
( srchID
==list
SID[
Kelements
] ) the
break
; are
/* array
exitexhausted
loop
found
found
–logical
or the
list
ofifthe
(since
actual
sizeifmay
be */
K++ ; /* move to next position */
larger)
while (!=KSID[K]
< N ) ; /* stop at end of list */
(2) }srchID
int srchID, K, N = 100 ; /* Assume 100 elements */
•  This
ensures
that
as soon
as
the value is found, the
printf(
“Enter
your
SID
“
)
;
These
can
be combined
in athereby
single for
structure as
loop
is
exited
immediately,
avoiding
scanf(
“%d”, &srchID ) ;
shown
below.
unnecessary work
 /* Perform the search */
for( K=0; K<N && srchID != SID[ K ] ; K++ ) ;

if( K<N )
printf( “SID = %d, Mark = %f\n”, SID[K], Mark[K] );
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,
element
will the
be actual
foundruntime
...
The
term Linearwhen
derivesthe
from
the fact that
performance
of the
algorithm,
in terms
numbers
distinct
 The “best
case”
occurs when
weof
actually
findofthe
value
CPU operations,
is the
expressed
as a formula
like:
looked for in
first position
considered
 The “average case” refers to the statistical average
T = A*N + B
obtained over many searches of the list
Thisthe
is clearly
justfor
N/2aelements
considered
This is just
equation
line (recall:
y = mx + c)
To appreciate the improvement, consider cases where srchID will
not be located in SID.
Search Algorithms - Linear
For a statistical spread of srchID values (some big, some small) on
 In only
cases
the listare
is sorted,
search algorithm
average,
N/2where
comparisons
requiredthe
to eliminate
the need to
search
can be improved upon
....further.
 Assume that SID is sorted in ascending order
The previous algorithm always requires N comparisons. The
complexity is still O(N), however.
 /* Perform the search */
for( K=0; K<N && srchID < SID[ K ] ; K++ ) ;

if( K<N && srchID == SID[ K ] )
printf( “SID = %d, Mark = %f\n”, SID[K], Mark[K] );
 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 store
do {
if ( Value == SearchValue ) break ; 2 fetch, 1 compare, 1 branch
K++ ;
1 fetch, 1 increment, 1 store
} while ( K < 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
The Big O notation in this case is expressed as
Search
Algorithms
Efficiency
Time Complexity ::= O( N ) for F(N)
K
 Assume that the behaviour of an algorithm (ie. how long it
takes
to execute)we
is described
a function
F(N)is
that
Alternatively,
say – thebyOrder
of F(N)
NK
counts the number of operations required to complete the
algorithm.
Note
that we ignore the leading coefficient (aK) of NK,
 Consider the
polynomialof
in its
N: magnitude.
regardless

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 V[K] are sorted in ascending order (from
smallest to largest)

V[0] < V[1] < ..... < V[N-2] < V[N-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
A[0]
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
A[Mid]
to quickly eliminate one of the sub-lists with a single
A[Mid+1]
comparison.
 Thereafter, we focus attention on the remaining sub-list
– but, we reapply the same divide and conquer
technique.
A[N-1]
S
U
B
L
I
S
T
S
U
B
L
I
S
T
Search Algorithms - Binary

i
g
n
We assume that all data has been inputted to array
V,
o
N has been initialized with the number of input values,
r
e
and the search value VS has been inputted.
 We use the following declarations

float V [10000] , VS ;
int Lo, Hi, Mid, N ;
Lo
Mid
 Binary Search algorithm uses the variables Lo and Hi
as array subscripts
Hi
 Lo refers to the first value position in a sub-list
 Hi refers to the last value position in a sub-list
ignore
 Mid is used as the midpoint subscript: Mid = (Lo+Hi)/2
S
U
B
L
I
S
T
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 == V[Mid] ) break ;
if( VS > V[Mid] ) Lo = Mid + 1 ;
Mid
else
Hi = Mid – 1 ;
} while ( Lo
<= Hi ) ;
?????
printf( “Search value %f “, VS ) ;
Hi
if ( VS == V[Mid] )
printf( “found at position %d\n”, Mid );
else
printf( “not found\n” ) ;





Lo
VS
VS
VS
S
U
B
L
I
S
T
Search Algorithms - Binary
 To understand the complexity assume a data set of
256 values. We consider the worst case scenario.









Size of data set
256
128
64
32
16
8
4
2
1
Step #
1
N = 256 = 28,
2
so it has taken 8+1 = 9
3
steps to prove that VS
4
does not exist in V.
5
6
In general, for a list of
7
size N = 2K, it takes K+1
steps, or O( log2 N )
8
9
 Once we split the sub-list to size 1 we clearly determine
that the list cannot contain the search value.
Search Algorithms - Binary
 In general, for a list of size N = 2K, it takes
K+1 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
 Sequential (Linear)
 Divide & Conquer (Binary)
O( >N ? )
O( N )
O( log N )
 In the best case, any search algorithm may be
T
successful
after only one (1) probe
i
m
e
 Usually,
one is interested in worst case and average
case in choosing an algorithm.
N
Sorting Algorithms
.... order
Puttingthings
thingsPutting
in order
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
82
(or smallest) element in a sublist
 Each time the proper value is found, it45is exchanged
with the element at the end of the sublist
31
 We re-apply this technique by shrinking the size of the
72
sublist, until there is no remaining sublist (or a sublist
of size 1 element – which is already sorted).
56
62
 We consider the example ..........
87
90
Sorting Algorithms - Selection
82
82
82
62
62
62
45
45
45
45
45
45
31
31
31
31
31
31
72
72
72
72
72
56
56
56
56
56
56
72
62
62
62
82
82
82
87
87
87
87
87
87
90
90
90
90
90
90
1
2
3
4
Sorting Algorithms - Selection
62
56
56
31
31
31
45
45
45
45
45
45
31
31
31
56
56
56
56
62
62
62
62
62
72
72
72
72
72
72
82
82
82
82
82
82
87
87
87
87
87
87
90
90
90
90
90
90
7
8
5
6
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 K=0)
 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
0
31of
0 sorted values.
45
 The unsorted sub-list has subscripts in the subrange [0..N-1]
 The sorted sub-list has subscripts in the subrange [N..N] which
56
is not occupied physically (hence, it does not exist, it is the
empty set)
62
 For K from 0 to N-1, in increments of 1, perform
72
 Search the unordered sub-list [0..N-1-K] for the largest value
82
and store its position P
 Exchange the largest element with the last element in the 87
sublist using positions P and N-1-K.
N-1
90
 /* The exchange adds the largest element to the
beginning
of
(N-1-K)
the sorted sub-list, while removing it from the end of the
unsorted sub-list */
Sorting Algorithms - Selection
 void SelectionSort ( double List[
], int of
N array
) { elements
Swapping
must be done with some care

int J, K, P ;
and thought. Three statements
double Temp ;
are required, and a temporary

for( J = 0 ; J < N-1 ; J++ ) { storage variable must be used.

P=0;
for( K = 0 ; K < N - J ; K++ )
if( List[P] < List[K] ) P = K ;
Temp = List[P] ;
List[P] = List[N-1-J] ;
List[N-1-J] = Temp ;




 }
Temp
62
1
List[P]
62
56
3
2
}
List[N-1-J]
56
62
/* Define a dswap function */
void dswap ( double * A, double * B ) {
double T ;
T = *A ; *A = *B ; *B = T ;
 void SelectionSort ( double List[ ], int N ) {
return ;

int J, K, P ;
}
Sorting Algorithms - Selection







 }
double Temp ;
for( J = 0 ; J < N-1 ; J++ ) {
P=0;
for( K = 0 ; K < N - J ; K++ )
if( List[P] < List[K] ) P = K ;
Temp = List[P] ;
dswap
&List[N-1-J]
);
List[P] (=&List[P],
List[N-1-J]
;
List[N-1-J] = Temp ;
}
Sorting Algorithms - Selection
 void SelectionSort ( double List[ ], int N ) {







 }
int J, K, P ;
How many operations must be
double Temp ;
performed?
for( J = 0 ; J < N-1 ; J++ ) {
Sum from J = 0 to N-2
P=0;
Sum from K = 0 to N-1-J
for( K = 0 ; K < N - J ; K++ ) Core loop logic (CoreOps)
if( List[P] < List[K] ) P Gauss
= K ; dealt with this problem
and developed several
Temp = List[P] ;
formulae which bear his name.
List[P] = List[N-1-J] ;
List[N-1-J] = Temp ;
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
 One-dimensional arrays are used in many fields of
science, Recommendation
mathematics, engineering
for Learningand almost any
subject
for which
computational
simulation is involved
Some
students
experience difficulties
learning
mathematical
concepts, from
 Computer
Graphics
and Gaming
algebra to statistics.
Programming the techniques will
 Financial
modeling
greatly
improve understanding and
help accounting
to cement the concepts with a
 Business
foundation based on application and
 In all cases it is important to understand the role that
practice.
 Physics and Chemistry
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.
Example usage for calling:
Recursive Binary Search
P = BinSearch( VS, V, 0, N-1 ) ;
if( P ==Search
-1 )
 Binary
can be expressed very elegantly using
printf( “Value %lf not found”, VS ) ;
recursion
else
printf( “Value %lf found at position %d\n”, VS, P ) ;
 int BinSearch ( float VS, float V[ ], int Lo, int Hi ) {






 }
int Mid ;
if( Lo > Hi ) return -1 ;
Mid = ( Lo + Hi ) / 2 ;
if( VS == V[Mid] ) return Mid ;
if( VS < V[Mid] ) return BinSearch( VS, V, Lo, Mid-1 );
else return BinSearch( VS, V, Mid+1, 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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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.
A
B
C
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 !!

A
B
C
Towers of Hanoi
 Base Case 1 :: Move plate from A to B
A
B
C
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 */
A
B
C
Towers of Hanoi
 Base Case 2 ::
A
B
C
Towers of Hanoi
 Base Case 2 ::
Move top plate from A to C
A
B
C
Towers of Hanoi
 Base Case 2 ::
Move top plate from A to C
Move bottom plate from A to B
A
B
C
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, An
B, intriguing
C, 2 ) ; /*idea
Move
from
A to B,
starts
to emerge
.... but use C */
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 )
A
B
C
Towers of Hanoi
 Base Case 3 ::
Move top plate from A to B
Move top plate from B to C
Move top plate from C to A
Move top plate from A to B
A
Move middle plate from A to C
Move bottom plate from A to B
Move middle plate from C to B
B
C
Towers of Hanoi
 Base Case 3 ::
Move top plate from A to B
Move top plate from B to C
Move top plate from C to A
Move top plate from A to B
A
Move middle plate from A to C
Move bottom plate from A to B
Move middle plate from C to B
B
C
Towers of Hanoi
 Base
3 ::be getting complicated, until we realize that
This Case
seems to
Hanoifrom
( A, B,
C, B
3 ) can
be expressed
as from
:
Move top plate
A to
Move
middle plate
A to C
Move top plate from B to C Move bottom plate from A to B
C, B, 2
);
Move top plate from Hanoi
C to A( A,Move
middle
plate from C to B
Hanoi ( &A[2], &B[2], C, 1 ) ;
Move top plate from A to B
Hanoi ( C, B, A, 2 ) ;
A
B
C
In general, applying mathematical induction, we find that
Hanoi ( A, B, C, N ) can be expressed as :
Towers of Hanoi
void Hanoi ( int A[ ], int B[ ], int C[ ], int N ) {
 Problem: Move
all1plates
to either
if( N ==
) { B[0] from
= A[0] A
; return
; } B or
if( Ntimes
== 2 ) {smaller
C[0] = A[0]
;
such that at all
plates
are on top
A[0] = B[0] ; C[0] = B[0] ; return ; }
larger plates.Hanoi ( A, C, B, N-1 ) ;
Hanoi ( &A[N-1], &B[N-1], C, 1 ) ;
Hanoi ( C, B, A, N-1 ) ;
return ;
}
A
B
C
C,
of
Most people are able to grasp the sequence of movements to
solve the Towers of Hanoi problem.
Towers of Hanoi
 Problem:
all plates
from
to eitherbase
B orcases
C, of
The
solution isMove
recursive,
built up
fromAhandling
such
that
at all times
plates
are on topit of
N=1,
2 and
3 plates.
Once smaller
the pattern
is understood,
is then
larger plates. reapplied to arbitrary N.

DONEis much
!! harder, however.
Programming
an iterative!!algorithm
( Try it – but in your spare time :)
A
B
C
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
 Don’t 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