2
I’m trying to implement the quicksort method, but the code I wrote is not working, someone can help me (it sorts part of the sequence, but some elements remain cluttered).
public static void quick(int v[], int start, int end) {
if (end>start) {
int pivo = v[(start+end)/2];
int i = start;
int f = end;
while (f>i) {
while (i <= end && v[i] < pivo) i++;
while (f >= start && v[f] >= pivo) f--;
if (f>i) {
int aux = v[f];
v[f] = v[i];
v[i] = aux;
i++;
f--;
}
}
if (f!= end)
quick(v, start, f);
if (i!=start)
quick(v, i, end);
}
}
From what I can tell, the ordering does not occur as expected when the pivot is part of the sequence. I changed the pivot strategy from the middle element to half the edges, and now it sometimes works and sometimes it doesn’t.
– user3444287