编程要求:排序。对文件 largeW.txt(下载链接)中的数据,编程实现冒泡排序(方法名:bubbleSort) 与 归并排序(mergeSort),把排序后的结果分别保存到largeW_bubble.txt 和 largeW_merge.txt 中,把两种排序结果的运行时间输出到屏幕中(发博文时把运行时间的截图作为运行结果提交)。
/**
* 程序功能:冒泡排序的实现及文件的IO回顾
* 作者:冯得晟
* 时间:2014.03.27
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TwoSorts {
static Integer[] ArrayList;
static int num;
static String srcpath="H:\\学习\\大三\\大三 下\\算法概论\\课后作业\\largeW.txt";
static String bubpath="H:\\学习\\大三\\大三 下\\算法概论\\课后作业\\largeW_bubble.txt";
static String merpath="H:\\学习\\大三\\大三 下\\算法概论\\课后作业\\largeW_merge.txt";
void readFileByChars(String fileName) {
try{
FileInputStream fis=new FileInputStream(fileName);
InputStreamReader dis=new InputStreamReader(fis);
BufferedReader reader=new BufferedReader(dis);
String s;
int a=0;
List <Integer> ls=new ArrayList<Integer>();
while((s=reader.readLine())!=null){
a=a++;
ls.add(Integer.parseInt(s.trim()));
}
ArrayList=(Integer[])ls.toArray(new Integer[a]);
num=ArrayList.length;
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}
void WirteFile(String fileName){
try{
File file=null;
file =new File(fileName);
FileOutputStream fos=new FileOutputStream(file);
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter writer=new BufferedWriter(osw);
for(int i=0;i<ArrayList.length;i++){
writer.write(ArrayList[i].toString()+"\r\n");
writer.flush();
}
System.out.println("存储完成");
}catch(IOException e){
e.printStackTrace();
}
}
void BubbleSort(Integer r[],int n){
System.out.println("待排序的数字个数为:"+n);
int exchange=n;
for(int i=0;i<r.length;i++){
for(int j=0;j<r.length-i-1;j++){
if(r[j]>r[j+1]){
exchange=r[j];
r[j]=r[j+1];
r[j+1]=exchange;
}
}
}
String intArrayString = Arrays.toString(r);
System.out.println("冒泡排序结果:"+intArrayString);
}
Integer[] mergeSort(Integer[] num3, int s, int t, Integer[] num1) {
int p;
Integer[] num2 = new Integer[t + 1];
if (s == t)
num1[s] = num3[s];
else {
p = (s + t) / 2;
mergeSort(num3, s, p, num2);
mergeSort(num3, p + 1, t, num2);
merg(num2, s, p, t, num1);
}
return num1;
}
static void merg(Integer[] num2, int l, int m, int n, Integer[] num1) {
int i, j, k;
i = l;
j = m + 1;
k = l;
while (i <= m && j <= n) {
if (num2[i] < num2[j])
num1[k++] = num2[i++];
else {
num1[k++] = num2[j++];
}
}
while (i <= m) {
num1[k++] = num2[i++];
}
while (j <= n) {
num1[k++] = num2[j++];
}
}
public static void main(String[] args){
TwoSorts ts=new TwoSorts();
ts.readFileByChars(srcpath);
long timeStart=System.currentTimeMillis();//开始计时
ts.BubbleSort(ArrayList, num);
long timeEnd=System.currentTimeMillis();//计时结束
System.out.println("冒泡排序所花费的时间是:"+(timeEnd-timeStart)/1000+"秒");
ts.WirteFile(bubpath);
Integer[] num1=new Integer[num];
System.out.println("待排序的数字个数为:"+num);
long TimeStart=System.currentTimeMillis();//开始计时
ts.mergeSort(ArrayList,0,num-1, num1);
long TimeEnd=System.currentTimeMillis();//计时结束
System.out.println("归并排序所花费的时间是:"+(TimeEnd-TimeStart)/1000+"秒");
ts.WirteFile(merpath);
}
}
运行结果:
冒泡排序结果下载地址:http://download.csdn.net/detail/bbyyo/7109711