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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:06:43+00:00 2026-06-01T10:06:43+00:00

I am looking for a suitable algorithm to build a relatively small (up to

  • 0

I am looking for a suitable algorithm to build a relatively small (up to 255 elements) sorted array of integers. The target platform is a STM32 based embedded system. Since memory is limited, an in-place method is preferred.

I am aware that the obvious way is implement and profile the usual suspects (quick sort, insert sert, shell sort), but would like to ask for your experiences nevertheless. More specifically I have found very little information on the performance when building the array – that is, how well different algorithms may use the fact that all existing elements are already ordered.

Edit 1:
Although the question is tagged C++ the STL is not available. Furthermore, the sorting does indeed occur inside a inner loop. To further clarify, I am looking for an algorithm that is especially suited to build a sorted list in an efficient manner. I assume (maybe wrongly) there must be algorithms specially suited for this task. That’s the question.

Edit 2:
When saying building a sorted list I mean that the list starts empty and is filled by by a bounded number (max 255) of 16-bit integers which are in no particular order. The list must be processed after all elements have been stored. For processing the list must be sorted, preferably in descending order.

Thanks in advance,
Arne

  • 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-01T10:06:45+00:00Added an answer on June 1, 2026 at 10:06 am

    If your problem demands that:

    • you store your elements fully sorted in an actual C/C++ array, and
    • you maintain your items in sorted order at all times,

    then you’ve painted yourself into a corner: your requirements spell “insertion sort”.

    No matter what algorithm or auxiliary datastructure you choose, inserting a new element in the middle will require you to move the larger elements up by an index, and deleting any element (except the largest) will require you to move the larger elements down by an index. Since an insertion sort does exactly that, without any additional logic or datastructure, you might as well use it.

    The only exception is if your comparison is particularly expensive. If that is the case, then you can use a binary search to find your insertion point (instead of comparing your new element against each old element as you move it). However, you will still need to move all elements larger than the mutation point, so you will never be able to improve your performance past O(N) (although a bulk data move should be pretty fast…).

    Also, you should evaluate your requirements: if you know that N < 256, and the worst case of inserting an object in position 0 is fast enough for your application, you should stop there. There’s no point in making things more complicated than necessary, to save time you don’t need.


    On the other hand, if:

    • you don’t actually need to keep all elements fully sorted at all times, and
    • what you do actually need is to repeatedly find and remove the largest/smallest element in the array

    then what you need is called a priority queue, and you can implement it (in memory-efficient, in-place fashion) by using an implicit heap. Implicit heap operations are O(log N), and typically have a good performance factor; even for N = 255, this can make a big difference in worst-case performance.

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

Sidebar

Related Questions

I'm looking for some guidance on whether SQL Azure is a suitable platform for
I'm looking for a suitable cross-platform web framework (if that's the proper term). I
I'm looking for suitable .NET class that allows me to store a large number
I’m looking for a suitable distribution of Subversion for deployment within an enterprise. It
I am looking for a suitable package (free or otherwise) to duplicate the functionality
Can someone give a head-to-head comparison between them? We are looking for a suitable
I'm looking for a Python library that's suitable, with DOM access too. I don't
I am looking to find a why to calculate a suitable background colour and
I am looking for suitable replacement code that allows me replace the content inside
I was looking for some suitable 2D element container. What I want is the

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.