In an in-place function, the changes made by the function remain after the call completes. In-place algorithms are sometimes called destructive, since the original input is modified during the function call.
"In-place" does not mean "without creating any additional variables!" Rather, it means "without creating a new copy of the input." In general, an in-place function will only create additional variables that are O(1) space.
An out-of-place function doesn't make any changes that are visible to other functions. Usually, those functions copy any data structures or objects before manipulating and changing them.
Here are two functions that do the same operation on an array, except one is in-place and the other is out-of-place:
function squareArrayInPlace(intArray) {
intArray.forEach((int, index) => {
intArray[index] *= int;
});
// NOTE: no need to return anything - we modified intArray in place
}
function squareArrayOutOfPlace(intArray) {
// We allocate a new array that we'll fill in with the new values
const squaredArray = [];
intArray.forEach((int, index) => {
squaredArray[index] = Math.pow(int, 2);
});
return squaredArray;
}
Working in-place is a good way to save time and space. An in-place algorithm avoids the cost of initializing or copying data structures, and it usually has an O(1) space cost.
But be careful: an in-place algorithm can cause side effects. Your input is altered, which can affect code outside of your function.
Generally, out-of-place algorithms are considered safer because they avoid side effects. You should only use an in-place algorithm if you're space constrained or you're positive you don't need the original input anymore, even for debugging.
0 comments:
Post a Comment