第三周作业

来源:互联网 发布:王侯将相宁有种乎全文 编辑:程序博客网 时间:2024/06/07 09:22
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.Date;import java.util.List;public class bb {static String path="F:/xiao.txt";static String bubbleSortpath="src/xiao_bubble.txt";//冒泡排序结果存放位置static String mergeSortpath="src/xiao_merge.txt";//归并排序结果存放位置public static void main(String[] args) {try {File file=new File(path);FileInputStream fin=new FileInputStream(file);InputStreamReader inread=new InputStreamReader(fin, "gb2312");BufferedReader buffread=new BufferedReader(inread); String nums="";int num=0;List<Integer> list=new ArrayList<Integer>();while((nums=buffread.readLine())!=null){num=num+1;list.add(Integer.parseInt(nums.trim()));}Integer[] numArray=(Integer[])list.toArray(new Integer[num]);long timeStart=System.currentTimeMillis();//开始冒泡排序计时bubbleSort(numArray);long timeEnd=System.currentTimeMillis();//冒泡排序计时结束 System.out.println("冒泡排序为:");ResultSortWriteTxt(numArray,0);//冒泡排序结果写到文本文件numArray=(Integer[])list.toArray(new Integer[num]);Integer[] num0=new Integer[numArray.length];timeStart=System.currentTimeMillis();//开始归并排序计时Integer[] result  = mergeSort(numArray, 0, numArray.length - 1, num0); timeEnd=System.currentTimeMillis();//归并排序计时结束System.out.println("归并排序为:");ResultSortWriteTxt(result,1);//把归并排序的结果写到文本文件System.out.println("冒泡排序所花费的时间是:"+(timeEnd-timeStart)+"毫秒");System.out.println("归并排序所花费的时间是:"+(timeEnd-timeStart)+"毫秒");System.out.println("文本中总共有:"+num+"条数据");} catch (Exception e) {e.printStackTrace();}}public static void bubbleSort(Integer[] Array) throws IOException{//冒泡排序Integer[] numArray=(Integer[]) Array;int k=0; for(int i=0;i<numArray.length;i++){ for(int j=i+1;j<numArray.length;j++){ if(numArray[i]>numArray[j]){k=numArray[i];numArray[i]=numArray[j];numArray[j]=k; } } }}public static void  ResultSortWriteTxt(Integer[] numArray,int whichSort) throws IOException{ File file=null; if(whichSort==0){//冒泡排序是表示0 file =new File(bubbleSortpath); }else if(whichSort==1){//归并排序是表示1 file =new File(mergeSortpath); } if(!file.exists()){ file.createNewFile(); } FileOutputStream fout=new FileOutputStream(file); BufferedWriter buffwrite=new BufferedWriter(new OutputStreamWriter(fout,"gb2312")); for(int i=0;i<numArray.length;i++){ System.out.println(numArray[i]); buffwrite.write(numArray[i].toString()+"\r\n"); buffwrite.flush(); //每写入一次字节流数据刷新一次 } fout.close(); buffwrite.close();} private static Integer[] mergeSort(Integer[] num, int s, int t, Integer[] num0) {//归并排序    int k;    Integer[] num1 = new Integer[t + 1];    if (s == t){     num0[s] = num[s];    }else {     k = (s + t) / 2;     mergeSort(num, s, k, num1);     mergeSort(num, k + 1, t, num1);     merg(num1, s, k, t, num0);    }    return num0; } //合并有序表 private static void merg(Integer[] num1, int l, int m, int n, Integer[] num0) { int i, j, k; i = l; j = m + 1; k = l; while (i <= m && j <= n) { if (num1[i] < num1[j]){      num0[k++] = num1[i++]; }else{      num0[k++] = num1[j++];     }  } while (i <= m) {     num0[k++] = num1[i++];  } while (j <= n) {     num0[k++] = num1[j++]; }} }


 

 

0 0