Riordinamento numerico: quicksort
#include
float *ordina( int N, float v[]) {
int i, j, k=0;
float separator;
float dum;
if (N<2) return v;
separator = v[N/2];
i=0;
j=N-1;
while (j>i) {
while ( v[i]<separator ) i++;
while ( v[j]>separator ) j--;
if (i<j) {
dum = v[i];
v[i]= v[j];
v[j]= dum;
k++;
i++;
}
}
ordina(i,v);
ordina(N-i,v+i);
if(k) {
return v;
} else {
return NULL;
}
}
» Torna »
|