What do you use for sorting?

SGDK only sub forum

Moderator: Stef

astrofra
Interested
Posts: 28
Joined: Sun Dec 14, 2014 8:50 am
Location: Orleans | France
Contact:

Re: What do you use for sorting?

Post by astrofra » Wed Nov 16, 2016 8:01 pm

Here is the Quicksort I'm using on my vectorball part of "Malabars Bumper" :

Code: Select all

#include "genesis.h"

// Sorting generic structure
struct  QSORT_ENTRY
{
    fix16   value;
    u16    index;
};

static struct QSORT_ENTRY t;

//------------------------------------------------------
inline void    QSwap (struct QSORT_ENTRY *a, struct QSORT_ENTRY *b)
//------------------------------------------------------
{   
    // struct QSORT_ENTRY t = *a;
    t = *a;
    *a = *b;
    *b = t;
}

//----------------------------------------
// http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#C
inline void    QuickSort (u16 n, struct QSORT_ENTRY *a)
//----------------------------------------
{
    short i, j, p;
    if (n < 2)
        return;
    p = a[n >> 1].value;
    for (i = 0, j = n - 1;; i++, j--) {
        while (a[i].value < p)
            i++;
        while (p < a[j].value)
            j--;
        if (i >= j)
            break;

        QSwap(&a[i], &a[j]);
    }
    QuickSort(i, a);
    QuickSort(n - i, a + i);
}
640 polygons are enough for everyone.

Post Reply