Thursday, December 20, 2018

In Place Algorithm

In place algorithms:
  • focus on space efficiency
  • uses no auxiliary data structures and uses very small extra space
  • have O(1) space complexity (strict definition) to O(log n) space complexity (broad definition)
Side Effects:
  • usually overwrites the original data to create the modified output
  • may need more time in some cases
Examples:
Reversing a sorted list in-place.
a = [1,2,3,4,5]
n = len(a)
temp = 0
for i in range(n/2):
     temp = a[n-1-i]
    a[n-1-i] = a[i]
    a[i] = temp

In-place sorting algorithms:

No comments:

Post a Comment