第三周作业-冒泡排序与归并排序

package test;

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  sortnum{
	static String path="C:/Users/玲/Desktop/largeW.txt";
	static String bubbleSortpath="C:/Users/玲/Desktop/bubbleNum.txt";	//冒泡排序结果存放位置
	static String mergeSortpath="C:/Users/玲/Desktop/mergeNum.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
   
   
    
     list=new ArrayList
    
    
     
     ();
			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("冒泡排序所花费的时间是:"+(timeEnd-timeStart)+"毫秒");
			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("归并排序所花费的时间是:"+(timeEnd-timeStart)+"毫秒");
			ResultSortWriteTxt(result,1);//把归并排序的结果写到文本文件
			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[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
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值