Metodos de armazenamento (QSort, SelectionSort)

6 respostas
D

Booa noite pessoal, preciso de uma pequena ajuda, tenho dois códigos aqui desses dois metodos de ordenação, alguem poderia me explicar detalhadamente como eles fucionam? Obrigado

SelectionSort

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sorts;

/**
 *
 * @author 184593
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    static int contador;

    public static void main(String[] args) {

        int array[] = new int[5];


        for (int i = 0; i < 5; i++) {
            array[i] = (int) (5 * Math.random());
        }
        //SELECTION SORT
        mostra(array);
        SelectionSort(array);
        mostra(array);
        System.out.println("Comparacoes: " + contador );

    }

    public static void SelectionSort(int[] v) {
        int index_min,
                aux;

        for (int i = 0; i < v.length; i++) {
            index_min = i;
            for (int j = i + 1; j < v.length; j++) {
                if (v[j] < v[index_min]) {
                    index_min = j;
                }
            }
            if (index_min != i) {
                aux = v[index_min];
                v[index_min] = v[i];
                v[i] = aux;
                contador++;
            }
        }
        System.out.println("ordenado");
    }

    public static void mostra(int[] v) {
        for (int i = 0; i < v.length; i++) {
            System.out.println(v[i] + "-");
        }
    }
}

QuickSort

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author 184593
 */
public class QuickSort {

    /**
     * @param args the command line arguments
     */
    static int contador;

    public static void main(String[] args) {

        int array[] = new int[1000];


        for (int i = 0; i < 1000; i++) {
            array[i] = (int) (1000 * Math.random());
        }
        //SELECTION SORT
//mostra(array);
        quick_sort(array,0,1000);
        mostra(array);
        System.out.println("comparacoes" + contador);

    }
  public static void quick_sort(int []v,int ini, int fim) {
                int meio;
 
                if (ini < fim) {
                        meio = partition(v, ini, fim);
                        quick_sort(v, ini, meio);
                        quick_sort(v, meio + 1, fim);
                }
        }
 
        public static int partition(int []v, int ini, int fim) {
                int pivo, topo, i;
                pivo = v[ini];
                topo = ini;
 
                for (i = ini + 1; i < fim; i++) {
                        if (v[i] < pivo) {
                                v[topo] = v[i];
                                v[i] = v[topo + 1];
                                topo++; 
                                contador++;
                        }
                }
                v[topo] = pivo;
                return topo;
        }

    public static void mostra(int[] v) {
        for (int i = 0; i < v.length; i++) {
            System.out.println(v[i] + "-");
        }
    }
}

6 Respostas

D

Cara, na boa, não leva a mau não, mas Google it

Tem milhões de videos explicativos, além disso voce pode debbugar o código.

Provavelmente você esta no segunda-periodo de faculdade, então corre atraz disso.

http://www.youtube.com/watch?v=ywWBy6J5gz8 —> QS

http://www.youtube.com/watch?v=Ns4TPTC8whw --> Selection Sort

Att,

D

Obrigado, irei dar uma olhada! Alguem poderia me explicar como posso debugar o código? Obrigado

denisspitfire

google it too

denisspitfire

caso voce esteja com problemas para debugar ou algo do genero podemos te ajudar… oque voce pode fazer é… pesquisar alguns algoritmos de ordenação. Depois tentar implementar.

denisspitfire

esse link sim vai te ajudar…acaba essa apostila que é 10 na prova.
http://www.caelum.com.br/download/caelum-algoritmos-estruturas-dados-java-cs14.pdf

D

Essa apostila é excelente.

Vale muito a pena ler a FJ-11 e a apostila de estrutura de dados (FJ-14 eu acho), em qual faculdade você estuda?

Criado 28 de maio de 2012
Ultima resposta 29 de mai. de 2012
Respostas 6
Participantes 3