4

Do in place and space complexity O(1) mean different things? If yes, can someone explain the difference?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
ion20
  • 637
  • 4
  • 9
  • 21

2 Answers2

5

Space complexity of O(1) is a stronger requirement than in-place, because O(1) implies that the changes are done in place, but not the other way around.

You can make an in-place algorithm that has a space complexity above O(1). For example, the recursive re-heapifying algorithm of Heapsort is in-place, but its recursive implementation without tail call optimization has an O(log N) space complexity.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

I think, Sergey's explanation is a bit confusing.

The terminology "in-place" is used for algorithms which doesn't require additional Data Structures to transform the input. However, to be considered as in-place algorithm, it must have maximum of O(log(N)) space complexity for additional pointers, pivots, stacks pointers. Any algorithm which requires more than O(log(N)) is not considered in-place algorithm

So, O(1) doesn't always mean it is in-place algorithm. For example, think of a method that returns random element from an array. You generate a random number with the index boundary of the input array, which requires O(1) space. And just return the matching value under the random index. But you are not transforming the input array. So, this is not in-place algorithm.

codebusta
  • 1,922
  • 1
  • 14
  • 15