Example 1: (Linear Search - Taking O(n) time to run)
Consider the following piece of code for searching an element stored in variable 'value' in
an unsorted array 'a' of size 'n'.
bool LinearSearch(int a[], int value) {
int size = sizeof(a)/ sizeof(a[0]);
for (int i = 0; i < size; i++) {
if (a[i] == value)
return true;
}
return false;
}
Since the array 'a' is unsorted, the only way to be sure that the 'value' is not in the array is to look at
every single value of the array. Hence, in worst case, the algorithm will run all the way to the end
of the array which is of length 'n'. So, the worst case running time of the above linear search
algorithm for an unsorted array of length 'n' is O(n).