package component;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class QuickSortJPanel extends JPanel implements Runnable {
// 构造方法
public QuickSortJPanel(int[] A) {
this.B = A;
this.A = A;
this.higth = 60 * A.length;
this.setPreferredSize(new Dimension(400, 480));
this.setBackground(Color.WHITE);
}
public void run() {
int[] tempSort = new int[B.length];
for (int ttt = 0; ttt < B.length; ttt++)
tempSort[ttt] = B[ttt];
ALsort.add(tempSort);
QSort(A, 0, A.length - 1);
step = 1;
for (step = 1; step <= ALswap.size(); step++) {
p = (PairIJ) ALswap.get(step - 1);
swapI = p.getI();
swapJ = p.getJ();
sort = (int[]) ALsort.get(step - 1);
distance = (swapJ - swapI) * 40;
Ipivot = (Integer) ALpivot.get(step - 1);
pivot = Ipivot.intValue();
paintShow();
}
lastPaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Font ft = new Font("songti", Font.BOLD, 18);
if (sort != null)
for (int i = 0; i < sort.length; i++) {
if (i == swapI - 1) {
if (paintTime < (swapJ - swapI) * 40 / 2) {
if (i == pivot)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
y++;
g.fillOval(swapI * 40 + paintTime, higth / 2 - 15 + y,
30, 30);
g.setColor(Color.BLACK);
g.setFont(ft);
g.drawString("" + sort[i], swapI * 40 + paintTime + 10,
higth / 2 - 15 + y + 20);
} else {
y--;
if (i == pivot)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillOval(swapI * 40 + paintTime, higth / 2 - 15 + y,
30, 30);
g.setColor(Color.BLACK);
g.setFont(ft);
g.drawString("" + sort[i], swapI * 40 + paintTime + 10,
higth / 2 - 15 + y + 20);
}
} else if (i == swapJ - 1) {
if (paintTime < (swapJ - swapI) * 40 / 2) {
if (i == pivot)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillOval(swapJ * 40 - paintTime, higth / 2 - 15
- paintTime, 30, 30);
g.setColor(Color.BLACK);
g.setFont(ft);
g.drawString("" + sort[i], swapJ * 40 - paintTime + 10,
higth / 2 - 15 - paintTime + 20);
} else {
if (i == pivot)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillOval(swapJ * 40 - paintTime, higth / 2 - 15
- (swapJ - swapI) * 40 + paintTime, 30, 30);
g.setColor(Color.BLACK);
g.setFont(ft);
g.drawString("" + sort[i], swapJ * 40 - paintTime + 10,
higth / 2 - 15 - (swapJ - swapI) * 40
+ paintTime + 20);
}
} else {
if (i == pivot)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillOval((i + 1) * 40, higth / 2 - 15, 30, 30);
g.setColor(Color.BLACK);
g.setFont(ft);
g.drawString("" + sort[i], (i + 1) * 40 + 10,
higth / 2 - 15 + 20);
}
}
if (last) {
for (int i = 0; i < A.length; i++) {
g.setColor(Color.RED);
g.fillOval((i + 1) * 40, higth / 2 - 15, 30, 30);
g.setColor(Color.BLACK);
g.setFont(ft);
g.drawString("" + A[i], (i + 1) * 40 + 10, higth / 2 - 15 + 20);
}
}
}
// 整数快速排序
public void QSort(int a[], int left, int right) {
int i = left;
int j = right;
// 选择中间一个为中轴
int p = (left + right) / 2;
ALpivot.add(new Integer(p));
if (left < right) {
while (i < j) {
while (a[i] <= a[p] && i < right)
i++;
while (a[j] > a[p] && j > left)
j--;
if (i <= j || a[j] > a[i]) {
if (i != j) {
swap(a, i, j);
int[] tempSort = new int[a.length];
for (int ttt = 0; ttt < B.length; ttt++)
tempSort[ttt] = a[ttt];
ALsort.add(tempSort);
}
}
}
if (a[p] >= a[j] && p != j) {
swap(a, p, j);
int[] tempSort = new int[a.length];
for (int ttt = 0; ttt < B.length; ttt++)
tempSort[ttt] = a[ttt];
ALsort.add(tempSort);
}
QSort(a, left, j - 1);
QSort(a, j + 1, right);
}
}
public void paintShow() {
for (paintTime = 0; paintTime < distance; paintTime++) {
repaint();
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void swap(int[] a2, int i, int j) {
int temp = a2[i];
a2[i] = a2[j];
a2[j] = temp;
ALswap.add(new PairIJ(i + 1, j + 1));
}
public void lastPaint() {
last = true;
repaint();
}
public void nextStep(boolean next) {
if (next) {
step++;
if (step > ALswap.size()) {
JOptionPane.showMessageDialog(this, "已到达最后一步", "提示",
JOptionPane.INFORMATION_MESSAGE);
step--;
return;
}
} else {
step--;
if (step <= 0) {
JOptionPane.showMessageDialog(this, "已经是第一步", "提示",
JOptionPane.INFORMATION_MESSAGE);
step++;
return;
}
}
y = 0;
paintTime = 0;
p = (PairIJ) ALswap.get(step - 1);
Ipivot = (Integer) ALpivot.get(step - 1);
pivot = Ipivot.intValue();
swapI = p.getI();
swapJ = p.getJ();
sort = (int[]) ALsort.get(step - 1);
distance = (swapJ - swapI) * 40;
}
public void setSleepTime(int sleepTime) {
this.sleepTime = sleepTime;
}
private int[] A, B;
int sleepTime = 200;
int higth, width;
int step = 0;
boolean last = false;
int swapI, swapJ;
int paintTime;
ArrayList ALpivot = new ArrayList();
int distance;
Integer Ipivot;
int pivot;
int y = 0;
PairIJ p;
Graphics g;
int[] sort = null;
ArrayList ALswap = new ArrayList();
Vector ALsort = new Vector();
}
JAVA实现的排序演示过程
4星 · 超过85%的资源 需积分: 9 167 浏览量
2009-05-19
23:15:32
上传
评论 3
收藏 36KB RAR 举报
i_kelven
- 粉丝: 0
- 资源: 3
最新资源
- The Experiment 2 of Engineering Electromagnetics.pdf
- Engineering-Electromagnetic-Theory-Lab-4.pdf
- Engineering electromagnetic theory-1.docx
- The Experiment 4 of Engineering Electromagnetics.pdf
- Engineering-Electromagnetic-Theory-Lab-3.pdf
- The Experiment 3 of Engineering Electromagnetics.pdf
- Engineering electromagnetic theory-2.docx
- tensorflow-gpu-2.7.0-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-rocm-2.12.1.570-cp310-cp310-manylinux2014-x86-64.whl
- tensorflow-rocm-2.12.1.570-cp39-cp39-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈