I have to do an external quickSort simulator , showing for example the step by step of it.
Displaying changes in variables lower limit, upper limit, main vector, area vector (would be the available memory space to sort the file), upper writing, lower writing, lower reading, reading.
I have already done the quickSort algorithm and graphical interface algorithm showing only the end of the ordering.
Doubt What is the best way to show this in the graphical interface and how to do it, any ideas? No need to do it for me, just suggest a simple way to do it, since it's the first time I move with GUI. I'm going to post the quickSort class and interface.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package quicksortexterno;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
*
*/
public class Quick {
private int i, j;
private List<Integer> vetorArquivo = new ArrayList<>();
public Quick() {
}
public void addValor(int valor) {
vetorArquivo.add(valor);
}
public String inicia() {
quickSort(0, vetorArquivo.size() - 1);
return vetorArquivo.toString();
}
public void particao(int esq, int dir) {
int limiteSup;
int limiteInf;
List<Integer> area = new ArrayList<>();
boolean teste = true;
limiteInf = -100000000;
limiteSup = 100000000;
int escritaInf;
int escritaSup;
int leituraSup;
int Li;
int aux;
Li = escritaInf = esq;
leituraSup = escritaSup = dir;
i = esq - 1;
j = dir + 1;
while (leituraSup >= Li) {
if (area.size() < 2) {
if (teste) {
area.add(vetorArquivo.get(leituraSup));
Collections.sort(area);
leituraSup--;
teste = false;
} else {
area.add(vetorArquivo.get(Li));
Collections.sort(area);
Li++;
teste = true;
}
continue;
}
if (leituraSup == escritaSup) {
aux = vetorArquivo.get(leituraSup);
leituraSup--;
teste = false;
} else {
if (Li == escritaInf) {
aux = vetorArquivo.get(Li);
Li++;
teste = true;
} else {
if (teste) {
aux = vetorArquivo.get(leituraSup);
leituraSup--;
teste = false;
} else {
aux = vetorArquivo.get(Li);
Li++;
teste = true;
}
}
}
if (aux > limiteSup) {
j = escritaSup;
vetorArquivo.remove(escritaSup);
vetorArquivo.add(escritaSup, aux);
escritaSup--;
continue;
}
if (aux < limiteInf) {
i = escritaInf;
vetorArquivo.remove(escritaInf);
vetorArquivo.add(escritaInf, aux);
escritaInf++;
continue;
}
area.add(aux);
Collections.sort(area);
if (escritaInf - esq < dir - escritaSup) {
vetorArquivo.remove(escritaInf);
vetorArquivo.add(escritaInf, area.get(0));
escritaInf++;
limiteInf = area.get(0);
area.remove(0);
} else {
vetorArquivo.remove(escritaSup);
vetorArquivo.add(escritaSup, area.get(area.size() - 1));
escritaSup--;
limiteSup = area.get(area.size() - 1);
area.remove(area.size() - 1);
}
}
int k = 0;
while (escritaInf <= escritaSup) {
vetorArquivo.remove(escritaInf);
vetorArquivo.add(escritaInf, area.get(k));
k++;
escritaInf++;
}
System.out.println(vetorArquivo.toString());
}
public void quickSort(int esq, int dir) {
if (dir - esq >= 1) {
particao(esq, dir);
if (i - esq < dir - j) {
quickSort(esq, i);
quickSort(j, dir);
} else {
quickSort(j, dir);
quickSort(esq, i);
}
}
}
}
-
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package quicksortexterno;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
/**
*
* @author
*/
public class Janela extends javax.swing.JFrame {
Quick quick = new Quick();
ArrayList<Integer> vetorAdd = new ArrayList<>();
/**
* Creates new form Janela
*/
public Janela() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
quickSortButton = new javax.swing.JButton();
adicionar = new javax.swing.JButton();
valor = new javax.swing.JTextField();
labelAdd = new javax.swing.JLabel();
label = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Simulador Quick Sort Externo");
quickSortButton.setText("QuickSort");
quickSortButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quickSortButtonActionPerformed(evt);
}
});
adicionar.setText("Adicionar");
adicionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
adicionarActionPerformed(evt);
}
});
labelAdd.setText(" ");
label.setText(" ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(adicionar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 655, Short.MAX_VALUE)
.addComponent(quickSortButton)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(label)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(adicionar)
.addComponent(quickSortButton)
.addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelAdd))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label)
.addContainerGap(160, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void adicionarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
quick.addValor(parseInt(valor.getText()));
vetorAdd.add(parseInt(valor.getText()));
labelAdd.setText(vetorAdd.toString());
valor.setText("");
}
private void quickSortButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String s = quick.inicia();
label.setText(s);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Janela().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton adicionar;
private javax.swing.JLabel label;
private javax.swing.JLabel labelAdd;
private javax.swing.JButton quickSortButton;
private javax.swing.JTextField valor;
// End of variables declaration
}