Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7884915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:55:39+00:00 2026-06-03T04:55:39+00:00

I think typically, insertion to the middle (any where) of an array can be

  • 0

I think typically, insertion to the middle (any where) of an array can be costly — it can take O(n) time to shift the elements over one slot, and possibly reallocation of memory for the array. On the other hand, a linked link can take O(1) time.

But in Ruby, Python, and PHP, is there an array object that only takes O(1) time to insert an element in the middle of the array?

update: And it has to follow that the replacement of the array element any where in the array or accessing it is also O(1).

update: insertion as in the following:

1.9.3-p125 :001 > RUBY_DESCRIPTION
 => "ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]" 

1.9.3-p125 :002 > a = %w{ a b c d }
 => ["a", "b", "c", "d"] 

1.9.3-p125 :007 > a.insert(3, 123)
 => ["a", "b", "c", 123, "d"] 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:55:40+00:00Added an answer on June 3, 2026 at 4:55 am

    In PHP, you have \SplDoublyLinkedList that can do this for you. http://php.net/manual/en/class.spldoublylinkedlist.php

    Note that PHP arrays are not arrays that you see in C, C#, Java, etc – they are ordered hash maps. See the intro of http://www.php.net/manual/en/language.types.array.php

    Depending on your use case (if this isn’t homework), \SplMaxHeap isn’t O(1) but may be your optimal choice. http://php.net/manual/en/class.splmaxheap.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The use case I can think of is the following: In a typical application,
I think I'm burnt out, and that's why I can't see an obvious mistake.
Our company develops a web application that other companies can license. Typically, our application
As the function accepted by for_each take only one parameter (the element of the
I think this is one of those vi vs. emacs type of questions, but
We are working on designing an application that is typically OLTP (think: purchasing system).
Here's a brain-twister for the SQL guys - can anyone think of a reason
In my game a user can load custom scripts. Typically there are 50-100 scripts
My app is a workout scheduler. The client typically does workouts at one of
I think I am missing something about the sphinx extension for doctest. The typical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.