THE TWENTY-THIRD DAY

    今天星期六,参加培训的第二十三天,一如往常,早起教室开始看书,结果呢,总之今天很不在状态,一直相抽武则天,但是就是抽不到,很纠结,然后就一直不在于状态,没有办法的事情。

    实在静不下心来,然后就出去买了一盒烟,抽一颗,然后就开始比着书敲代码,行吧,代码如下:

    堆排序:(二叉树)

package am;

import java.util.ArrayList;

public class Heap<E extends Comparable> {
    private ArrayList<E> list = new ArrayList<E>();
    public Heap() {}
    public Heap(E[] objects) {
    	for(int i= 0 ;i<objects.length;i++) {
    		add(objects[i]);
    	}
    }
    public void add(E newObject) {
    	list.add(newObject);
    	int currentIndex = list.size()-1;
    	while(currentIndex>0) {
    		int parentIndex = (currentIndex -1)/2;
    		if(list.get(currentIndex).compareTo(
    				list.get(parentIndex))>0) {
    			E temp = list.get(currentIndex);
    			list.set(currentIndex, list.get(parentIndex));
    			list.set(parentIndex, temp);
    		}else {
    			break;
    		}
    			currentIndex = parentIndex;
    	}
    }
    
    public E remove() {
    	if(list.size()==0) {
    		return null;
    	}
    	E removeObject = list.get(0);
    	list.set(0, list.get(list.size()-1));
    	list.remove(list.size()-1);
    	int currentIndex=0;
    	while(currentIndex<list.size()) {
    		int leftChildIndex = 2*currentIndex+1;
    		int rightChildIndex = 2*currentIndex+2;
    		if(leftChildIndex>=list.size()) {
    			break;
    		}
    		int maxIndex = leftChildIndex;
    		if(rightChildIndex<list.size()) {
    			if(list.get(maxIndex).compareTo(list.get(rightChildIndex))<0) {
    				maxIndex = rightChildIndex;
    			}
    		}
    		if(list.get(currentIndex).compareTo(list.get(maxIndex))<0) {
    			E temp = list.get(maxIndex);
    			list.set(maxIndex, list.get(currentIndex));
    			list.set(currentIndex, temp);
    			currentIndex=maxIndex;
    		} else {
    			break;
    		}
    	}
    	return removeObject;
    }
    public int getSize() {
    	return list.size();
    }
}

然后就是外部排序:(针对大数据:内存不够)

package am;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class SortLargeFile {
    public static final int MAX_ARRAY_SIZE = 100;
    public static final int BUFFER_SIZE = 100;
    public static void main(String[] args) throws Exception {
    	createLargeFile();
		sort("largedata.dat","sortedfile.dat");
		displayFile("sortedfile.dat");
	}
    private static void createLargeFile() throws IOException {
    	DataOutputStream output = new DataOutputStream(
    			new BufferedOutputStream(new FileOutputStream("largedata.dat")));
    	for(int i= 0;i<256;i++) {
    		output.writeInt((int)(Math.random()*10000));
    	}
    	output.close();
    	DataInputStream input = new DataInputStream(new FileInputStream("largedata.dat"));
      for(int i=0;i<256;i++) {
    	  System.out.println(input.readInt());
      }   	
      input.close();
    }
    public static void sort(String sourcefile,String targetfile) throws Exception {
    	int numberOfSegments = 
    			initializeSegments(MAX_ARRAY_SIZE,sourcefile,"f1.dat");
    	merge(numberOfSegments,MAX_ARRAY_SIZE,"f1.dat","f2.dat","f3.dat",targetfile);
    }
    private static int initializeSegments(int segmentSize,String originalFile,String f1) throws Exception {
    	int[] list = new int[segmentSize];
    	DataInputStream input = new DataInputStream(
    			new BufferedInputStream(new FileInputStream(originalFile)) );
    	DataOutputStream output = new DataOutputStream(
    			new BufferedOutputStream(new FileOutputStream(f1)));
    	int numberOfSegments = 0;
    	while(input.available()>0) {
    		numberOfSegments++;
    		int i = 0;
    		for(;input.available()>0&&i<segmentSize;i++) {
    			list[i] = input.readInt();
    		}
    		Arrays.sort(list,0,i);
    		for(int j= 0;j<i;j++) {
    			output.writeInt(list[j]);
    		}
    	}
    	input.close();
    	output.close();
    	return numberOfSegments;
    }
    public static void merge (int numberOfSegments,int segmentSize,
    		String f1,String f2,String f3,String targetfile) throws Exception {
    	if(numberOfSegments>1) {
    		mergeOneStep(numberOfSegments,segmentSize,f1,f2,f3);
    		merge((numberOfSegments+1)/2,segmentSize*2,f3,f1,f2,targetfile);
    	} else {
    		File sortedFile = new File(targetfile);
    		if(sortedFile.exists()) {
    			sortedFile.delete();
    		}
    		new File(f1).renameTo(sortedFile);
    	}
    }
    private static void mergeOneStep(int numberOfSegments,int segmentSize,String f1,
    		String f2,String f3) throws Exception {
    	DataInputStream f1Input = new DataInputStream(
    			new BufferedInputStream(new FileInputStream(f1),BUFFER_SIZE));
    	DataOutputStream f2Output = new DataOutputStream(
    			new BufferedOutputStream(new FileOutputStream(f2),BUFFER_SIZE));
    	copyHalfToF2(numberOfSegments,segmentSize,f1Input,f2Output);
    	f2Output.close();
    	DataInputStream f2Input = new DataInputStream(
    			new BufferedInputStream(new FileInputStream(f2),BUFFER_SIZE));
    	DataOutputStream f3Output = new DataOutputStream(
    			new BufferedOutputStream(new FileOutputStream(f3),BUFFER_SIZE));
    	mergeSegments(numberOfSegments/2,segmentSize,f1Input,f2Input,f3Output);
    	f1Input.close();
    	f2Input.close();
    	f3Output.close();
    }
    private static void copyHalfToF2(int numberOfSegments,int segmentSize,
    		DataInputStream f1,DataOutputStream f2) throws IOException {
    	for(int i= 0;i<(numberOfSegments/2)*segmentSize;i++) {
    		f2.writeInt(f1.readInt());
    	}
    }
    private static void mergeSegments(int numberOfSegments,int segmentSize,
    		DataInputStream f1,DataInputStream f2,DataOutputStream f3) throws IOException {
    	for(int i = 0;i<numberOfSegments;i++) {
    		mergeTwoSegments(segmentSize,f1,f2,f3);
    	}
    	while(f1.available()>0) {
    		f3.writeInt(f1.readInt());
    	}
    	
    }
    private static void mergeTwoSegments(int segmentSize,
    		DataInputStream f1,DataInputStream f2,DataOutputStream f3) throws IOException {
    	int intFromF1 = f1.readInt();
    	int intFromF2 = f2.readInt();
    	int f1Count = 1;
    	int f2Count = 1;
    	while(true) {
    		if(intFromF1<intFromF2) {
    			f3.writeInt(intFromF1);
    			if(f1.available()==0||f1Count++>=segmentSize) {
    				f3.writeInt(intFromF2);
    				break;
    			} else {
    				intFromF1 = f1.readInt();
    			}
    		} else {
    			f3.writeInt(intFromF2);
    			if(f2.available()==0||f2Count++>=segmentSize) {
    				f3.writeInt(intFromF1);
    				break;
    			}else {
    				intFromF2=f2.readInt();
    			}
    		}
    	}
    	while(f1.available()>0&&f1Count++<segmentSize) {
    		f3.writeInt(f1.readInt());
    	}
    	while(f2.available()>0&&f2Count++<segmentSize) {
    		f3.writeInt(f2.readInt());
    	}
    	
    }
    public static void displayFile(String filename) {
		try {
			DataInputStream input= new DataInputStream(new FileInputStream(filename));
			System.out.println("-------------------------------");
			System.out.println("-------------------------------");
			System.out.println("-------------------------------");
			System.out.println("-------------------------------");
			System.out.println("-------------------------------");
			  for(int i=0;i<256;i++) {
		    	  System.out.println(input.readInt());
		      }   	
		      input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

别的就没有什么,好了,就这样了,结束。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值