Topic 02a Linear Search SOLUTIONS.pdf - Topic 2a Linear Search Solutions Copyright(c 2017 Daniel D Suthers All rights reserved These solution notes may

# Topic 02a Linear Search SOLUTIONS.pdf - Topic 2a Linear...

• Homework Help
• 3

This preview shows page 1 - 2 out of 3 pages.

Topic 2a, Linear Search: Solutions Copyright (c) 2017 Daniel D. Suthers. All rights reserved. These solution notes may only be used by students in ICS 311 Fall 2018 at the University of Hawaii. Searching Problem Input: A sequence of n numbers A = a 1 , a 2 , .... a n (in no particular order) and a value v . Output: An index i such that v = A[ i ] or the special value NIL if v does not appear in A . (2 pts) (a) Write pseudocode for linearSearch(A,n,v) , an algorithm that scans through a sequence represented as an array A of length n looking for v , and provides the desired output (index i or NIL). Use Java style 0-based indexing and A[i] to access elements. Number the lines for reference, as started below. linearSearch(A,n,v) 1 for i = 0 to n - 1 2 if A[i] == v 3 return i 4 return NIL (3 pts) (b) Let p be the number of array positions checked. ( p will be replaced with a function of n: p = f(n). For example, when the element is not present, p = n. ) Let c 1 be the cost of executing line 1, c 2 the cost of executing line 2, etc. As was done in the lecture notes:

#### You've reached the end of your free preview.

Want to read all 3 pages?

• Fall '08
• Barjaktarovic,M

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern