import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class z {
public static void main(String[] args) throws IOException {
// 文件绝对路径
FileReader fr = new FileReader(
"F:\\补丁\\作业\\largeW.txt");
BufferedReader br = new BufferedReader(fr);
List<String> list = new ArrayList<String>();
String rLine = "";
while ((rLine = br.readLine()) != null) {
list.add(rLine);
}
int number[] = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
number[i] = Integer.parseInt(((String) list.get(i)).trim());// 进行逐个转换,剔除空格
}
br.close();
long startTime = System.currentTimeMillis(); // 获取开始时间
// 开始冒泡排序
bubbleSort(number);
long endTime = System.currentTimeMillis(); // 获取结束时间
//输出排序后的数据
for (int i = 0; i < list.size(); i++) {
System.out.println(number[i]);
}
//将数组内数据写入到TXT文件中
FileWriter fileWriter = new FileWriter(
"F:\\补丁\\作业\\largeW_bubble.txt");
for (int i = 0; i < number.length; i++) {
fileWriter.write(String.valueOf(number[i]) + " ");
}
fileWriter.flush();
fileWriter.close();
System.out.println("冒泡排序运行时间: " + (endTime - startTime) + "ms");
}
public static void bubbleSort(int[] data){
for (int i = 0; i < data.length; i++)
for (int j = 0; j < data.length; j++)
if (data[i] < data[j]) {
int t;
t = data[i];
data[i] = data[j];
data[j] = t;
}
}
}
第三周作业——冒泡排序
最新推荐文章于 2023-11-16 12:48:48 发布