está mal el método de partitionDutchFlag
Publicado por ignacio (1 intervención) el 04/11/2020 15:15:35
Que está mal el método de partitionDutchFlag, tiene que tomar 4 parametros, el arreglo, la pos inicial, pivote, posición final, después hace mal la recursión en el QuickSort2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private static <T extends Comparable<? super T>> void quickSort2(T[] array,int begin, int end){
if (array == null) throw new IllegalArgumentException("El arreglo es null, no se puede ordenar");
int inicio = 0;
int fin = (array.length)-1;
quickSort2(array, inicio, fin);
}
private static <T extends Comparable<? super T>> void quickSort2(T[] array,int begin, int end){
if(begin < end){
//calculo la partición
int p = partitionDutchFlag(array, array[array.length/2]);
//ordeno la parte izquierda
quickSort2(array, begin, p);
//ordeno la parte derecha
quickSort2(array, p+1, end);
}
}
private static <T extends Comparable<? super T>> int partitionDutchFlag(T[] array,T mitad){
int i = 0;
int j = 0;
int k = array.length;
while (j < k){
if(array[j].compareTo(mitad) < 0){
swap(array,i,j);
i++;
j++;
}else if(array[j].compareTo(mitad) > 0){
k--;
swap(array, j, k);
}else{
j++;
}
}
return j;
}
Valora esta pregunta


0