0

El vector de objetos se compone de puntos, cada punto tiene una coordenada x e y. Teniendo en cuenta que necesito realizar dos ordenamientos, uno utilizando la coordenada X y otro usando la coordenada Y me recomendaron pasar a otro vector la coordenada en cuestión y aplicarle el ordenamiento a ese vector, pero a la vez que se vayan moviendo, ir moviendo el vector de objetos original. Tengo el mergeSort y me funciona, pero aun no logro que me funcione el movimiento de los objetos.

Tengo lo siguiente:

public void merge(double[] array, int start, int mid, int end, Point[] Point){
    // create a temp array
    double[] temp = new double[end - start + 1];
    // tracking vars for intervals and for temp
    int i = start, j = mid + 1, k = 0, cont = 0;
    // traverse both arrays and in each iteration add smaller of both elements int temp
    while(i <= mid && j <= end){
        if(array[i] <= array[j]){
            temp[k] = array[i];
            k++; i++; cont++;
        }else{
            temp[k] = array[j];
            Point tempo = Point[j];  // se supone que aca hace un swap
            Point[cont] = tempo;     // para mover el objeto punto
            k++; j++; cont++;   
        }
    }
    // add elements left in the first interval
    while(i <= mid){
        temp[k] = array[i];
        k++; i++;
    }
    // add elements left in the second interval
    while(j <= end){
        temp[k] = array[j];
        k++; j++;
    }
    // copy temp to original interval
    for(i = start;i <= end;i++){
        array[i] = temp[i - start];
    }
}

// start and end are the starting and ending index of current interval of array
public void mergeSort(double[] array, int start, int end, Point[] Point){
    if(start < end){
        int mid = (start + end) / 2;
        mergeSort(array, start, mid, Point);
        mergeSort(array, mid+1, end, Point);
        merge(array, start, mid, end, Point);
    }
}

ahora el main

public static void main(String[] args) {
    test t = new test();
    Point[] puntos = new Point[10];

    puntos[0] = new Point(5.1, 5.2);
    puntos[1] = new Point(3.3, 4.2);
    puntos[2] = new Point(7.5, 6.5);
    puntos[3] = new Point(4.2, 5.0);
    puntos[4] = new Point(8.3, 7.6);
    puntos[5] = new Point(5.9, 7.0);
    puntos[6] = new Point(6.4, 5.2);
    puntos[7] = new Point(2.6, 3.7);

    double[] coordX = new double[8];

    for(int i = 0;i < coordX.length;i++){
        coordX[i] = puntos[i].getX();
    }

    t.mergeSort(coordX, 0, coordX.length - 1, puntos);

    for(double x : coordX){
        System.out.println(x);
    }

    for(Point p : puntos){
        System.out.println(p);
    }
}

Obtengo dos salidas, el vector de coord ordenado en X (Funciona) y el vector de puntos ordenado con referencia al vector de coord(No funciona)

2.6
3.3
4.2
5.1
5.9
6.4
7.5
8.3

Point{x=8.3, y=7.6}
Point{x=7.5, y=6.5}
Point{x=2.6, y=3.7}
Point{x=4.2, y=5.0}
Point{x=5.9, y=7.0}
Point{x=6.4, y=5.2}
Point{x=6.4, y=5.2}
Point{x=2.6, y=3.7}
null
null

1 respuesta 1

0

Después de intentar varias cosas y guiarme con otros metodos de ordenamiento, llegué a la conclusión en que claramente no le estaba aplicando el mergesort al vector de objetos, la solución fue la siguiente:

public void merge(double[] array, int start, int mid, int end, Point[] Point){
    // create a temp array
    double[] temp = new double[end - start + 1];
    Point[] tempo = new Point[end - start + 1];   // CREO UN VECTOR AUX DE PUNTOS PARA
                                                  // PARA IR LLENANDO EL VECTOR CON 
                                                  // CADA MOVIMIENTO
    // tracking vars for intervals and for temp
    int i = start, j = mid + 1, k = 0; //cont = 0;
    // traverse both arrays and in each iteration add smaller of both elements int temp
    while(i <= mid && j <= end){
        if(array[i] <= array[j]){
            temp[k] = array[i];
            tempo[k] = Point[i];   // AGREGO EL PUNTO CORRESPONDIENTE AL VECTOR AUX
            k++; i++;
        }else{
            temp[k] = array[j];
            tempo[k] = Point[j];    // AGREGO EL PUNTO CORRESPONDIENTE AL VECTOR AUX
            k++; j++;
        }
    }
    // add elements left in the first interval
    while(i <= mid){
        temp[k] = array[i];
        tempo[k] = Point[i];    // AGREGO EL PUNTO CORRESPONDIENTE AL VECTOR AUX
        k++; i++;
    }
    // add elements left in the second interval
    while(j <= end){
        temp[k] = array[j];
        tempo[k] = Point[j];      // AGREGO EL PUNTO CORRESPONDIENTE AL VECTOR AUX
        k++; j++;
    }
    // copy temp & tempo to original interval
    for(i = start;i <= end;i++){
        array[i] = temp[i - start];
    }
    for(i = start;i <= end;i++){
        Point[i] = tempo[i - start];
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.