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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:31:26+00:00 2026-05-31T00:31:26+00:00

In case you are given: certain amount of data memory with size half of

  • 0

In case you are given:

  • certain amount of data
  • memory with size half of the data size
  • part of the data is sorted
  • you do not know the size of the sorted data.

Which sorting algorithm would you choose?
I am debating between insertion and quicksort. I know that the best case for insertion sort is O(n), but the worst case is O(n2). Also, considering the fact the memory is limited, I would divide the data in two parts, and on each of them do quicksort, then merge everything together. It will take O(n) time to split the data, O(n) to merge the data, and O(n log n) to sort the data using quicksort, for a net runtime of O(n log n).

Does anyone have any suggestions on how to improve this?

  • 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-05-31T00:31:27+00:00Added an answer on May 31, 2026 at 12:31 am

    Your mergesort-like approach seems very reasonable. More generally, this type of sorting algorithm is called an external sorting algorithm. These algorithms often work as you’ve described – load some subset of the data into memory, sort it, then write it back out to disk. At the end, use a merging algorithm to merge everything back together. The choice of how much to load in and what sorting algorithm to use are usually the dominant concerns. I’ll focus mostly on the sorting algorithm choice.

    Your concerns about quicksort’s worst-case behavior are generally speaking nothing to worry about, since if you choose the pivot randomly the probability that you get a really bad runtime is low. The random pivot strategy also works well even if the data is already sorted, as it has no worst-case inputs (unless someone knows your random number generator and the seed). You could also use a quicksort variant like introsort, which doesn’t have the worst-case behavior, as your sorting algorithm in order to avoid this worst-case.

    That said, since you know that the data is already partially sorted, you may want to look into an adaptive sorting algorithm for your sorting step. You’ve mentioned insertion sort for this, but there are much better adaptive algorithms out there. If memory is scarce (as you’ve described), you might want to try looking into the smoothsort algorithm, which has best-case runtime O(n), worst-case runtime O(n log n), and uses only O(1) memory. It’s not as adaptive as some other algorithms (like Python’s timsort, natural mergesort, or Cartesian tree sort), but it has lower memory usage. It’s also not as fast as a good quicksort, but if the data really is mostly sorted it can do pretty well.

    Hope this helps!

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

Sidebar

Related Questions

Given the case I made two independent changes in one file: eg. added a
Given a case where I have an object that may be in one or
Is there a CTRL+space -like way of auto-constructing a switch case around a given
Given a simple switch statement switch (int) { case 1 : { printf(1\n); break;
I need to highlight, case insensitively, given keywords in a JavaScript string. For example:
Is the order of attributes in a certain tag releveant. Specific case: This is
i'm talking to a COM object (Microsoft ADO Recordset object). In a certain case
I have small function which will recolor pixels in a Bitmap from a given
Problem description There are different categories which contain an arbitrary amount of elements .
I'm almost certain I know the answer to this question, but I'm hoping there's

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.