Worst, average, and best cases

In the previous section, we were able to define time complexity for our code using an asymptotic algorithm. In this section, we are going to determine a case of the implementation of an algorithm. There are three cases when implementing time complexity in an algorithm; they are worst, average, and best cases. Before we go through them, let's look at the following Search() function implementation:

int Search(int arr[], int arrSize, int x){    // Iterate through arr elements    for (int i = 0; i < arrSize; ++i)    {        // If x is found        // returns index of x        if (arr[i] == x)            return i;    }    // If x is not found    // returns -1    return -1;}

As we can see in the preceding Search() function, it will find an index of target element ( ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.