A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition
by Jay Wengrow
Chapter 4
These are the solutions to the exercises found in the section, Exercises. The solutions provided here are in Python, but you can find the solutions in JavaScript and Ruby in the code download.[6]
-
Here is the completed table:
N Elements
O(N)
O(log N)
O(N2)
100
100
About 7
10,000
2000
2000
About 11
4,000,000
-
The array would have 16 elements, since 162 is 256. (Another way of saying this is that the square root of 256 is 16.)
-
The algorithm has a time complexity of O(N2). N, in this case, is the size of the array. We have an outer loop that iterates over the array N times, and for each of those times, an inner loop iterates over the same array N times. This results in N2 steps.
-
The following version is O(N), as we only iterate through the array ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access