np.split() for dividing arrays in NumPy - Time & Space Complexity
We want to understand how the time needed to split an array changes as the array gets bigger.
Specifically, how does dividing an array into parts affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000)
splits = np.split(arr, 10)
This code creates an array of 1000 numbers and splits it into 10 equal parts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying parts of the original array into new sub-arrays.
- How many times: The array is split into 10 parts, so this copying happens 10 times.
As the array size grows, the total amount of data copied grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copies of 1 element each = 10 operations |
| 100 | About 10 copies of 10 elements each = 100 operations |
| 1000 | About 10 copies of 100 elements each = 1000 operations |
Pattern observation: The total work grows roughly in direct proportion to the size of the input array.
Time Complexity: O(n)
This means the time to split the array grows linearly with the size of the array.
[X] Wrong: "Splitting an array into parts is very fast and does not depend on the array size."
[OK] Correct: Actually, splitting involves copying data into new arrays, so the bigger the array, the more copying is needed, which takes more time.
Understanding how array operations scale helps you explain your code choices clearly and shows you know how to handle data efficiently.
"What if we used views instead of copies when splitting? How would the time complexity change?"