mardi 1 octobre 2019

What is the simplest code that you would use to represent a Log(n) BigO notation?

I would like to know a clear and simple code that can be used to teach a O(log(n)) case.

Its quite simple to see examples for O(n) and O(n²) as a for loop and a nested for loop respectively. But I can't think of a trivial and simple example for O(log(n)).

O(n):

function N(n) {
    for (i = 0; i < n; i++){
      console.log(i);
    }
}

O(n²):

function N2(n) {
  for (i = 0; i < n; i++){
    for (j = 0; j < n; j++){
      console.log(i,j);
    }
  }
}

What would be the simplest and direct form of a code that would result in a O(log(n))?

Aucun commentaire:

Enregistrer un commentaire