本程序是实现几种排序算法,通过提示输入数字来选择所要采用的算法。只贴了两个类上来,其他的只是继承InsertSort而已。Sort类是主类:package cn.sort;import javax.swing.JOptionPane;/**
* @(#)Sort.java 主类
*
* @author
* @No.200631000524
* @Class:5
* @version 1.00 2008/4/5
*/// import javax.swing.JOptionPane;
public class Sort {
/**
* @param argh
* 主函数用来调用各个子类的函数; 建立每一个类的对象,在主类直接调用排序方法实现排序;
*
*
*/
public static void main(String[] argh) {
InsertSort e = new InsertSort();
SeletionSort f = new SeletionSort();
BinaryInsertSort g = new BinaryInsertSort();
ShellSort h = new ShellSort();
BubbleSort i = new BubbleSort();
QuickSort j = new QuickSort();
HeapSort k = new HeapSort();
MergeSort l = new MergeSort();
RadixSort m = new RadixSort();
/**
* 建立对话框,用户选择需要的排序算法;
*/
String input = JOptionPane
.showInputDialog(
null,
"Input a number to select the algorithmic:\n"
+ "Insert Algorithmic:1 Selection Algorithmic:2 BinaryInsert Algorithmic:3\n"
+ "Shell Algorithmic:4 Bubble Algorithmic:5 Quick Algorithmic:6\n"
+ "Heap Algorithmic:7 Merge Algorithmic:8 Radix Algorithmict:9",
"Welcome to Algorithmic-Sort!",
JOptionPane.QUESTION_MESSAGE);
int choice = Integer.parseInt(input);
/**
* switch()选择算法;
*/
switch (choice) {
case 1: e.select();
break; case 2:
f.select();
break; case 3:
g.select();
break; case 4:
h.select();
break; case 5:
i.select();
break; case 6:
j.select();
break; case 7:
k.select();
break; case 8:
l.select();
break; case 9:
m.select();
break;
default:
System.out.println("WRONG INPUT!");
} }
}InsertSort实现插入排序并作为其他排序算法的基类:package cn.sort;import java.awt.BorderLayout;
import java.util.Random;import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;/**
* 选择排序 实现选择排序算法; 作为其他排序类的基类;
*
* @author 谢家裕
*
*/
public class InsertSort extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/*public InsertSort() {
setTitle("title");
setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
for(int i=0;i<=100;i++){
textArea.append("a");
}
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
}*/
/**
* select()方法实现数组的输入,并调用sort()排序算法;
* 被其他类所继承;
*/
public void select() {
/**
* 通过对话框输入数组的长度,数组的各个元素;
*/
String input2 = JOptionPane.showInputDialog(null, "Number=",
"Input the length of the Array", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(input2);
int[] sortArray = new int[number];
Random generator = new Random();
/**
* 产生小于100000000的随机整数;
*/
for (int i = 0; i < sortArray.length; i++) {
sortArray[i] = generator.nextInt(100000000);
}
sort(sortArray);
} /**
* 选择排序算法的实现;
*
* @param a
*/
public void sort(int[] a) {
/*
* 获得执行for循环之前系统当前时间,以nanosecond毫微秒记;
*/
long begin = System.nanoTime();
for (int index = 1; index < a.length; index++) {
temp = a[index];
for (j = index; j > 0; j--) {
if (a[j - 1] > temp) {
a[j] = a[j - 1];
} else
break;
}
a[j] = temp; }
/*
* 获得执行for循环之后系统当前时间,以nanosecond毫微秒记;
*/
long end = System.nanoTime();
long k = end - begin;
String efficiency = new String("O(1/4(n^2+5n-6))");
print(a, k, efficiency);
} /**
* 输出排序的结果;
* 被其他类所继承;
* @param a
*/
public void print(int[] a, long k, String efficiency) {
String out = new String();
for (int i = 0; i < a.length; i++)
// System.out.print(a[i] + " ");
if (i == 0) {
out = String.valueOf(a[i]);
} else {
out = out + " " + a[i];
if (i % 30 == 0)
out += "\n";
}
/*
* 输出排序后的元素,所采用算法的效率以及花费时间(nanosecond)毫微秒;
*/
JOptionPane.showMessageDialog(null, "The sort of this Array is:\n"
+ out + "\nThe efficiency is:" + efficiency + ".\n"
+ "The time of this algorithmic has spent is: " + k + " nanasecond.",
"Answer", JOptionPane.INFORMATION_MESSAGE);
InsertSort e=new InsertSort();
final int x=300;
final int y=300;
e.setTitle("title");
e.setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
textArea.append("a");//目的是想输出以下两句,但是显示了窗口里面什么也没有,也没有滚动条。@@@@@@@@@@@@@@@@@@@@@@@@@@
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
} private int temp;
private int j;
}
* @(#)Sort.java 主类
*
* @author
* @No.200631000524
* @Class:5
* @version 1.00 2008/4/5
*/// import javax.swing.JOptionPane;
public class Sort {
/**
* @param argh
* 主函数用来调用各个子类的函数; 建立每一个类的对象,在主类直接调用排序方法实现排序;
*
*
*/
public static void main(String[] argh) {
InsertSort e = new InsertSort();
SeletionSort f = new SeletionSort();
BinaryInsertSort g = new BinaryInsertSort();
ShellSort h = new ShellSort();
BubbleSort i = new BubbleSort();
QuickSort j = new QuickSort();
HeapSort k = new HeapSort();
MergeSort l = new MergeSort();
RadixSort m = new RadixSort();
/**
* 建立对话框,用户选择需要的排序算法;
*/
String input = JOptionPane
.showInputDialog(
null,
"Input a number to select the algorithmic:\n"
+ "Insert Algorithmic:1 Selection Algorithmic:2 BinaryInsert Algorithmic:3\n"
+ "Shell Algorithmic:4 Bubble Algorithmic:5 Quick Algorithmic:6\n"
+ "Heap Algorithmic:7 Merge Algorithmic:8 Radix Algorithmict:9",
"Welcome to Algorithmic-Sort!",
JOptionPane.QUESTION_MESSAGE);
int choice = Integer.parseInt(input);
/**
* switch()选择算法;
*/
switch (choice) {
case 1: e.select();
break; case 2:
f.select();
break; case 3:
g.select();
break; case 4:
h.select();
break; case 5:
i.select();
break; case 6:
j.select();
break; case 7:
k.select();
break; case 8:
l.select();
break; case 9:
m.select();
break;
default:
System.out.println("WRONG INPUT!");
} }
}InsertSort实现插入排序并作为其他排序算法的基类:package cn.sort;import java.awt.BorderLayout;
import java.util.Random;import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;/**
* 选择排序 实现选择排序算法; 作为其他排序类的基类;
*
* @author 谢家裕
*
*/
public class InsertSort extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/*public InsertSort() {
setTitle("title");
setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
for(int i=0;i<=100;i++){
textArea.append("a");
}
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
}*/
/**
* select()方法实现数组的输入,并调用sort()排序算法;
* 被其他类所继承;
*/
public void select() {
/**
* 通过对话框输入数组的长度,数组的各个元素;
*/
String input2 = JOptionPane.showInputDialog(null, "Number=",
"Input the length of the Array", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(input2);
int[] sortArray = new int[number];
Random generator = new Random();
/**
* 产生小于100000000的随机整数;
*/
for (int i = 0; i < sortArray.length; i++) {
sortArray[i] = generator.nextInt(100000000);
}
sort(sortArray);
} /**
* 选择排序算法的实现;
*
* @param a
*/
public void sort(int[] a) {
/*
* 获得执行for循环之前系统当前时间,以nanosecond毫微秒记;
*/
long begin = System.nanoTime();
for (int index = 1; index < a.length; index++) {
temp = a[index];
for (j = index; j > 0; j--) {
if (a[j - 1] > temp) {
a[j] = a[j - 1];
} else
break;
}
a[j] = temp; }
/*
* 获得执行for循环之后系统当前时间,以nanosecond毫微秒记;
*/
long end = System.nanoTime();
long k = end - begin;
String efficiency = new String("O(1/4(n^2+5n-6))");
print(a, k, efficiency);
} /**
* 输出排序的结果;
* 被其他类所继承;
* @param a
*/
public void print(int[] a, long k, String efficiency) {
String out = new String();
for (int i = 0; i < a.length; i++)
// System.out.print(a[i] + " ");
if (i == 0) {
out = String.valueOf(a[i]);
} else {
out = out + " " + a[i];
if (i % 30 == 0)
out += "\n";
}
/*
* 输出排序后的元素,所采用算法的效率以及花费时间(nanosecond)毫微秒;
*/
JOptionPane.showMessageDialog(null, "The sort of this Array is:\n"
+ out + "\nThe efficiency is:" + efficiency + ".\n"
+ "The time of this algorithmic has spent is: " + k + " nanasecond.",
"Answer", JOptionPane.INFORMATION_MESSAGE);
InsertSort e=new InsertSort();
final int x=300;
final int y=300;
e.setTitle("title");
e.setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
textArea.append("a");//目的是想输出以下两句,但是显示了窗口里面什么也没有,也没有滚动条。@@@@@@@@@@@@@@@@@@@@@@@@@@
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true);
} private int temp;
private int j;
}
解决方案 »
- 急!!各位大牛帮忙看一个问题:java.net.UnknownHostException: www.xxx.com
- SE做学生成绩管理系统的问题
- 求解 大侠帮帮忙
- 关于一个字符串后缀的删除问题
- 急!困扰了很久得问题!在多线程程序中各自独立线程的线程为什么会死锁?
- 最小集合的java支撑环境(运行环境?)~
- 请大家注意SQL注入----------小心地保护自己的数据库。那么。。
- 求助,中文换行成乱码,如何解决.
- 如何防止按浏览器右上角的"x" 来关闭浏览器
- Hashtable<String, String> 存储的内容会颠倒吗?
- 以前学习时遇到的,很简单的一个问题,希望大家指教一下
- 关于正则式语法问题
InsertSort e=new InsertSort();
final int x=300;
final int y=300;
e.setTitle("title");
e.setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
textArea.append("a");//目的是想输出以下两句,但是显示了窗口里面什么也没有,也没有滚动条。@@@@@@@@@@@@@@@@@@@@@@@@@@
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setVisible(true); 以上不需要再创建一个InsertSort e=new InsertSort();直接用this
//InsertSort e=new InsertSort();
final int x=300;
final int y=300;
this.setTitle("title");
this.setSize(x, y);
JTextArea textArea = new JTextArea(8,40);
textArea.append("a");//目的是想输出以下两句,但是显示了窗口里面什么也没有,也没有滚动条。@@@@@@@@@@@@@@@@@@@@@@@@@@
textArea.append("I can do it!");
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
然后在 main 中 public static void main(String[] argh) {
InsertSort e = new InsertSort();
又生成了一个 InsertSort 对象你把 显示的 东西都放在 print 的哪个对象里 然后 又用 main的哪个对象.