0% found this document useful (0 votes)
4 views

CS 1083 Mod 2

The document outlines the learning objectives and methods for implementing a linear search algorithm, including code examples for finding both the last and first instances of a value in an array. It emphasizes the importance of handling cases where the test value is not present by using a NOT FOUND variable. Additionally, it mentions an assignment related to instance variables and constructors.

Uploaded by

rerife3137
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS 1083 Mod 2

The document outlines the learning objectives and methods for implementing a linear search algorithm, including code examples for finding both the last and first instances of a value in an array. It emphasizes the importance of handling cases where the test value is not present by using a NOT FOUND variable. Additionally, it mentions an assignment related to instance variables and constructors.

Uploaded by

rerife3137
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Linear Search-module 1

Learning Objectives : # Code linear search


called
algorithm in

search
various

Sometimes
sequential
understand algorithm and
i complexity
O"notation
big
Explain the linear search
why
algorithm is OCN)

Linear Search Example

Method 1
(Andrew's Attempts
Look for val= 5 same attempt ,
would return the
my Attempt last instance of the number .

for[intx < Arr )&


= 0; x .

length ; x++

if (Arr[x] = = test) & public Static final int NOT FOUND = -1;

ind = X; int found pos;


3 for[intx 0 ; < Arr )&
= x .

length ; x++

3 found Pos =
NOT FOUND ;

it (Arr2x] ==
test; E
foundPos =
X;

Key Notes : have a var Not Found just 3


return foundPos ;
incase test value is not present in

the Array This method returns the 3


.
last instance of the value we are

looking for .

>x x
>X
- -
xx -
Method 2 Similar to last method however ,
the first instance found is returned.

the code : forCinti itt) &


= 0 ; i < Arr
length ;
.

it (Arr[i] = =
test) E
return ;;

3
return NOTFOUND ;

while ()E
Method 3 Note This code is not
fully complete
:
yet .

if (Arr (i) = = test) &

foundPosition == i ; Let's complete this :

3 int i = 0;

return found position ; While (found Pos = = NOTFOUND Ali < Arr .

length) E
test) <
if
[Array(i) ==

foundPosition = i;

3;
i + +;
return foundPosition ;

it line goupasnumberelement C increase


Assignment 1

Part A instance variables ->


room number , StartDate ,
* Constructor Schedule
lenght ,
schedule
# to
String() method .

You might also like