第三周作业——冒泡排序和归并排序

package week_3;


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.List;

public class DataSort {
	static String srcPath = "c:\\largeW.txt";
	static String bubbleSortPath = "largeW_bubble.txt";
	static String mergeSortPath = "largeW_merge.txt";
	
	public static void main(String args[]) {
		try{
			File file = new File(srcPath);
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader inputReader = new InputStreamReader(fis);
			BufferedReader bufferedReader = new BufferedReader(inputReader);
			String lineData = "";
			int num = 0;
			List<Integer> list = new ArrayList<Integer>();   //建立一个列表数组
			    
			while((lineData=bufferedReader.readLine()) != null){         //判断是否是有数据
				
				num = num + 1;
				list.add(Integer.parseInt(lineData.trim()));   //将数据加到数组里
			}
			Integer[] lineDataArray = list.toArray(new Integer[0]);
			//开始冒泡排序
			
			long timeStart = System.currentTimeMillis();    //开始冒泡排序
			BubbleSort(lineDataArray);      //冒泡排序
			long timeEnd = System.currentTimeMillis();   //冒泡排序结束
			System.out.println("冒泡排序所花的时间是"+(timeEnd-timeStart));
			ResultSortWriteTxt(lineDataArray,0);
		}catch(Exception e) {
			
		}
	}
	
	//把排序结果写到结果文本文件
	public static void ResultSortWriteTxt(Integer[] lineDataArray,int n) throws IOException{
		File file = null;
		if (n==0) {          //n=0代表是冒泡排序
			file = new File(bubbleSortPath);
		}else if(n==1) {      //n=1代表是归并排序
			file = new File(mergeSortPath);
		}
		
		if(!file.exists()) {  //如果文件已经排序好了
			file.createNewFile();
			
		}
		FileOutputStream fos= new FileOutputStream(file);
		OutputStreamWriter osw =  new OutputStreamWriter(fos,"gb2312");
		BufferedWriter writer = new BufferedWriter(osw);
		for(int i=0;i<lineDataArray.length;i++) {
			writer.write(lineDataArray[i].toString()+"\r\n");
			writer.flush();
		}
		
		fos.close();
		osw.close();
		writer.close();
		
	}
	
	//下面是实现冒泡排序,两两对比,大的排在前面
	public static void BubbleSort(Integer[] Array) throws IOException{
		Integer[] lineDataArray =  (Integer[]) Array;
		int temp = 0;
		int  len = lineDataArray.length;        //取文件数据的长度
		for(int i=0;i<len+1;i++) {
			for(int j=1;j<len-i-1;j++) {
				if(lineDataArray[i]<lineDataArray[j]){
					temp = lineDataArray[i];
					lineDataArray[i] = lineDataArray[j];
					lineDataArray[j] = temp;
				}
			}
			//System.out.println(lineDataArray[i]);
		}
		
	}
	
	//下面是实现归并排序
	public static void mergeSort(int[] src,int[] des){     
		//从下标1开始存放待排序数组
		int len = 1, n=src.length-1;
		while(len<n){
			MergePass(src,des,n,len);    //待排序序列从数组src中传到des中
			len = 2*len;
			MergePass(des,src,n,len);    //待排序序列从数组des中传到src中
		}
	}
	
	
	//一趟归并排序算法
	public static void MergePass(int[] src,int[] des,int n,int h) {
		int i=1;
		while(i<=(n-2*h+1)) {
			Merge(src,des,i,i+h-1,i+2*h-1);
			i+=2*h;
		}
		if(i<(n-h+1))
			Merge(src,des,i,i+h-1,n);
		else {
			while(i<=n) {
				des[i] = src[i];
				i++;
			}
		}
	}
	
	
	//一次归并算法
	public static void Merge(int[] src,int[] des,int s,int m,int t) {
		int i,j,k;
		i=s; j = m+1;  k = s;
		while(i<=m && j<=t) {
			if(src[i]<=src[j]){
				src[k++] = src[i++] ;
			}else{
				src[k++] = src[j++] ;
			}
		}
		
		if(i<=m)
			while(i<=m){
				des[k++] = src[i++];
			}
		else{
			while(j<=t) {
				des[k++] = src[j++];
			}
		}
	}
	
	
	
}
未完待续
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值