Question

Basically what does this return -1 do ?

for an example :

int linear[] = {4, 21, 36, 14, 66, 91, 8, 22, 7, 81, 77, 10};
int key = 77;
for (int i = 0; i < linear.length; i++){
  if (linear[i] > key)
    return -1; //here
  else if (linear[i] == key)
    return i;
}
  • i understand return 1 and return 0 well . but return -1 ?

  • what if the question sounds like this :

Show the way of solving the linear search based on the code given ?

Was it helpful?

Solution

In this case it says/indicates that linear[i] is smaller than your key.

In some occasions it might also indicate that a key is not found.

This is a widely used convention (I mean returning -1 in such cases).

OTHER TIPS

I believe this function is meant to take a sorted array and returns the index of a given key if it is found in the array and return -1 if there is no matching element in the array.

EDIT: to avoid confusion - in the example you show linear is not sorted. This means that it will not do what I describe above. If linear is not sorted, then the function will do the following:

  • if there is an element in linear such that all prior elements in linear are strictly smaller than key and it is equal to key, the function will return its index
  • Otherwise if there is an element greater than key the function will return -1.
  • Otherwise the return value is not defined in the code snippet you provide.

It's just a shorthand for when the value is not found in the sorted array. With the assumption that it's sorted, if a bigger value than the one you're searching for is met, it's useless to continue the search, so it quits and returns -1 (0 or a positive number would be ambiguous)

The code will likely not do what is expected:

Even though 77 is in the array - it will not be "found" because the 91 before it will "hide" it.

So this will return -1, if the element is not found, or a higher value preceded the key in the list

int linear[] = {4, 21, 36, 14, 66, 91, 8, 22, 7, 81, 77, 10};
int key = 77;

for (int i = 0; i < linear.length; i++){
  if (linear[i] > key)                        // when linear gets to 91 - this is true
    return -1; //here                         // this will return -1 before 77 is found
  else if (linear[i] == key)
    return i;
}

To search the whole (unsorted) array - the following code is needed:

for (int i = 0; i < linear.length; i++)
{
  if (linear[i] == key)
    return i;
}
return -1; // not found

So in a function - return will stop executing the function when the return is reached and the caller will get the value returned.

So, if this was in a function foo() and code had x=foo(); then x would be -1

Typically both the key and the array would be passed in to foo: x=foo(77, linear);

-1 as a special value is a hold-over from earlier days of programming. It's a common convention that indicates an error condition.

See the trouble with an integer return type is that it doesn't give you a way to signal that the function couldn't successfully run. So usually part of the contract of many methods is that they return a positive integer (or zero). Negative values (or specifically -1) means that something about the premise of your function call wasn't right.

For example, if you do this, you'll get -1:

System.out.println("This string doesn't contain a semicolon".indexOf(";"));

You're asking for the index of a string that isn't in what you're searching. Clearly we can't return any positive integer, so we return -1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top