GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
#include"typedef.h"1/**************************************************************************\2@---------------------------------------------------------------------------3@---------------------------------------------------------------------------4@ FILE: quicksort.c5@---------------------------------------------------------------------------6@---------------------------------------------------------------------------7@8\**************************************************************************/9/**********************************************************************\10@ standard quicksort algorithms11@ the are called like 'quicksort(V, 0, n-1, comp)12@ where V is a pointer to n objects and comp is a function that13@ compares the objects.14\**********************************************************************/1516171819/**************************************************************************\20@---------------------------------------------------------------------------21@ void mat_quicksort(M, inf, sup, comp)22@ matrix_TYP **M;23@ int inf, sup, (*comp)();24@25@ sorts a list of matrices M from M[inf] to M[sup] with respect to comp.26@---------------------------------------------------------------------------27@28\**************************************************************************/29void mat_quicksort(M, inf, sup, comp)30matrix_TYP **M;31int inf, sup, (*comp)();32{33int low, med, high;34matrix_TYP *tmp;3536if(inf+1 < sup)37{38/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */39med = (inf+1+sup)/2;40tmp = M[inf];41M[inf] = M[med];42M[med] = tmp;43low = inf+1;44high = sup;45while(low < high)46{47while(comp(M[inf], M[low]) >= 0 && low < sup)48++low;49while(comp(M[inf], M[high]) <= 0 && high > inf)50--high;51if(low < high)52{53tmp = M[high];54M[high] = M[low];55M[low] = tmp;56}57}58if(inf != high)59{60tmp = M[high];61M[high] = M[inf];62M[inf] = tmp;63}64mat_quicksort(M, inf, high-1, comp);65mat_quicksort(M, high+1, sup, comp);66}67if(inf+1 == sup)68{69if(comp(M[inf], M[sup]) == 1)70{71tmp = M[inf];72M[inf] = M[sup];73M[sup] = tmp;74}75}76}7778/**************************************************************************\79@---------------------------------------------------------------------------80@ void vec_quicksort(v, inf, sup, dim, comp)81@ int **v;82@ int inf, sup, (*comp)(), dim;83@84@ sorts a list of vectors v from v[inf] to v[sup] with respect to comp.85@---------------------------------------------------------------------------86@87\**************************************************************************/88void vec_quicksort(v, inf, sup, dim, comp)89int **v;90int inf, sup, (*comp)(), dim;91{92int low, med, high;93int *tmp;9495if(inf+1 < sup)96{97/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */98med = (inf+1+sup)/2;99tmp = v[inf];100v[inf] = v[med];101v[med] = tmp;102low = inf+1;103high = sup;104while(low < high)105{106while(comp(v[inf], v[low], dim) >= 0 && low < sup)107++low;108while(comp(v[inf], v[high], dim) <= 0 && high > inf)109--high;110if(low < high)111{112tmp = v[high];113v[high] = v[low];114v[low] = tmp;115}116}117if(inf != high)118{119tmp = v[high];120v[high] = v[inf];121v[inf] = tmp;122}123vec_quicksort(v, inf, high-1, dim, comp);124vec_quicksort(v, high+1, sup, dim, comp);125}126if(inf+1 == sup)127{128if(comp(v[inf], v[sup], dim) == 1)129{130tmp = v[inf];131v[inf] = v[sup];132v[sup] = tmp;133}134}135}136137138/**************************************************************************\139@---------------------------------------------------------------------------140@ void pointer_mat_quicksort(v, inf, sup, rows, cols, comp)141@ int ***v;142@ int inf, sup, rows, cols, (*comp)();143@144@---------------------------------------------------------------------------145@146@ sorts a list of 2-dimensional vecotrs v from v[inf] to v[sup]147@ with respect to comp.148\**************************************************************************/149void pointer_mat_quicksort(v, inf, sup, rows, cols, comp)150int ***v;151int inf, sup, rows, cols, (*comp)();152{153int low, med, high;154int **tmp;155156if(inf+1 < sup)157{158/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */159med = (inf+1+sup)/2;160tmp = v[inf];161v[inf] = v[med];162v[med] = tmp;163low = inf+1;164high = sup;165while(low < high)166{167while(comp(v[inf], v[low], rows, cols) >= 0 && low < sup)168++low;169while(comp(v[inf], v[high], rows, cols) <= 0 && high > inf)170--high;171if(low < high)172{173tmp = v[high];174v[high] = v[low];175v[low] = tmp;176}177}178if(inf != high)179{180tmp = v[high];181v[high] = v[inf];182v[inf] = tmp;183}184pointer_mat_quicksort(v, inf, high-1, rows, cols, comp);185pointer_mat_quicksort(v, high+1, sup, rows, cols, comp);186}187if(inf+1 == sup)188{189if(comp(v[inf], v[sup], rows, cols) == 1)190{191tmp = v[inf];192v[inf] = v[sup];193v[sup] = tmp;194}195}196}197198199