Outline lecture n n Revise arrays Entering into

  • Slides: 26
Download presentation
Outline lecture n n Revise arrays Entering into an array Totalling values in an

Outline lecture n n Revise arrays Entering into an array Totalling values in an array Sequential search

What is an Array n n We are interested in one-dimensional arrays An array

What is an Array n n We are interested in one-dimensional arrays An array has a fixed number of elements and all elements are of the same type Each box has an index which in java and C++ starts with 0 Here is an array which holds the ages of 10 people 0 1 32 34 2 3 56 32 4 12 5 67 6 21 7 34 8 9 21 45

0 1 32 34 2 3 56 32 4 12 5 67 6 21

0 1 32 34 2 3 56 32 4 12 5 67 6 21 7 34 8 9 21 45 n Lets look at this array in detail What do you notice about the array ? n Is the data organised in any particular way? n

Examples of Arrays n n n Draw an array of 20 elements which contains

Examples of Arrays n n n Draw an array of 20 elements which contains student marks – what type will it be ? Draw an array of 15 elements which contains student grades ranging form A-E – what type will it be?

Entering data into an array n n When you enter data into an array

Entering data into an array n n When you enter data into an array we use a loop What type of loop do you think we will use? Hint – we know the number of elements there are in the array Use a counter to keep track of how many elements are entered into the array

Read Entering data into an array n Algorithm n Loop : R = 1

Read Entering data into an array n Algorithm n Loop : R = 1 to 10 Enter A( R) Loop end n n n n The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location The computer can find a particular element in the array by using the reference number index represented by R R = loop counter A(R ) = element R in the A array R 1 10 2 1 Enter A(R ) R B

n n n n Accumulating the elements of. Calcarray You may need to sum

n n n n Accumulating the elements of. Calcarray You may need to sum the elements of an array Initialise the sum variable which contains the total to zero The instruction Sum = Sum + A(R ) tells the computer to add the valve of element A(R ) to the old valve of the sum (Sum) and to store the result into sum memory location (Sum) Algorithm Loop : R = 1 to 10 sum = Sum + A( R) Loop end n The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location Sum = Sum of the elements of A n A(R ) = element R in the A array n n R 1 5 2 1 Sum = Sum + A(R ) R B

Why Search ? n n n Everyday life -We always Looking for something –

Why Search ? n n n Everyday life -We always Looking for something – builder yellow pages, universities, hairdressers Computers can search World wide web – Spreadsheet – Databases – Large records – 1000 s takes time - many comparison slow system – user wont wait long time

Search Algorithms n n n Different types – Sequential Search, Binary Search Discuss -

Search Algorithms n n n Different types – Sequential Search, Binary Search Discuss - search algorithms - analyze them Analysis of the algorithms enables programmers to decide which algorithm to use for a specific application

Some observations – Key n n n each item in a data set -

Some observations – Key n n n each item in a data set - special member that uniquely identifies the item in the data set For e. g University - a data set which contains student records – student ID uniquely identifies each student This unique member of the item is called Key of the item

Some observations - Key n n n Where can Keys of the item in

Some observations - Key n n n Where can Keys of the item in a data set be used in ? When searching the data set - for a particular item, what do we do ? compare the key of the item for which we are searching - with the keys of the items in the data set – e. g if we looking particular student id in a list of student id exam results

Analysis of Algorithms n n - In addition to describing the algorithms – analyse

Analysis of Algorithms n n - In addition to describing the algorithms – analyse them What does analysis of algorithms involve ? key comparisons Moreover – the number of key comparisons refers to - the number of times the key of the item (in search & sort algorithms) is compared with the keys of the items in the list

Target ? n ?

Target ? n ?

Sequential search (linear search) n n Problem : - we want to search for

Sequential search (linear search) n n Problem : - we want to search for a given element in a list. Do we know where the target element occurs? .

Sequential search (linear search) n n We can have no guarantees about the order

Sequential search (linear search) n n We can have no guarantees about the order of elements in the list if (for example) insertions have been under a user’s control. Search starts at the first element in the list and continues until either the item is found in the list - or entire list is searched

A simple search method is as follows: p 193 Algorithm 1. R=1 2. While

A simple search method is as follows: p 193 Algorithm 1. R=1 2. While Array(R ) <> Target R=R+1 While. End 3. If R > N Then Print “Element Not Found” Else Print Array (R )

Flowchart for Sequential Search P 193 R=1 While Target Array(R ) and R<=N R=

Flowchart for Sequential Search P 193 R=1 While Target Array(R ) and R<=N R= R+1 F Print Array(R ) If R>N T Print Element Not Found

Flowchart for Sequential Search using for loop J 1 10 2 1 If target

Flowchart for Sequential Search using for loop J 1 10 2 1 If target not found J= J+1 F Print Array(R ) If Target found T Print Element Not Found

Task : - in pairs n n Draw an array with 10 integer values

Task : - in pairs n n Draw an array with 10 integer values Populate the array with 10 elements 1563789523 Write an algorithm using the sequential search technique to find the target 6 in the array Draw a flowchart

Demonstration n http: //www. cosc. canterbury. ac. nz/peopl e/mukundan/dsal/LSearch. html The more comparisons he

Demonstration n http: //www. cosc. canterbury. ac. nz/peopl e/mukundan/dsal/LSearch. html The more comparisons he longer it takes – time!

Sequential search (linear search) n n If the search item is found, its index

Sequential search (linear search) n n If the search item is found, its index (that is its location in array) is returned. If search is unsuccessful, -1 is returned Note the sequential search does not require the list elements to be in any particular order

Sequential Search Analysis : Task – in pairs n n For each iteration in

Sequential Search Analysis : Task – in pairs n n For each iteration in the loop, the search item is compared with an element in the list, and a few other statements are executed. Loop terminates when search item is found in list Therefore the execution of the other statement in loop is directly related to the outcome of the key comparisons

Sequential Search Analysis n n n When analysing a search algorithm, - count the

Sequential Search Analysis n n n When analysing a search algorithm, - count the number of key comparisons –why ? because this number gives us the most useful information, this criterion for counting the number of key comparisons can be applied equally well to other search algorithms

Task : - in pairs – how many comparisons ? An iterative sequential search

Task : - in pairs – how many comparisons ? An iterative sequential search of an array that {a) finds its target; (b) does not find its target (a) A search for 8 Look at 9 (b) A search for 6 Look at 9 9 5 8 4 7 8 <> 9 , so continue searching … Look at 5 9 5 8 4 7 6 <> 9 , so continue searching … Look at 5 9 5 8 4 7

Sequential Search Analysis n n Suppose the search item is in the list Then

Sequential Search Analysis n n Suppose the search item is in the list Then number of key comparisons depends on where in the list the search item is located. If search item is first element of L – make one key comparison – best case Worst case search item is the last item – algorithm makes n comparisons

Sequential Search Analysis n n n If the search item Target , is the

Sequential Search Analysis n n n If the search item Target , is the first element in list, how many one comparisons are needed? if target is second, how many one comparisons are needed? So if the target is the kth element in the list k comparisons are made