qsort.c 856 B

12345678910111213141516171819202122
  1. #include <stdlib.h>
  2. // Do not use register a5; callback function might need it.
  3. register long __tbl asm ("a5");
  4. __ATTR_LIB_C__ void qsort(void *list, short num_items, short size, compare_t cmp_func)
  5. {
  6. unsigned short gap,byte_gap,i,j;
  7. char *p,*a,*b,temp;
  8. for (gap=((unsigned short)num_items)>>1; gap>0; gap>>=1) // Yes, this is not a quicksort,
  9. { // but works fast enough...
  10. byte_gap=gap*(unsigned short)size;
  11. for(i=byte_gap; i<((unsigned short)num_items)*(unsigned short)size; i+=size)
  12. for(p=(char*)list+i-byte_gap; p>=(char*)list; p-= byte_gap)
  13. {
  14. a=p; b=p+byte_gap;
  15. if(cmp_func(a,b)<=0) break;
  16. for(j=size;j;j--)
  17. temp=*a, *a++=*b, *b++=temp;
  18. }
  19. }
  20. }