2.1Array Operations
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.
- How linear search works, and why it returns -1 for a value it cannot find
- Why the search must stop at
size, not atCAPACITY - Why searching is O(n), and how a sorted array can make it faster
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 indices 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 array reaches the element at a given index in one step: the address of arr[3] is computed, so it is O(1). 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 O(log n). 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.
- Start. The value to find is 9, and
sizeis 6. i = 0: 12 is not equal to 9. Move on to the next element.i = 1: 7 is not equal either.i = 2: 3 is not equal either.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 arrayThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
In the worst case (the value is at the end or missing) all n elements are checked. The best case is the first element: O(1).
Only the loop counter is used.
Searching a linked list is O(n) 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?
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.
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?
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.
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.