26
loading...
This website collects cookies to deliver better user experience
forEach
, map
, and reduce
run through the entire collection of data, from start to finish. They are good examples of linear-time operations.numbers[i]
five times. To calculate Big-O here we have to multiply O(n) * O(n), because the execution of our log is dependent on iterating through the entirety of the second loop before we can increment i
and move to the next index in our first loop. Thus, the Big-O in this case is O(n²).reduce
function, which is linear. We get O(n² + n). However, Big-O is not concerned with non-dominant terms and because quadratic time is worse than linear time, we drop the second n. The final Big-O of the above function is O(n²).while
loop, we split our data (the array) in half and call that point a pivot. We then check to see if the value in the array at pivot equals 128. If it does, we return a statement to say where the number was located. If it’s less than 128, then we change the value of our startIndex
to the value of pivot plus one. That’s because if the value of pivot is less than 128, then we know that all of the numbers preceding pivot will also be lower than 128 (remember, the array has been sorted). If the value of pivot is greater than 128, then we change the value of our startIndex
to the value of pivot minus one. This way we are able to halve our data.false
to indicate that the number does not exist in the given array. With each pass we divide our data by two. Whenever you see this pattern of dividing the population or data by two, know that you are looking at Big-O of O(log n).26