📄 sort.java
字号:
import java.io.*;
final class model//模板类
{
private long _time=0;//运行时间
private int _changecount=0;//交换次数
private int _movecount=0;//移动次数
private int _comparecount=0;//比较次数
model()//重写构造函数
{
//time = changecount = movecount = comparecount = 0;
}
public long time()
{
return _time;//返回时间
}
public void settime(long time1,long time2)
{
_time=time2-time1;//设置时间
}
public int changecount()
{
return _changecount;//返回交换次数
}
public void addchangecount()
{
_changecount++;//交换次数加1
}
public int movecount()
{
return _movecount;//返回移动次数
}
public void addmovecount()
{
_movecount++;//移动次数加1
}
public int comparecount()
{
return _comparecount;//返回比较次数
}
public void addcomparecount()
{
_comparecount++;//比较次数加1
}
protected void toString(int i)
{
System.out.println(i);
}
protected void toString(long i)
{
System.out.println(i);
}
}
interface clock//实现计时的接口
{
public abstract long time();
}
class Tclock implements clock//计时接口的实现
{
public long time()
{
return System.currentTimeMillis();
}
}
abstract class operate //定义所有操作的抽象类
{
private model mod;
protected int array[];
private clock t = new Tclock();
final public void sort()
{
mod = new model();
//long time1 = System.currentTimeMillis();
long time1 = t.time();
dosort();
//long time2 = System.currentTimeMillis();
long time2 = t.time();
mod.settime(time1,time2);
print();
}
final protected void change(int i, int j)//交换
{
int x = array[i];
array[i] = array[j];
array[j] = x;
mod.addchangecount();
}
final protected void move(int i, int j)//移动
{
int x = array[j];
while (j > i)
{
array[j] = array[j - 1];
j--;
mod.addmovecount();
}
array[i] = x;
}
final protected boolean compare(int i, int j)//比较
{
mod.addcomparecount();
return (array[i] > array[j]) ? true : false;
}
public void print()//输出数据函数
{
System.out.println("排序后的数组为:");
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
System.out.println("比较次数:");
mod.toString(mod.comparecount());
System.out.println("移动次数:");
mod.toString(mod.movecount());
System.out.println("交换次数:");
mod.toString(mod.changecount());
System.out.println("运行时间:");
mod.toString(mod.time());
}
abstract void dosort();
}
final class bubble extends operate//冒泡排序
{
public bubble(int[] arr)//构造函数,初始化array
{
array = arr;
}
void dosort()//实现dosort()方法
{
for (int i = array.length; i > 0; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (compare(j, j + 1))
{
change(j, j + 1);
}
}
}
}
}
final class select extends operate//选择排序
{
public select(int[] arr)//构造函数,初始化array
{
array = arr;
}
void dosort()//实现dosort()方法
{
for (int i = 0; i < array.length - 1; i++)
{
int k = i;
for (int j = k + 1; j < array.length; j++)
{
if (compare(k, j))
{
k = j;
}
}
if (k != i)
{
change(i, k);
}
}
}
}
final class insert extends operate//插入排序
{
public insert(int[] arr)//构造函数,初始化array
{
array = arr;
}
void dosort()//实现dosort()方法
{
for (int i = 1; i < array.length; i++)
{
int j = i;
while (j > 0 && compare(j - 1, i))
{
j--;
}
move(j, i);
}
}
}
public class sort
{
protected static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//定义输入流对象
protected static cin docin = new cin();
protected static void print(String s)//简化输出方法
{
System.out.println(s);
}
public static void main(String args[])
{
int array[]=null;
boolean flag = true;
while (flag)
{
print("请选择输入数组的方法:1、从键盘输入;其它、从文件读入");
try
{
if(Integer.parseInt(reader.readLine())==1)
{
array = docin.DoCinFromScreen();
}
else
{
array = docin.DoCinFromText();
}
}
catch(Exception ex)
{
}
boolean flag2 = true;
while (flag2)
{
flag2 = false;
print("请选择要排序的方法:1、冒泡排序;2、选择排序;3、插入排序");
int way = 0;
try
{
way = Integer.parseInt(reader.readLine());
}
catch (Exception ex)
{
}
operate oper;//声明排序对象
if (way == 1)
{
oper = new bubble(array);
oper.sort();
}
else if (way == 2)
{
oper = new select(array);
oper.sort();
}
else if (way == 3)
{
oper = new insert(array);
oper.sort();
}
else
{
print("输入错误,请重新选择");
flag2 = true;
}
}
print("是否重来一遍?1、是;其它、否");
try
{
if (Integer.parseInt(reader.readLine()) == 1)
continue;
else
flag = false;
}
catch (Exception ex)
{
}
}
}
}
class cin extends sort
{
private int array[];
//从键盘输入数组
protected int[] DoCinFromScreen()
{
int len = 0;
System.out.print("请输入要排序的数组长度:");
try
{
len = Integer.parseInt(reader.readLine());
}
catch (Exception ex)
{
}
array=new int[len];
print("请输入" + len + "个数,用回车隔开");
try
{
for (int i = 0; i < len; i++)
{
array[i] = Integer.parseInt(reader.readLine());//初始化array
}
}
catch (Exception ex)
{
}
return array;
}
//从text文件读入数组
protected int[] DoCinFromText()
{
FileReader FR=null;
try
{
FR = new FileReader("test.txt");
}
catch(Exception ex)
{
}
int i=0;
try
{
i=FR.read();
}
catch(Exception ex)
{
}
String str="";
while (i != -1)
{
try
{
if (i < 48 || i > 57)
{
str +=" ";
}
else
{
str += String.valueOf(i - 48);
}
i = FR.read();
}
catch (Exception ex)
{
}
}
//char a[]={' '};
String string[] = str.split(" ");
array=new int[string.length];
for (int s = 0; s < string.length; s++)
{
array[s] = Integer.parseInt(string[s]);
}
return array;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -