Implementazioni di algoritmi/Quicksort: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m →Ruby |
|||
Riga 7:
===[[w:linguaggio C|C]]===
<source lang="C">
namespace { // namespace anonimo
void sort(int array[], int begin, int end) {▼
template<class T>
void
if(_x >= _y)
return;
const T m = _x[(_y-_x)/2];
T *x = _x; // just a copy
T *y = _y;
while(true) {
while(*x < m && x < y) x++;
while(*y > m && x < y || *x == *y)
if(x < y) {
T t = *x;
*x = *y;
*y = t;
}
else
break;
qSort(_x, x); // tutti gli elementi minori di m
qSort(x+1, _y); // tutti quelli maggiori uguali di m
}
}
template<class T>
qSort(x, &x[size-1]); // passa a qSort il primo elemento e l'ultimo}
</source>
|