Search⌘ K
AI Features

Searching Algorithms

Explore fundamental searching algorithms used in coding interviews, including linear and binary search. Understand step-by-step implementations, scenarios of use, and performance considerations to improve your algorithm problem-solving skills in C++.

Brute Force: Linear Search

This is the most simple searching algorithm and it is in O(n)O(n) time. In fact, give it a shot. You’ll probably be able to come up with it yourself!

C++
#include "AuxiliaryFunctions.h"
int linearSearch(int s, int* arr, int arrSize) {
return -1;
// Write your code here
}

How Linear Search works

Go through each element one by one. When the element that you are searching for is found, return its index. Here are some slides to make things clearer:

Implementation

C++
#include <iostream>
using namespace std;
void printArray(int* arr, int arrSize);
int findMin(int* arr, int start, int end);
int findMax(int* arr, int start, int end);

Binary Search

You must have already encountered Binary Search if you have ...