answersLogoWhite

0


Best Answer

// This method will search through nums for target.

// It will return the index of target in nums, or -1 if target is not in nums.

public static int search(final int target, final int[] nums) {

// Linear search means start at one end and search element-by-element

for(int i = 0; i < nums.length; ++i) {

if(nums[i] == target) {

return i;

}

}

return -1;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: WAP in java to search for a given number using linear search?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the Advantages of binary search on linear search in c?

(i) Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature. For in-memory searching, if the interval to be searching is small, a linear search may have superior performance simply because it exhibits better locality of reference. (ii) Binary search algorithm employs recursive approach and this approach requires more stack space. (iii) Programming binary search algorithm is very difficult and error prone (Kruse, 1999).


Which is faster binary tree or binary search tree?

A tree doesn't do anything so it has no speed...


Flowchart for linear search?

PROGRAM://LINEAR SEARCH USING FUNCTIONS#includevoid linear(int [],int,int);main(){int n,i,x,a[10];printf("\nEnter the limit:");scanf("%d",&n);printf("\nEnter the elements:");for(i=0;i


Constant current generation of linear sweep voltage using op-amp?

When using the Op-amp we get constant current generation of the linear sweep voltage.


What condition linear search is better than binary search?

In linear search, the searched key will be compared with each element of the array from the beginning and terminate comparing when the searched key is found or the array is reached. Here time complexity in worst case and average case is O (n). To find an element quickly we use divide and conquer method by using binary search algorithm. Here probed region is reduced from n to n/2. Time complexity is O (log2 n), but here the array should be sorted. But in interpolation search the probed region is reduced from n to n1/2. If the array elements are uniformly distributed the average case complexity is O (log2 (log2n)). Am also searching for hashing to compare &amp; contrast with above.

Related questions

How many checks on average would it take to search a list of 1000000000 items using a linear search?

It would take a long time


How do you run graphics program in C?

pro c language to implement linear search using pointers


What are the Advantages of binary search on linear search in c?

(i) Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature. For in-memory searching, if the interval to be searching is small, a linear search may have superior performance simply because it exhibits better locality of reference. (ii) Binary search algorithm employs recursive approach and this approach requires more stack space. (iii) Programming binary search algorithm is very difficult and error prone (Kruse, 1999).


Linear prediction rule?

Linear prediction is a mathematical operation on future values of an estimated discrete time signal. Its rule is to predict the output by using the given inputs.


Which is faster binary tree or binary search tree?

A tree doesn't do anything so it has no speed...


What are linear algebraic expressions using fractional coefficients?

Rational linear expressions.


How do you find the number of words can be formed using the given letters?

cookies


How do you find missing number if the mean is given using percentages?

Convert it.


What benefit can using a reverse search phone number have?

When using a reverse search phone number the person you are calling can tell who you are without having to look you up manually, this will increase the chances of calls from reputable companies being answered.


How does an individual search engine like google or yahoo different from a meta search engine like google custom search engine or surfwax?

I think Google Custom Search doesn't act as new pages in cache. It's like someone piggy backing off a given technology without affecting the source at all. In fact there are a number of people using Google custom search to pretend to create their own search engine.


Is there an equate antibacterial hand soap MSDS?

Since I had issues finding the MSDS using the MSDS search tool and had no help with the phone number I was given, here is a more direct answer: http://msds.walmartstores.com/cache/121589.pdf


Find out the square root for that given number using string?

Halv that given number,Make a square that has that half number as the length of a side.Stretch the string across the diagonal of the square from corner to corner.The length of the string is the square root of the given number