Sequential Search
/* Given A[0..n] sorted in non-decreasing order, return the subscript of an occurrence of val in A (if val occurs in A) or n+1 otherwise. */
int find(int[] A, int n, int val)
{
int k = 0;
while (k <= n && val != A[k]) k++;
return k;
}
Worst case: O(n)
Best case: O(1)
Average case O(n)
Previous slide
Next slide
Back to first slide
View graphic version