logo
logo

Time Complexities (Good)

Lesson

Could you provide examples and an explanation of time complexities that are generally considered efficient and effective for various algorithms, despite the fact that the efficiency of a given time complexity is highly dependent on the specific use case of the algorithm?

Generally, a "good" time complexity can be categorized into four types:

  • Constant Time: O(1)

  • Linear Time: O(n)

  • Logarithmic Time: O(log n)

  • Linearithmic Time: O(n log n)

Let's explore each of these time complexities with code examples.

Constant Time: In Constant Time complexity, the time taken by an algorithm remains constant, irrespective of the size of input. For example, finding the value of an element in a map. Let's see the code example below:

function findValue(map, key) {
  return map[key];
}

In the code, we are finding the value of the key in the map, and it is a constant time operation as it only takes one step, irrespective of the size of the map.

Linear Time: In Linear Time complexity, the time taken by an algorithm increases linearly with the size of input. For example, finding the maximum value in an array. Let's see the code example below:

function findMax(array) {
  let max = array[0];
  for (let i = 1; i < array.length; i++) {
    if (array[i] > max) {
      max = array[i];
    }
  }
  return max;
}

In the code, we are iterating through the array, checking each element to find the maximum value, and it takes n steps, where n is the length of the array. Therefore, the time complexity of this algorithm is O(n).

Logarithmic Time: In Logarithmic Time complexity, the time taken by an algorithm increases logarithmically with the size of input. For example, performing binary search. Let's see the code example below:

function binarySearch(array, element) {
  let left = 0;
  let right = array.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (array[mid] === element) {
      return mid;
    } else if (array[mid] < element) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

We are performing binary search to find the index of the element in the array. In each iteration, we are splitting the array in half, and the time taken increases logarithmically with the size of the array. Therefore, the time complexity of this algorithm is O(log n).

Linearithmic Time: In Linearithmic Time complexity, the time taken by an algorithm increases logarithmically with the size of input, multiplied by the size of the input. For example, performing merge sort. Let's see the code example below:

function mergeSort(array) {
  if (array.length <= 1) {
    return array;
  }
  let middle = Math.floor(array.length / 2);
  let left = array.slice(0, middle);
  let right = array.slice(middle);
  return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right) {
  let result = [];
  while (left.length && right.length) {
    if (left[0] < right[0]) {
      result.push(left.shift());
    } else {
      result.push(right.shift());
    }
  }
  return result.concat(left, right);
}

The time complexity of merge sort is O(n log n), where n is the number of elements in the array. This time complexity is achieved by dividing the array into smaller sub-arrays, sorting them, and then merging them back together in a sorted order.

When the merge function is called, it compares the first elements of the left and right sub-arrays, and adds the smaller element to the result array. This comparison and addition takes constant time, O(1), so the total time taken to merge two sub-arrays of size n/2 is O(n).

The merge function is called log(n) times during the mergeSort function, as the input array is continually divided into smaller sub-arrays. Therefore, the total time taken by the merge function is O(n log n).

Overall, the merge sort algorithm has a time complexity of O(n log n), which makes it an efficient algorithm for sorting large arrays. However, it does have a space complexity of O(n), as it needs to create temporary arrays to store the sub-arrays during the sorting process.

Although the efficiency of a given time complexity is highly dependent on the specific use case of the algorithm, generally, four types of time complexities are considered "good": Constant Time, Linear Time, Logarithmic Time, and Linearithmic Time. These complexities are achieved by algorithms that take a constant time, increase linearly, logarithmically or linearithmically with the size of the input.

While some algorithms are more efficient than others, it is essential to consider the trade-offs between time and space complexity when selecting an algorithm for a particular use case.