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

  • Home
  • SEARCH
  • 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 8779749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:52:33+00:00 2026-06-13T19:52:33+00:00

I have an array of elements. This array could be: Randomly shuffled (about 20%

  • 0

I have an array of elements. This array could be:

  • Randomly shuffled (about 20% of the time)
  • Nearly sorted* in ascending order (about 40% of the time)
  • Nearly sorted in descending order (about 40% of the time)

But I do not know (in advance) which of these cases applies. I would prefer to sort the array into the order which it is already close to.

It does not matter whether the output is ascending or descending, but it must be one or the other (so I can perform a binary search on it.)

The sort need not be stable.


Some background info: The process goes roughly like this:

  • Populate the array
  • Sort on some attribute A
  • Do some processing (compute quantiles, and some other minor stuff)
  • Sort on some other attribute B
  • Do more processing
  • Sort on attribute C
  • Do more processing

A and B are often correlated with each other (but may be positively or negatively.) Same applies to B and C. Occasionally A == C.

* “nearly sorted” here means most elements are close to their final positions. But rarely exactly at their final positions (there is a lot of additive noise, and not many long sorted subsequences.) Still, there are usually a few “outliers” at the start and end of the array which are poor predictors of the order for the next sort. 


Is there an algorithm that can advantage of the fact that I have no preference for ascending vs. descending, to sort more cheaply (compared to the TimSort I am currently using?)

  • 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-13T19:52:34+00:00Added an answer on June 13, 2026 at 7:52 pm

    I’d continue using Timsort (however, a good alternative is Smoothsort*), but first probe the array to decide whether to sort in ascending or descending order. Look at the first and last elements and sort accordingly. If the array is unsorted, the choice is immaterial; if it is (partially) sorted, probing at a wide interval is more likely to correctly detect which way.

    *Smoothsort has the same best, average, and worst case time as Timsort, and better space complexity. Like Timsort, it was specifically designed to take advantage of partially sorted data.

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

Sidebar

Related Questions

I have this routine which alters all elements within an array... for (int i
I have an array, that represents an inventory, with nearly 50 elements (items: some
how i can duplicate one element from array: for example, i have this array:
I have a Zend_Form with file element like this: ->addElement('file', 'image', array( 'required' =>
I have an array of elements in Ruby [2,4,6,3,8] I need to remove elements
Consider I have an array of elements out of which I want to create
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I have an array with 16 elements. I would like to evaluate these to
An array is defined of assumed elements like I have array like String[] strArray
I have array of strings, String[] data and it's 10 elements has value P

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.