Array Time Complexity

Hi, I wonder what’s the average time complexity and amortized time complexity of these operations:

  • shift / unshift
  • pop / push
  • slicing (arr[range] and arr[start, length])

It’d be great if you can laso explain the reason of its complexity behind each operation. THX.

Also what’s the default size of array, and when it’s expanding its size, what’s the factor of it.

  1. Shift/Unshift are always costly because it has to rearrange the entire array elements after inserting or deleting elements from the beginning. This operation can be map with Queue(FIFO) Data structure

  2. pop/push inserts or delete the elements from last position as Stack(LIFO) Data structure

  3. slicing is to get sub array from given point to end point with the indexes. It only copies the sub array into memory.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs