(JAVA) help in sorting exercise (Selection Sort and insert)

/ * Exercise: write a method that puts in ascending order a disordered sequence of n integers.
(a) using sorting by selection
(b) using sorting by insert*/

I tried to solve the exercise, but I am coming to this result:
Original Sequence: 3 9 1 3 2 0 8 11
Sequence Selection: 0 3 1 3 2 11 8 9
Insertion sequence: 0 1 3 2 3 8 9 11
What Can I be doing wrong?

class Tres {

    public static int [] ordenaSelecao (int [] array) {
    int [] arraySelecao = array;
    int menor = arraySelecao[0];
    int posMenor = 0;

        for (int i = 0; i<arraySelecao.length; i++) {
            //buscando menor elemento
            for (int j=i+1; j<arraySelecao.length; j++){
                if (menor > arraySelecao[j]) {
                menor = arraySelecao[j];
                posMenor = j;
                }

            }   

            if (arraySelecao[i] > arraySelecao[posMenor]) {
                int aux = arraySelecao[posMenor];
                arraySelecao[posMenor] = arraySelecao[i];
                arraySelecao[i] = aux;
            }

        }
        return arraySelecao;
    }


    public static int [] ordenaInsercao (int [] array) {
    int [] arrayInsercao = array;
        for (int i=1; i<arrayInsercao.length; i++){
            for (int j=i-1; j>0; j--){
                if (arrayInsercao[i]<arrayInsercao[j]) {
                    int aux = arrayInsercao[i];
                    arrayInsercao[j+1] = arrayInsercao[j];
                    arrayInsercao[j] = aux;
                }
            }
        }

        return arrayInsercao;

    }



    public static void main(String[] args) {
        int [] array = {3,9,1,3,2,0,8,11};

        System.out.print("Sequencia Original: ");
        for (int i=0; i<array.length; i++) {
            System.out.print(array[i]+" ");
        }

        System.out.println(" ");
        System.out.print("Sequencia Selecao: ");
        int [] arraySelecao = ordenaSelecao(array);
        for (int i=0; i<arraySelecao.length; i++) {
            System.out.print(arraySelecao[i]+" ");
        }

        System.out.println(" ");
        System.out.print("Sequencia Insercao: ");
        int [] arrayInsercao = ordenaInsercao(array);
        for (int i=0; i<arrayInsercao.length; i++) {
            System.out.print(arrayInsercao[i]+" ");
        } 
    }
}
Author: Victor Stafusa, 2017-07-03

1 answers

I fixed your program, here's how it looked:

import java.util.Arrays;

class Tres {
    public static int[] ordenaSelecao(int[] array) {
        int[] arraySelecao = array.clone();

        for (int i = 0; i < arraySelecao.length; i++) {
            int menor = arraySelecao[i];
            int posMenor = i;

            // Buscando o menor elemento.
            for (int j = i + 1; j < arraySelecao.length; j++) {
                if (menor > arraySelecao[j]) {
                    menor = arraySelecao[j];
                    posMenor = j;
                }
            }

            // Posicionando o menor elemento.
            if (arraySelecao[i] > arraySelecao[posMenor]) {
                int aux = arraySelecao[posMenor];
                arraySelecao[posMenor] = arraySelecao[i];
                arraySelecao[i] = aux;
            }
        }
        return arraySelecao;
    }

    public static int[] ordenaInsercao(int[] array) {
        int[] arrayInsercao = array.clone();
        for (int i = 1; i < arrayInsercao.length; i++) {
            for (int j = i - 1; j >= 0; j--) {
                if (arrayInsercao[j + 1] < arrayInsercao[j]) {
                    int aux = arrayInsercao[j + 1];
                    arrayInsercao[j + 1] = arrayInsercao[j];
                    arrayInsercao[j] = aux;
                }
            }
        }

        return arrayInsercao;
    }

    public static void main(String[] args) {
        int[] arrayOriginal = {3, 9, 1, 3, 2, 0, 8, 11};
        System.out.println("Sequencia Original: " + Arrays.toString(arrayOriginal) + ".");

        int[] arraySelecao = ordenaSelecao(arrayOriginal);
        System.out.println("Sequencia Selecao: " + Arrays.toString(arraySelecao) + ".");

        int[] arrayInsercao = ordenaInsercao(arrayOriginal);
        System.out.println("Sequencia Insercao: " + Arrays.toString(arrayInsercao) + ".");
    }
}

See here working on ideone.

Your program was almost right, the errors there were few and silly:

  • In the selection algorithm, you must track the smallest element of each iteration of i independent of the other iterations. It should not crawl in the context of the entire function because so, once the smallest element is found, you get stuck in it. Solution to do this, put the veriables menor and posMenor into the loop of i starting them from the position i (and not zero).

  • In the insertion algorithm, within the j loop, you compare the positions i, j and j + 1. You should only use the positions j and j + 1. Also, the Stop condition is j >= 0, and not j>0.

  • The arrays being passed by parameters are modified and then returned. This means that in your test (in method main), the array that would be passed to insert sorting would be the array resulting from selection sorting(which if it is working, will already be sorted). What you wanted was to test selection sorting on top of an array with the same content given to insert sorting. To do this, you can use the clone() method of the array to create a copy, thus avoiding changing the original array.

  • You can use the method java.util.Arrays.toString(int[]) for convert the array elements to a String, which eliminates the need to print element by Element from it.

 0
Author: Victor Stafusa, 2017-07-03 04:01:52