📄 arraybub.java
字号:
import java.io.*;
public class ArrayBub {
private long[] a; // 存储待排序数据
private int nElems; // 待排数据的个数
// -----------------------------------------
// 构造函数
public ArrayBub(String fileName) {
// 初始化数组
a = new long[1000];
nElems = 0;
int i;
FileReader fin;
BufferedReader buf;
// 打开文件
try {
fin = new FileReader(fileName);
buf = new BufferedReader(fin);
} catch (FileNotFoundException e) {
System.out.println("找不到文件 " + fileName);
return;
}
// 读取文件上的数据
try {
String Read = buf.readLine();
long temp;
if (Read != null) {
String[] strNumber = Read.split(" ");
for (int j = 0; j < strNumber.length; j++) {
insert((long) Integer.parseInt(strNumber[j]));
}
}
// 关闭文件
fin.close();
buf.close();
} catch (IOException e) {
System.out.println("读取文件错误");
}
}
// -----------------------------------------
// 往数组中插入数据
public void insert(long value) {
a[nElems] = value;
nElems++;
}
// -----------------------------------------
// 在控制台输出数据
public void display() {
for (int j = 0; j < nElems; j++)
System.out.print(a[j] + " ");
System.out.println();
}
// -------------------------------------------
// 使用冒泡算法对数据进行排序
public void bubbleSort() {
int out, in;
for (out = nElems - 1; out > 1; out--)
for (in = 0; in < out; in++)
if (a[in] > a[in + 1])
swap(in, in + 1);
}
// 交换两个元素
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -