If you are looking with the Linear Search Algorithm assignment or if you want the help in your Linear Search Algorithm project with to the point Algorithm assignment solution or if you have very little time to complete the assignment, here our expert will help you with it. We can do it for you and better than you expected.
Our Linear Search Algorithm Assignment Help are expert in coding and we can complete your assignment at your terms and conditions.
We provide the help for Linear Search Algorithm Assignment for the students of school, middle high school, Senior High School, college and undergraduate level. Our expert penal is full of Algorithm teachers, professors from all major countries around the world. Get speedy and cost effective solutions to your assignment.
1) It is a simple search algorithm that search a target value in an array or list.
1: [ Initialize] Set j := 1, l =0 //here l= location 2: Repeat Step 3 and 4 3: while l = 0 and j <= m // j greater equal to m 4: If i = data[ j ], // item = i 5: then, Set l = j 6: Set j = j + 1 // end step2 7: if l = 0, then: 8: item(i) not found 9: else 10: location(l) of item 11: exit |
Public static int linearSearch( Object[] array, Object key) { for (int j = 0; j < array.length; j++) if (array[ j ].equals( key )) return j; return -1; } |
Question: the array size of the element is 9. Find the search 20.
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Solution: The given element of the arry size is 9.
Step1: number to search == arr[ 0 ] , we found arr[0]= 12, then 20 == 12?
So we got the 20 not equal to 12, so check next element
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Step2: number to search == arr[ 1 ] , we found arr[ 1 ]= 5, then 20 == 5?
So we got the 20 not equal to 5, so check next element
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Step3: number to search == arr[ 2 ] , we found arr[ 2 ]= 10, then 20 == 10?
So we got the 20 not equal to 10, so check next element
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Step4: number to search == arr[ 3 ] , we found arr[ 3 ]= 15, then 20 == 15?
So we got the 20 not equal to 15, so check next element
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Step5: number to search == arr[ 4 ] , we found arr[ 4 ]= 15, then 20 == 31?
So we got the 20 not equal to 31, so check next element
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Step6: number to search == arr[ 5 ] , we found arr[ 5 ] = 20, yes return true
So we got the 20 equal to 20, so it is true
12 | 5 | 10 | 15 | 31 | 20 | 25 | 2 | 40 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |