algorithmsortingembedded

What is an appropriate sort algorithm for an embedded system?


I'm developing the software for an embedded system, and I need to implement a sorting routine, and I'm having trouble choosing an optimal solution. My requirements are as follows:

  1. Because this is a very memory-limited system, space complexity is a primary factor.
  2. Because the number of elements to sort will generally be small, and the sorting will happen only occasionally, time complexity is not necessarily a primary factor.
  3. A stable algorithm is a requirement for my application.
  4. Because this is an embedded system, code size is a factor.
  5. There is no guarantee that the data will initially be in a nearly-sorted order.

I've considered the following algorithms:

While the answer (for my exact circumstances) may very well be, "uh, duh, it doesn't really matter, use bubble sort for all we care", that answer is not very useful. In general, what sort algorithms are useful on embedded systems?


Solution

  • Insertion sort is fine too, it runs fast in practice, is stable and is in-place. It's very much related to gnome sort, faster in practice but for gnome sort the code is a bit smaller and it takes less auxiliary space (not in terms of big O, the difference is in the constant)

    edit: yea I messed up a bit and got them reversed - I probably shouldn't answer questions before I've had my coffee.. it previously said that insertion sort has less code and space than gnome sort