此冒泡排序在我的电脑上运行花费了1443秒,可以在你们的电脑上试一下。。。。
import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
ArrayList arraylist = new ArrayList();
try {
long startTime, endTime;//冒泡开始的时间与结束的时间;
int readNum;//读到的字符串转化成整形
FileReader f = new FileReader(“C:/Users/Y470/Desktop/largeW.txt”);
BufferedReader br = new BufferedReader(f);
String line = br.readLine();
while (line != null) {
readNum = Integer.parseInt(line.trim());
arraylist.add(readNum);
line = br.readLine();
}
int array[] = new int[arraylist.size()];//转化成整型数组
for (int i = 0; i < array.length; i++) {
array[i] = (Integer) arraylist.get(i);
}
startTime = System.currentTimeMillis();//得到冒泡开始的时间
System.out.println(startTime);
BubbleSort(array);// 调用冒泡排序
endTime = System.currentTimeMillis();//得到冒泡结束的时间
System.out.println(endTime);
System.out.println((endTime – startTime) / 1000 + “秒”);// 冒泡排序花费的时间
//排完序写入largeR.txt中
FileWriter w = new FileWriter(“C:/Users/Y470/Desktop/largeR.txt”);
BufferedWriter bw = new BufferedWriter(w);
String str;
for (int i = 0; i < array.length; i++) {
str = String.valueOf(array[i]);
bw.write(str);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 冒泡排序
public static void BubbleSort(int[] arr) {
int change;
for (int i = 1; i <= arr.length – 1; i++) {
for (int j = 0; j < arr.length – i; j++) {
if (arr[j] > arr[j + 1]) {
change = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = change;
}
}
}
}
}