Bom estou finalizando um trabalho para faculdade, so que o danado insiste em dar o seguinte erro:
Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “”
Eu sei que o motivo é porque o a variável arrayCamposTamanho[0] não foi preenchida, mas eu queria que de alguma forma, mesmo se o campo estivesse vazio o programa não lotasse o console de erros. Sei la, se tivesse vazio aparecesse uma mensagem solicitando o valor ou avisando que o campo está vazio.
Tem como alguém me salvar? Preciso entregar esse trabalho ainda hoje.
Segue código:
Sei* que está meio bagunçado, mas o principal(onde ta dando o erro) encontra-se no método ActionPerformed(linha 126).
[code]package Grafica.src;
import java.awt.;
import java.awt.event.;
import java.util.;
import javax.swing.;
class Grafica extends JFrame implements ActionListener {
String s;
int valor, temp;
JLabel label;
JButton botao, botinha, sair;
JComboBox comboArranjos;
String[] vetorArranjos = { "Aleatórios", "Misturados", "Crescente",
"Descrescente", "Iguais", "Crescente Misto", "Decrescente Misto" };
int[] auxiliar ;
JTextField[] arrayCamposTamanho = new JTextField[10];
JCheckBox insercaoDireta;
JCheckBox selecaoDireta;
JCheckBox bolha;
JCheckBox mergesort;
JCheckBox quicksort;
JCheckBox quicksortAleatorio;
JCheckBox heapsort;
JCheckBox shellsort;
JFrame principal;
JPanel panelAlgoritmos;
JPanel panelTamanhos;
JPanel panelLivre;
JPanel panelArranjos;
JLabel tipo, impressao, tamanho;
JTextArea texto;
Grafica() {
setTitle("Execução de algoritmos de ordenação");
}
void montaTela() {
arrayCamposTamanho[0] = new JTextField(null);
insercaoDireta = new JCheckBox("Inserção Direta");
selecaoDireta = new JCheckBox("Seleção Direta");
bolha = new JCheckBox("Bolha");
mergesort = new JCheckBox("Mergesort");
quicksort = new JCheckBox("Quicksort");
quicksortAleatorio = new JCheckBox("Quicksort Aleatório");
heapsort = new JCheckBox("Heapsort");
shellsort = new JCheckBox("Shellsort");
principal = new Grafica();
principal.setSize(600, 450);
principal.setLayout(new GridLayout(2, 3, 10, 10));
principal.setLocation(250,100);
principal.setVisible(true);
principal.setResizable(false);
principal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelAlgoritmos = new JPanel();
panelAlgoritmos.setLayout(new BoxLayout(panelAlgoritmos,BoxLayout.Y_AXIS));
panelTamanhos = new JPanel();
panelTamanhos.setLayout(new BoxLayout(panelTamanhos, BoxLayout.Y_AXIS));
panelLivre = new JPanel();
panelLivre.setLayout(new BoxLayout(panelLivre, BoxLayout.Y_AXIS));
panelArranjos = new JPanel();
panelArranjos.setLayout(new BoxLayout(panelArranjos, BoxLayout.Y_AXIS));
botao = new JButton("Calcular");
botinha = new JButton("About");
sair = new JButton("Sair");
comboArranjos = new JComboBox(vetorArranjos);
botao.addActionListener(this);
botinha.addActionListener(this);
sair.addActionListener(this);
principal.add(panelAlgoritmos);
principal.add(panelTamanhos);
principal.add(panelLivre);
principal.add(panelArranjos);
tipo = new JLabel("Tipo de geração dos números do vetor");
panelArranjos.add(tipo);
panelArranjos.add(comboArranjos);
comboArranjos.setSize(1,1);
comboArranjos.setAlignmentY(150);
comboArranjos.setSize(1,1);
panelLivre.add(botao);
panelLivre.add(botinha);
panelLivre.add(sair);
sair.setVisible(true);
botao.setVisible(true);
botinha.setVisible(true);
impressao = new JLabel ("Área de Impressão");
tamanho = new JLabel ("Tamanho do vetor");
//Colocando os elementos do array dentro do painel
texto = new JTextArea(50,50);
texto.setEditable(false);
JScrollPane scrollPane = new JScrollPane(texto);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
/*for(int j = 0; j < array.length; j ++){
System.out.println(texto.append(array[j]));
}*/
panelAlgoritmos.add(insercaoDireta);
panelAlgoritmos.add(selecaoDireta);
panelAlgoritmos.add(bolha);
panelAlgoritmos.add(mergesort);
panelAlgoritmos.add(quicksort);
panelAlgoritmos.add(quicksortAleatorio);
panelAlgoritmos.add(heapsort);
panelAlgoritmos.add(shellsort);
panelTamanhos.add(tamanho);
panelTamanhos.add(arrayCamposTamanho[0]);
panelTamanhos.add(impressao);
panelTamanhos.add(scrollPane);
}
public void actionPerformed(ActionEvent event) {
if("Sair".equals(event.getActionCommand())){
System.exit(0);
}
if("About".equals(event.getActionCommand())){
JOptionPane.showMessageDialog(principal,"xxx\n"+
"RGM:111111", "About", JOptionPane.INFORMATION_MESSAGE);
}
int [] temporario = new int[Integer.parseInt(arrayCamposTamanho[0].getText())];
temp = Integer.parseInt(arrayCamposTamanho[0].getText());
GeradorDeArranjos ger = new GeradorDeArranjos();
//Pegando o tamanho do array e gerando um arranjo de numeros
if(comboArranjos.getSelectedIndex() == 0){
temporario = ger.arranjoDeElementosAleatorios(temp);
}
if(comboArranjos.getSelectedIndex() == 1){
temporario = ger.arranjoDeElementosMisturados(temp);
}
if(comboArranjos.getSelectedIndex() == 2){
temporario = ger.arranjoDeElementosCrescentes(temp);
}
if(comboArranjos.getSelectedIndex() == 3){
temporario = ger.arranjoDeElementosDecrescentes(temp);
}
if(comboArranjos.getSelectedIndex() == 4){
temporario = ger.arranjoDeElementosIguais(temp);
}
if(comboArranjos.getSelectedIndex() == 5){
s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp,scan);
}
if(insercaoDireta.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
//for(int j =0; j< temporario.length; j++){
// .System.out.println(auxiliar[j]);
//}
int[] aux = temporario;
long tinicial = new Date().getTime();
int id, ie, numeroAInserir;
int fim = aux.length;
for(id=1; id < fim; id++)
{
numeroAInserir = aux[id];
ie=id;
while((ie >0) && (aux[ie-1] > numeroAInserir)){
aux[ie] = aux[ie-1];
ie--;
}
aux[ie]=numeroAInserir;
}
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
valor =1 ;
print(texec,valor);
}
}
}
if(selecaoDireta.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
int sd, se, indiceDoMinimo, tempo;
int fim = aux.length;
for (sd = 0; sd< fim - 1; sd++)
{
// Inicialmente o menor elemento já visto é o primeiro elemento
indiceDoMinimo = sd;
for(se=sd+1; se < fim; se++)
{
if(aux[se] < aux[indiceDoMinimo])
indiceDoMinimo = se;
}
// Coloca o menor elemento no inicio do sub-vetor atual. Para isso,
// troca de lugar os elementos nos indices e e indiceDoMinimo.
tempo = aux[sd];
aux[sd] = aux[indiceDoMinimo];
aux[indiceDoMinimo] =tempo;
}
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
valor =2 ;
print(texec,valor);
}
}
}
if(bolha.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
int bo, bp, tempbolha;
int fim = aux.length;
for(bo=fim -1; bo > 0 ; bo--)
{
// Varre o vetor desde o inicio procurando erros de ordenacao.
// Como cada passagem o maior elemento sobe até sua
// posicao correta, nao há necessidade de ir até o final.
for(bp=1; bp <= bo; bp++)
// Se a ordem está errada para o par j-1 e j
if(aux[bp-1] > aux[bp])
{
// Troca os dois de lugar
tempbolha = aux[bp-1];
aux[bp-1] = aux[bp];
aux[bp] = tempbolha;
}
}
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
//System.out.println("Tempo de Execução do algoritmo Bolha: "+texec+" Tamanho do arranjo: "+temp);
valor =3 ;
print(texec,valor);
}
}
}
if(mergesort.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
mergeSort(aux,0, aux.length-1);
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
//System.out.println("Tempo de Execução do algoritmo Mergesort: "+texec+" Tamanho do arranjo: "+temp);
valor =4 ;
print(texec,valor);
}
}
}
if(quicksort.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
quickSort(aux,0,aux.length-1);
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
//System.out.println("Tempo de Execução do algoritmo Quicksort: "+texec+" Tamanho do arranjo: "+temp);
valor =5 ;
print(texec,valor);
}
}
}
if(quicksortAleatorio.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
quickSortAleatorio(aux, 0,aux.length-1);
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
//System.out.println("Tempo de Execução do algoritmo Quicksort Aleatório: "+texec+" Tamanho do arranjo: "+temp);
valor =6 ;
print(texec,valor);
}
}
}
if(heapsort.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//int temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
heapSort(aux);
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
//System.out.println("Tempo de Execução do algoritmo Heapsort: "+texec+" Tamanho do arranjo: "+temp);
valor =7 ;
print(texec,valor);
}
}
}
if(shellsort.isSelected()){
for(int i = 0; i < 1; i++){
if(arrayCamposTamanho[i].getText().equals("")){
System.out.println(" - ");
}
else{
//temp = Integer.parseInt(arrayCamposTamanho[i].getText());
if(comboArranjos.getSelectedIndex() == 0)
temporario = ger.arranjoDeElementosAleatorios(temp);
else if(comboArranjos.getSelectedIndex() == 1)
temporario = ger.arranjoDeElementosMisturados(temp);
else if(comboArranjos.getSelectedIndex() == 2)
temporario = ger.arranjoDeElementosCrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 3)
temporario = ger.arranjoDeElementosDecrescentes(temp);
else if(comboArranjos.getSelectedIndex() == 4)
temporario = ger.arranjoDeElementosIguais(temp);
else if(comboArranjos.getSelectedIndex() == 5){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
else if(comboArranjos.getSelectedIndex() == 6){
//s = JOptionPane.showInputDialog(null,"Informe a porcentagem de elementos aleatórios - Entre 1% e 50%","",JOptionPane.QUESTION_MESSAGE);
int scan = Integer.parseInt(s);
temporario = ger.arranjoDeElementosCrescentesMistos(temp, scan);
}
int[] aux = ger.arranjoDeElementosAleatorios(temp);
long tinicial = new Date().getTime();
shellSort(aux);
long tfinal = new Date().getTime();
long texec = tfinal - tinicial;
//System.out.println("Tempo de Execução do algoritmo Shellsort: "+texec+" Tamanho do arranjo: "+temp);
valor =8 ;
print(texec,valor);
}
}
}
}
void merge(int [] A, int p, int q, int r) {
// A subseqüência A[p…q] está ordenada
// A subseqüência A[q+1…r] está ordenada
int i, j, k;
// Faz cópias das subseqüência seq1 = A[p…q] e seq2 = A[q+1…r]
int tamseq1 = q - p + 1; // tamanho da subseqüência 1
int tamseq2 = r - q; // tamanho da subseqüência 2
int [] seq1 = new int [tamseq1];
for(i=0; i < seq1.length; ++i)
seq1[i] = A[p+i];
int [] seq2 = new int [tamseq2];
for(j=0; j < seq2.length; ++j)
seq2[j] = A[q+j+1];
// Faz a junção das duas subseqüências
k = p; i = 0; j = 0;
while (i < seq1.length && j < seq2.length)
{
// Pega o menor elemento das duas seqüências
if(seq2[j] <= seq1[i])
{
A[k] = seq2[j];
j++;
}
else
{
A[k] = seq1[i];
i++;
}
k++;
}
// Completa com a seqüência que ainda não acabou
while (i < seq1.length)
{
A[k] = seq1[i];;
k++;
i++;
}
while (j < seq2.length)
{
A[k] = seq2[j];
k++;
j++;
}
// A subseqüência A[p…r] está ordenada
}
void mergeSort(int [] numeros, int p, int r)
{
int q;
if(p < r)
{
q = (p+r)/2;
mergeSort(numeros, p,q);
mergeSort(numeros, q+1,r);
merge(numeros,p,q,r);
}
}
void quickSort(int [] A, int p, int r)
{
int q;
if( p < r )
{
q = particao(A, p, r);
quickSort(A, p, q-1);
quickSort(A, q+1, r);
}
}
int particao(int [] A, int p, int r)
{
int x, i, j, temp;
x = A[r]; // pivo
i = p - 1;
for(j=p; j <= r-1; ++j)
if(A[j] <= x)
{
i++;
// trocar
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
// reposicionar o pivo
temp = A[i+1];
A[i+1] = A[r];
A[r] = temp;
return (i+1);
}
void quickSortAleatorio(int [] A, int p, int r)
{
int q;
if( p < r )
{
q = particaoAleatoria(A, p, r);
quickSortAleatorio(A, p, q-1);
quickSortAleatorio(A, q+1, r);
}
}
int particaoAleatoria (int [] A, int p, int r)
{
int i, temp;
double f;
// Escolhe um número aleatório entre p e r
f = java.lang.Math.random(); // retorna um real f tal que 0 <= f < 1
i =(int) (p + (r - p) * f);
// i é tal que p <= i < r
// Troca de posicao A[i] e A[r]
temp = A[r];
A[r] = A[i];
A[i] = temp;
return particao(A, p, r);
}
//HeapSort
int pai(int i) { return (i-1)/2;}
int esquerda(int i) { return 2*i+1;}
int direita(int i) { return 2*i+2;}
void refazHeapMax(int A[], int i, int compHeap)
{
int esq, dir, maior, menor, temp;
esq = esquerda(i);
dir = direita(i);
if(esq < compHeap && A[esq] > A[i])
maior = esq;
else
maior = i;
if(dir < compHeap && A[dir] > A[maior])
maior = dir;
if(maior != i)
{
// trocar A[i] <==> A[maior]
temp = A[i];
A[i] = A[maior];
A[maior] = temp;
// Ajusta a posicao de maior, se incorreta.
refazHeapMax(A, maior, compHeap);
}
}
void constroiHeapMax(int [] A)
{
int compHeap=A.length;
for(int i = (A.length)/2-1; i >= 0 ; i--)
refazHeapMax(A,i,compHeap);
}
void heapSort(int A[])
{
int i, compHeap, temp;
// Constrói o heap máximo do arranjo todo
compHeap = A.length;
constroiHeapMax(A);
for(i=A.length-1; i > 0; --i)
{
temp=A[0];
A[0]=A[i];
A[i]=temp;
// Diminui o heap, pois A[i] está posicionado
compHeap--;
refazHeapMax(A,0,compHeap);
}
}
//Shellsort
void shellSort(int A [])
{
int h;
int x, i, j;
// Determina o h maior que o comprimento do arranjo
for(h=1; h < A.length; h = 3*h+1)
;
while( h > 1)
{
h = h/3; // h é dividido por três
for(i = h; i < A.length; ++i)
{
x = A[i];
j = i;
while(j >= h && A[j-h] > x)
{
A[j] = A[j-h];
j=j-h;
}
A[j] = x;
}
}
}
// Metodo de impressao generico
void print(long texec, int tipo){
if(tipo == 1)
texto.append("\n\nTempo de Execução do algoritmo Inserção Direta: \n"
+texec+“ms\n”+“Tamanho do arranjo:\n “+temp+”\n\n”);
if(tipo == 2){
texto.append("\n\nTempo de Execução do algoritmo Seleção Direta: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
if(tipo == 3){
texto.append("\n\nTempo de Execução do algoritmo Bolha: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
if(tipo == 4){
texto.append("\n\nTempo de Execução do algoritmo MergeSort: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
if(tipo == 5){
texto.append("\n\nTempo de Execução do algoritmo QuickSort: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
if(tipo == 6){
texto.append("\n\nTempo de Execução do algoritmo QuickSort Aleatório: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
if(tipo == 7){
texto.append("\n\nTempo de Execução do algoritmoHeapSort: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
if(tipo == 8){
texto.append("\n\nTempo de Execução do algoritmo ShellSort: \n"
+texec+"ms\n"+"Tamanho do arranjo:\n "+temp+"\n\n");
}
}
}
[/code]
Agradeço desde já e fico no aguardo.