The next example is an algorithm that collects every combination of two-character strings built from an array of single characters. For example, given the array: ["a", "b", "c", "d"] , we’d return a new array containing the following string combinations:
| | [ |
| | 'ab' , 'ac' , 'ad' , 'ba' , 'bc' , 'bd' , |
| | 'ca' , 'cb' , 'cd' , 'da' , 'db' , 'dc' |
| | ] |
Following is a JavaScript implementation of this algorithm. Let’s see if we can figure out its Big O efficiency:
| | function wordBuilder(array) { |
| | let collection = []; |
| | |
| | for ( let i = 0; i < array.length; i++) { |
| | for ( let j = 0; j < array.length; j++) { |
| | if (i !== j) { |
| | collection.push(array[i] + array[j]); |
| | } |
| | } |
| | } |
| | |
| | return collection; |
| | } |
Here we’re running one loop nested inside another. The outer loop iterates over each character in the array, keeping track of the index i . For each index i , we run an inner loop that iterates again over each character in the same array using the index j . Within this inner loop, we concatenate the characters at i and j , with the exception of when i and j are pointing to the same index.
To determine the efficiency of our algorithm, we once again need to determine what the N data elements are. In our case, as in the previous example, N is the number of items inside the array passed to the function.
The next step is to determine the number of steps our algorithm takes relative to the N data elements. In our case, the outer loop iterates over all N elements, and for each element, the inner loop iterates again over all N elements, which amounts to N steps multiplied by N steps. This is the classic case of O(N 2 ), and is often what nested-loop algorithms turn out to be.
Now, what would happen if we modified our algorithm to compute each combination of three-character strings? That is, for our example array of ["a", "b", "c", "d"] , our function would return the array:
| | [ |
| | 'abc' , 'abd' , 'acb' , |
| | 'acd' , 'adb' , 'adc' , |
| | 'bac' , 'bad' , 'bca' , |
| | 'bcd' , 'bda' , 'bdc' , |
| | 'cab' , 'cad' , 'cba' , |
| | 'cbd' , 'cda' , 'cdb' , |
| | 'dab' , 'dac' , 'dba' , |
| | 'dbc' , 'dca' , 'dcb' |
| | ] |
Here’s an implementation that uses three nested loops. What is its time complexity?
| | function wordBuilder(array) { |
| | let collection = []; |
| | |
| | for ( let i = 0; i < array.length; i++) { |
| | for ( let j = 0; j < array.length; j++) { |
| | for ( let k = 0; k < array.length; k++) { |
| | if (i !== j && j !== k && i !== k) { |
| | collection.push(array[i] + array[j] + array[k]); |
| | } |
| | } |
| | } |
| | } |
| | |
| | return collection; |
| | } |
In this algorithm, for N data elements, we have N steps of the i loop multiplied by the N steps of the j loop multiplied by the N steps of the k loop. This is N * N * N, which is N 3 steps, which is described as O(N 3 ).
If we had four or five nested loops, we’d have algorithms that are O(N 4 ) and O(N 5 ), respectively. Let’s see how these all appear on a graph .
Optimizing code from a speed of O(N 3 ) to O(N 2 ) would be a big win, since the code becomes exponentially faster.