2

I am working with small data sets of N elements, usually with N = 8, 16, or 32 elements; all are positive 64-bit float numbers. I need to identify the smallest N/2 elements. It is not required that the smallest N/2 elements be sorted, just that I identify which ones they are from among the group of N. Each program run must perform this sort up to a billion times, so sort speed improvements could generate useful returns for me. Right now I am using a simple Quicksort that orders all N elements just to prove out my concept.

It is quite likely that my data are not randomly distributed, but I am not yet at the point where I can get a handle on that.

Eventually, the grand design is that this will find its way into the real world via an X-code implementation.

Any recommendations as to what my best sorting strategy ought to be?

Raphael
  • 73,212
  • 30
  • 182
  • 400
Richard M
  • 21
  • 1
  • 3

2 Answers2

3

Here are two ideas. It's hard to say whether they're any good - you'll have to implement them.

  1. Use (randomized or deterministic) QuickSelect in order to find the median, and then find the bottom half with a single scan.

  2. Use some hard-coded sorting network, e.g. Batcher, bitonic or pairwise. If there is some hardware support for the basic operation of sorting two memory cells, then this can be pretty fast (even though you're not using parallelism). You can also try to come up with your own sorting network that only sorts in your sense - perhaps trimming one of the ones I listed would do.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
2

You can modify your quicksort to a quickselect for the $N/2$ position.

Also, try implementing your own insertion sort. Insertion sort is REALLY fast for small arrays, and may be even faster than the quickselect.

abastion
  • 91
  • 1