algoritmauzmanı
Sign in
TREN
2.1Array Operations
Lesson 2.1.9

Searching for a value

Searching an array for a value means looking at the elements one by one from the start. If the value is found, its index is returned; if not, -1. This method is called linear search; it works whether or not the array is sorted.

What you will learn
  • How linear search works, and why it returns -1 for a value it cannot find
  • Why the search must stop at size, not at CAPACITY
  • Why searching is , and how a sorted array can make it faster
4 minTime O(n)Memory O(1)C · Java · Python
Know these first:2.1.2 Traversing an array

1Idea

i starts at 0 and compares each element with the value you are looking for. The search ends at the first equal element and returns i. If the loop goes through the whole filled part without a match, the value is not in the array. Valid are 0 or greater, so -1 signals this case without any confusion.

The loop stops at size. The cells between size and CAPACITY may hold old, deleted values (remember deleting from the end). A search that looks there finds a value that has already left the array.

If the same value appears more than once, the function returns the index of the first one. If you want all of them, do not stop early; record every match instead.

Index access and search by value

An reaches the element at a given index in one step: the address of arr[3] is computed, so it is . In a linked list, reaching node 3 means following three links from the start. When searching by value, both structures are in the same position: to find a value whose index you do not know, you look at the elements one by one.

If the array is sorted, there is something better: binary search looks at the middle element and rules out half of the range at each step. In a 1000-element array it makes at most 10 comparisons; it runs in . It is covered in the algorithms part.

2Step by step

The drawing searches for 9 in an array holding 12, 7, 3, 9, 5 and 8. The numbers under the cells are indices.

01234567value = 912739586sizei
  1. Start. The value to find is 9, and size is 6.
  2. i = 0: 12 is not equal to 9. Move on to the next element.
  3. i = 1: 7 is not equal either.
  4. i = 2: 3 is not equal either.
  5. i = 3: 9 is found. The function returns 3; 5 and 8 are never looked at.

#define CAPACITY 8 int search(int arr[], int size, int value) {    for (int i = 0; i < size; i++)        if (arr[i] == value)            return i;  // found: return the index    return -1;  // not in the array}
int search(int[] arr, int size, int value) {    for (int i = 0; i < size; i++)        if (arr[i] == value)            return i;  // found: return the index    return -1;  // not in the array}
def search(arr, size, value):    for i in range(size):        if arr[i] == value:            return i  # found: return the index    return -1  # not in the array

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(n)

In the worst case (the value is at the end or missing) all n elements are checked. The best case is the first element: .

Memory
O(1)

Only the loop counter is used.

Searching a linked list is in the same way. The difference is not in searching by value but in index access: O(1) in an array, O(n) in a linked list.

4Common mistake

A common mistake is running the loop up to the capacity:

for (int i = 0; i < CAPACITY; i++)  // should be size    if (arr[i] == value)        return i;

The cells beyond size are not part of the array. They hold deleted values or random values that were never written. This loop can find a value that was already deleted and return an invalid index. The correct bound is always size.

5Try it

What does search return for 7 in the array 7, 3, 7, 5?

Show the answer

The answer is A. The loop stops at the first match. 7 is at both index 0 and index 2; the first one found is 0.

You search an unsorted 1000-element array for a value that is not in it. How many comparisons are made?

Show the answer

The answer is C. To be sure the value is missing, every element has to be checked: 1000 comparisons. 10 comparisons are only possible with binary search, on a sorted array.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.