基数排序



public class Node {

int data;
int level;//在第几层
int pos;//在箱子的横向哪个位置
int flag;//为0表示在链中,否则为1 表是在list,即箱子中中,用于画图表明其位置
public Node(){
data=0;
level=pos=-1;
}
public Node(int n){
data=n;
level=-1;
pos=-1;
}
public String toString(){
return ""+data;

}
}


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.TimerTask;
import javax.swing.*;
public class radixsort extends JFrame implements ActionListener,Runnable{


JButton next,add,go,clear,random,base,stop,goon;
JTextField jt,//输入数据区域
jtNum,//产生随机数个数
jtMax,//产生随机数的最大值
jtBase;//输入排序的基数
Node[] node;
ArrayList[] list;//用来表示十个箱子,即十个位置
//list[0].add(1,1);
int winWidth,winHeight;
int n;//元素个数
int bits;//数字的最大位数
int level;//第几轮排序,最大值为bits
int index;//第几个元素排序,最大值为n
int reverse;//标志是箱子排序还是返回节点,若是正在进行则为0,否则为1
int radixx,radixy;//表明下一个要出的元素是第几个箱子的第几个位置
int randMax;//随机数最大值
int randNum;//产生随机数个数
int baseInt;//随机数基数默认为10
int stopFlag;
Thread thread;
public radixsort()
{
//初始化一些控件
super("基数排序");
this.setLayout(new BorderLayout());
list=new ArrayList[10];
for(int i=0;i<10;i++){
list[i]=new ArrayList();
}

next=new JButton("单步");
next.addActionListener(this);

go=new JButton("运行");
go.addActionListener(this);

stop=new JButton("暂停");
stop.addActionListener(this);

goon=new JButton("继续");
goon.addActionListener(this);
jt=new JTextField(15);
jt.setText("输入元素以逗号分离");

add=new JButton("创建");
add.addActionListener(this);

clear=new JButton("清除");
clear.addActionListener(this);

jtBase=new JTextField(4);
jtBase.setText("默认10");
base=new JButton("基数");
base.addActionListener(this);

jtNum=new JTextField(2);
jtNum.setText("位数");
jtMax=new JTextField(4);
jtMax.setText("最大值");
random=new JButton("产生随机数");
random.addActionListener(this);

JPanel south=new JPanel(new FlowLayout());
JPanel center=new JPanel();

south.add(jtBase);
south.add(base);
south.add(jtNum);
south.add(jtMax);
south.add(random);
south.add(jt,null);
south.add(add);
south.add(clear);
south.add(next);
south.add(go);
south.add(stop);
south.add(goon);
baseInt=10;
winWidth=1000;
winHeight=600;
this.getContentPane().add(south,BorderLayout.SOUTH);
this.setSize(winWidth,winHeight);
this.setVisib

le(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void paint(Graphics g){
super.paint(g);
winWidth=this.getWidth();
winHeight=this.getHeight();
int x=60,y=80,width=30,height=20;
if(node==null){

}
else for(int i=0;ithis.DrawNode(g,node[i],x,y,width,height);//画出未进入箱子的节点
if(i//g.drawLine(x+20, y+10, x+40, y+10);//元素之间的连线可有可无
}
x+=40;
}
paintBox(g);
}

public void DrawNode(Graphics g,Node n,int x,int y,int width,int height){
//用于画单个节点
if(n.flag!=0)return;//若是flag!=0则已经进入箱子中,不再画
g.drawRect(x, y, width, height);
g.drawString(""+n, x, y+20);
}

//求所有的数值最大位数,用于确定最终需要进行箱子排序的轮数
public int maxbit(Node data[])
{
int d=0;
int[] tmp=new int[data.length];
for(int i = 0 ;i < data.length;i++)
tmp[i]= data[i].data;
for(int i = 0;i < data.length;i++)
{
int p =1;
while(tmp[i]/baseInt > 0)
{
p++;
tmp[i] = tmp[i]/baseInt;
}
if(d < p) d = p;
}
return d;
}

//绘制排序的箱子
public void paintBox(Graphics g){
int x=100,y=winHeight-100,width=30,height=20;
g.drawString("第"+level+"轮排序",winWidth/2-50,winHeight-70);
for(int i=0;iif(list[i]!=null){
for(int j=0;jNode n=list[i].get(j);
if(n.flag!=0){
g.drawRect(i*width+x+i*(width-10), y-n.level*2*height, width, height);
g.drawString(""+n,n.pos*width+n.pos*(width-10)+x, y-n.level*2*height+height);
}
}
}
g.drawString("位置"+i,i*width+x+i*(width-10), y);
}
}

//用于单步执行的排序
public void stepSort(){
//分为两部分,一部分是将元素拿到箱子中,另一部分是要将箱子中的元素收回
//index表明是将第几个个元素进行操作
if(stopFlag==1){return;}
if(level>bits){JOptionPane.showMessageDialog(null, "已完成基数排序");return;}
if(reverse==0){

int pos=(int) ((node[index].data)/Math.pow(baseInt, level-1))%baseInt;//得到该轮排序的元素的数值
node[index].level=list[pos].size()+1;//设置元素的在箱子纵向的位置
node[index].pos=pos;//设置横向位置
node[index].flag=1;//更改标志表示已经在箱子中
list[pos].add(node[index]);//加到该位置箱子中
//System.out.println((int) ((node[index].data)/Math.pow(10, level-1))%10);//测试该元素在该层的的值
index++;//添加了一个元素索引加1
if(index>=n){//若index>=n则表明所有元素都已拿到箱子中 然后从箱子中回收
//level++;
//index=index/n;
reverse=1;
radixx=0;radixy=0;index=0;//进行初始化 初始化箱子中提取元素位置 以便回收
//System.out.println("reverse "+index);
}
repaint();
}
else{
//System.out.println("index "+index);

//if(index==0){System.out.println("index "+index);}
//System.out.println(" radixx "+radixx+" radixy "+radixy+" "+list[radixx]+" ");
//搜索箱子中的元素,将一个元素取出,然后退出循环
//radixx表示箱子位置索引 radixy表示该箱子里面的元素索引
//radixx<10表示还有箱子 该箱子为空或者要取出元素的索引小于箱子的元素的个数
while(radixx{
//System.out.println("here"+radixx+list[radixx].size()+" "+radixy);
if(list[radixx]!=null&&radixy/*
if(radixy>=list[radixx].size()){
radixx++;radixy=0;
list[radixx]=null;

}*/
//将元素取出插回队列中
node[index]=new Node(list[radixx].get(radixy).data);
node[index].flag=0;
list[radixx].remove(radixy);
//System.out.println("插入 "+node[index]);
index++;
if(index>=n){
//若是index>=n则全部元素回收完毕,进行下一轮箱子排序
//index/=n;
reverse=0;
radixx=0;
radixy=0;
index=0;
level++;
}
if(radixy>=list[radixx].size())
{
//若是该箱子的所有元素收回,则回收下一个箱子
radixy=0;
radixx++;
}
repaint();
break;
}
if(list[radixx]==null||list[radixx].size()==0){
//若是该箱子为空仍然回收下一个箱子
radixx++;
radixy=0;
//System.out.println("位置移动");
}
}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
radixsort rs=new radixsort();
}


public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="创建"){
if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
String elements;
elements=this.jt.getText();
if(elements.equals("")||elements==null||elements.equals("输入元素以逗号分离")){ JOptionPane.showMessageDialog(null,"请先输入元素");return;}
String ele[]=elements.trim().split(",");
node=new Node[ele.length];
n=ele.length;level=1;
for(int i=0;itry{
node[i]=new Node(Integer.parseInt(ele[i]));
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"输入元素格式不正确");return;
}
}
this.jt.setText("");
bits=maxbit(node);
repaint();
}
else if(e.getActionCommand()=="清除"){
//if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
//System.out.println("sadsadfght");
if(thread!=null||thread.isAlive()){thread.stop();thread=null;}
if(node!=null)
for(int i=0;inode[i]=null;
}
for(int i=0;i<10;i++){
list[i].removeAll(list[i]);
}
//System.out.println(" length "+node.length);
node=null;
bi

ts=0;
n=0;
level=0;//第几轮排序,最大值为bits
index=0;//第几个元素排序,最大值为n
reverse=0;//标志是箱子排序还是返回节点,若是正在进行则为0,否则为1
radixx=0;radixy=0;
repaint();
}
else if(e.getActionCommand()=="单步"){
if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
if(node==null){ JOptionPane.showMessageDialog(null,"请先创建元素");return;}
stepSort();
repaint();
}
else if(e.getActionCommand()=="运行"){
//try {
if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
if(node==null){ JOptionPane.showMessageDialog(null,"请先创建元素");return;}
//while(level<=bits)
else {
thread=new Thread(this);
thread.start();
this.repaint();
}
//} catch (InterruptedException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
//}
}
else if(e.getActionCommand()=="产生随机数"){
if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
try{
randNum=Integer.parseInt(jtNum.getText());
randMax=Integer.parseInt(jtMax.getText());
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "输入格式不正确");
}
node=new Node[randNum];
for(int i=0;inode[i]=new Node((int)(randMax*Math.random()));
}
n=randNum;level=1;
bits=maxbit(node);
repaint();
}
else if(e.getActionCommand()=="基数"){
if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
try{
baseInt=Integer.parseInt(jtBase.getText());
list=new ArrayList[baseInt];
for(int i=0;ilist[i]=new ArrayList();
}catch(Exception baseE){
JOptionPane.showMessageDialog(null, "输入有效数据");
}
repaint();
}
else if(e.getActionCommand()=="暂停"){
//if(thread!=null&&thread.isAlive()){JOptionPane.showMessageDialog(null, "程序正在运行!!");return;}
if(thread==null||!thread.isAlive()){JOptionPane.showMessageDialog(null, "程序未运行!!");return;}
//thread.yield();
//thread.suspend();
//thread.interrupt();
stopFlag=1;
/*try {
thread.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
//thread.
}
else if(e.getActionCommand()=="继续"){
//thread.notify();
stopFlag=0;
/*try {
thread.join();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
}
}

@Override
public void run() {
// TODO Auto-generated method stub
while(level<=bits){
try {
Thread.sleep(1000);
stepSort();
} catch (InterruptedException e) {
// TODO Auto-generated catch blo

ck
e.printStackTrace();
}
repaint();
}
JOptionPane.showMessageDialog(null, "基数排序完毕");
}
}



//6789,234,456,1,23,35,233,235

相关文档
最新文档