views:

128

answers:

2

What is the sort algorithm with fewest number of operations? I need to implement it in HLSL as part of a pixel shader effect v2.0 for WPF, so it needs to have a really small number of operations, considering Pixel Shader's limitations. I need to sort 9 values, specifically the current pixel and its neighbors.

+2  A: 

Knuth has done some work on finding optimal sorting algorithms. However even for just five elements the algorithm with the smallest number of comparisons is very complicated to implement.

I suggest instead of trying to find the optimal algorithm that you try to find one that is simple to implement and good enough for your needs. If you have access to a standard sorting algorithm try using that first. If not then you could use an insertion sort or merge sort to keep it simple and see if that is good enough for you.

Insertion Sort:

  • Simple implementation
  • Efficient for (quite) small data sets
  • Adaptive, i.e. efficient for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
  • More efficient in practice than most other simple quadratic, i.e. O(n2) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
  • Stable, i.e. does not change the relative order of elements with equal keys
  • In-place, i.e. only requires a constant amount O(1) of additional memory space
  • Online, i.e. can sort a list as it receives it.
Mark Byers
I have to squeeze it to the minimum possible, I've got some other stuff to do in the shader besides sorting, and all that takes up instructions too...
luvieere
@luvieere: So you mean that you need to minimize the code size, rather than the number of operations performed at run time? Then you *definitely* don't want the algorithm I linked to above. That does the opposite. Insertion sort might be perfect for you though. It's O(n^2) but since n is small that might not be an issue. It's also very easy to implement.
Mark Byers
Yes, code size in terms of the number of instructions used.
luvieere
+2  A: 

You either want to use Insertion sort or Radix sort. Here are some C++ implementations:

Radix Sort

void radix (int byte, long N, long *source, long *dest)
{
  long count[256];
  long index[256];
  memset (count, 0, sizeof (count));
  for ( int i=0; i<N; i++ )
    count[((source[i])>>(byte*8))&0xff]++;

  index[0]=0;
  for ( i=1; i<256; i++ )
    index[i]=index[i-1]+count[i-1];
  for ( i=0; i<N; i++ )
    dest[index[((source[i])>>(byte*8))&0xff]++] = source[i];
}

You need to call radix() four times, as it only works on one column.

Insertion Sort

void insertSort(int a[], int length)
{    
    int i, j, value;
    for(i = 1; i < length; i++)
    {
        value = a[i];
        for (j = i - 1; j >= 0 && a[j] > value; j--)
            a[j + 1] = a[j];
        a[j + 1] = value;
    }
}
David Titarenco