DEV Community

Cover image for Brute Force Algorithm in DSA: Best Case and Worst Case Explained with JavaScript.
Sudhanshu Gaikwad
Sudhanshu Gaikwad

Posted on

Brute Force Algorithm in DSA: Best Case and Worst Case Explained with JavaScript.

If you are learning Data Structures and Algorithms (DSA), you will often hear the term Brute Force.
But what does a brute force algorithm actually mean?
In this article, we will understand Brute Force in DSA, along with its Best Case, Worst Case, and Time Complexity, using a simple JavaScript example.


What Is a Brute Force Algorithm?

A Brute Force algorithm solves a problem by trying every possible option until it finds the correct answer. It is usually the simplest approach to solving a problem.
For example, imagine you have a list of numbers and want to find a specific number.

[10, 25, 7, 40, 15]

If you want to find 40, a brute force approach checks the numbers one by one: 10 → 25 → 7 → 40. Once it finds 40, it stops.

This approach is also called Linear Search.


Simple Brute Force Example in JavaScript

Let's write a simple function to search for a number in an array.

function findNumber(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }

  return -1;
}
const numbers = [10, 25, 7, 40, 15];
console.log(findNumber(numbers, 40));
Enter fullscreen mode Exit fullscreen mode

Output: 3
The output is 3 because 40 is located at index 3.

How does it work?


What is Best Case and Worst Case?

In algorithms, we measure how the running time changes based on the input. Two important terms are:

  1. Best Case O(1): The situation where the algorithm does the least amount of work. It finishes as quickly as possible.
const numbers = [40, 10, 25, 7, 15];
console.log(findNumber(numbers, 40)); // Found in 1 comparison → O(1)
Enter fullscreen mode Exit fullscreen mode
  1. Worst Case O(n): The situation where the algorithm does the most amount of work. It takes the longest time.
const numbers = [10, 25, 7, 15, 40];
console.log(findNumber(numbers, 40)); // Checks all 5 elements → O(n)
console.log(findNumber(numbers, 99)); // Checks all 5 elements → O(n)
Enter fullscreen mode Exit fullscreen mode

Simple Real-Life Example
Imagine you are looking for a friend’s name in a list of 5 names written on paper:

List: ["Sudhanshu", "Priya", "Rahul", "Sneha", "Vikram"]

Best Case: You are looking for “Sudhanshu”. It is the first name, so you find it immediately. → Only 1 check needed → O(1).

Worst Case: You are looking for “Vikram” (last name) or a name that is not in the list (like “Neha”). → You have to check every name until the end → O(n)

This is exactly how brute force algorithms behave — sometimes they are lucky (best case), and sometimes they have to check almost everything (worst case).


Space complexity for this approach is O(1) because we only use a few variables.


When Should You Use Brute Force?

  • The input size is small.
  • You need a correct solution quickly, and performance is not critical yet.
  • You want a clear baseline before optimizing with better algorithms (binary search, hashing, etc.).

Brute force is rarely the final answer for large datasets, but it is almost always the first solution you should write. Once it works, you can improve it.

Top comments (1)

Collapse
 
sudhanshudevelopers profile image
Sudhanshu Gaikwad
// Brute Force approach to find a number in an array
// It checks every element one by one from the start
function findNumber(arr, target) {
  // Loop through each element in the array
  for (let i = 0; i < arr.length; i++) {
    // If the current element matches the target, return its index
    if (arr[i] === target) {
      return i;
    }
  }

  // If the loop finishes without finding the target, return -1
  return -1;
}

const numbers = [10, 25, 7, 40, 15];

// Searching for 40
console.log(findNumber(numbers, 40)); // Output: 3
Enter fullscreen mode Exit fullscreen mode