(JAVA) Help in order of sorting (Selection Sort and Insertion)

1
  

/ * EXERCISE: Write a method that puts an unordered sequence of n integers in ascending order. Home   (a) Using sorting by selection
  (b) Using insertion ordering * /

I tried to solve the exercise, but I'm getting that result:
Original Sequence: 3 9 1 3 2 0 8 11
Sequence Selection: 0 3 1 3 2 11 8 9
Sequence Insertion: 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]+" ");
        } 
    }
}
    
asked by anonymous 03.07.2017 / 05:18

1 answer

0

I fixed your program, here it is:

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.

His program was almost right, the mistakes they had were few and silly:

  • In the selection algorithm, you should trace the smallest element of each iteration of i independent of the other iterations. It should not crawl in the context of the whole function because so, once the smallest element is found, you get stuck in it. The solution to this is to set the menor and posMenor verifiable within the loop of i starting from position i (not to zero).

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

  • Arrays being passed by parameters are modified and then returned. This means that in your test (in the main method), the array that would be passed to the insert order would be the array that results from sorting by selection (if it is already working, it will already be sorted). What you wanted to do was test sort ordering over an array with the same content as insert ordering. 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 java.util.Arrays.toString(int[]) to convert array elements to String , which eliminates the need to print element by element of it.

03.07.2017 / 06:01