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 8075349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:57:38+00:00 2026-06-05T14:57:38+00:00

I have two sorted NSMutableArrays (or I can use any other collection, not critical),

  • 0

I have two sorted NSMutableArrays (or I can use any other collection, not critical), I need to insert objects from the first array to the second and preserve sort order in the second array. What is the optimal (fastest) method to do that? I can implement all the known good algorithms, but my question is, if there is already some built-in method? If not, what is the best algorithm in my case?

  • 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-05T14:57:39+00:00Added an answer on June 5, 2026 at 2:57 pm

    The real answer would be: it depends, since you are asking: what is the fastest way of inserting objects from one array into another while preserving sort order.


    There is no built in way of inserting in the right place of a sorted array. You can achieve the same effect by just adding the two arrays together but it won’t be "the fastest way".

    What is actually faster depends on many things like: how much data does the arrays contain, what is the ratio of data in array1 vs array2 (does one array contain much more data than the other)?, etc.

    NOTE: You should probably begin with the simple solution and only optimize once you experience performance problems. Do measurements with a large data set though, to see that your solution works with whatever data your users may have.

    Inserting items from one sorted array into another sorted array

    If you want to merge the two arrays by inserting objects in the right place then normal algorithms apply. You should insert the smaller array into the bigger array and try to insert entire sorted sequences where possible instead of every item one by one.

    For best performance you should try to make a batch insert using insertObjects:atIndexes: instead of inserting the object one by one.

    You can use indexOfObject:inSortedRange:options:usingComparator: to find the index that each item should be inserted in the other array if you specify NSBinarySearchingInsertionIndex for the options. Also, the comparator you are using must be the same as the comparator that sorted the array, otherwise the result is "undefined".

    With this in mind you would do something like this

    Create mutable index
    For every ITEM in SMALLER ARRAY
        Find the index where to insert ITEM in LONGER ARRAY
        Add (the insertion location + the location in the short array) as the index in the mutable set.
    Next item.
    
    Batch insert all items.
    

    The documentation for insertObjects:atIndexes: tells you that "the corresponding location specified in indexes after earlier insertions have been made." Which in your case with two sorted array mean all items with a lower index will already have been added and thus you should add the index of the object in the short array to the value returned from indexOfObject:inSortedRange:options:usingComparator:.

    Another (probably very premature optimization) you can do is decrease the sortedRange for every item in the loop so that you don’t have to search through parts of the array that you know the item to be inserted is bigger than.

    There are probably many other optimizations that can be made. You should still measure first! Hopefully this will get you started.

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

Sidebar

Related Questions

I have two sorted arrays (can be ArrayLists, Collections, or any other data format)
I have two application that need to talk to each other. App1 needs to
Suppose we have an object that can be sorted using two or more comparison
I have two sorted arrays, Haystack and Needles . I need to iterate over
You are given two sorted integer arrays, which have some common integers. Any common
I have two properties files that are not the same and I need to
I have two lists of objects. Each list is already sorted by a property
The setup: I have two arrays which are not sorted and are not of
I need the following functionality Given two sorted lists, merge them I have this
I have this, and it works: # E. Given two lists sorted in increasing

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.