博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
外部排序(java实现)
阅读量:6690 次
发布时间:2019-06-25

本文共 3885 字,大约阅读时间需要 12 分钟。

mport java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
/**
* Purely informational so use at your own risk, it is not optimized and it can
* only handle data sets that have a size that is are divible by BUFFER_SIZE.
* 
*/
public class ExternalSort {
    
public static int BUFFER_SIZE = 10;
    
public File sort(File file) throws IOException {
        ArrayList
<File> files = split(file);
        
return process(files);
    }
    
// recursive method to merge the lists until we are left with a
    
// single merged list
    private File process(ArrayList<File> list) throws IOException {
        
if (list.size() == 1) {
            
return list.get(0);
        }
        ArrayList
<File> inter = new ArrayList<File>();
        
for (Iterator<File> itr = list.iterator(); itr.hasNext();) {
            File one 
= itr.next();
            
if (itr.hasNext()) {
                File two 
= itr.next();
                inter.add(merge(one, two));
            } 
else {
                
return one;
            }
        }
        
return process(inter);
    }
    
/**
     * Splits the original file into a number of sub files. 
     
*/
    
private ArrayList<File> split(File file) throws IOException {
        ArrayList
<File> files = new ArrayList<File>();
        
int[] buffer = new int[BUFFER_SIZE];
        FileInputStream fr 
= new FileInputStream(file);
        
boolean fileComplete = false;
        
while (!fileComplete) {
            
int index = buffer.length;
            
for (int i = 0; i < buffer.length && !fileComplete; i++) {
                buffer[i] 
= readInt(fr);
                
if (buffer[i] == -1) {
                    fileComplete 
= true;
                    index 
= i;
                }
            }
            
if (buffer[0] > -1) {
                Arrays.sort(buffer, 
0, index);
                File f 
= new File("set" + new Random().nextInt());
                FileOutputStream writer 
= new FileOutputStream(f);
                
for (int j = 0; j < index; j++) {
                    writeInt(buffer[j], writer);
                }
                writer.close();
                files.add(f);
            }
        }
        fr.close();
        
return files;
    }
    
/**
     * Merges two sorted files into a single file.
     * 
     * 
@param one
     * 
@param two
     * 
@return
     * 
@throws IOException
     
*/
    
private File merge(File one, File two) throws IOException {
        FileInputStream fis1 
= new FileInputStream(one);
        FileInputStream fis2 
= new FileInputStream(two);
        File output 
= new File("merged" + new Random().nextInt());
        FileOutputStream os 
= new FileOutputStream(output);
        
int a = readInt(fis1);
        
int b = readInt(fis2);
        
boolean finished = false;
        
while (!finished) {
            
if (a != -1 && b != -1) {
                
if (a < b) {
                    writeInt(a, os);
                    a 
= readInt(fis1);
                } 
else {
                    writeInt(b, os);
                    b 
= readInt(fis2);
                }
            } 
else {
                finished 
= true;
            }
            
if (a == -1 && b != -1) {
                writeInt(b, os);
                b 
= readInt(fis2);
            } 
else if (b == -1 && a != -1) {
                writeInt(a, os);
                a 
= readInt(fis1);
            }
        }
        os.close();
        
return output;
    }
    
private void writeInt(int value, FileOutputStream merged)
            
throws IOException {
        merged.write(value);
        merged.write(value 
>> 8);
        merged.write(value 
>> 16);
        merged.write(value 
>> 24);
        merged.flush();
    }
    
private int readInt(FileInputStream fis) throws IOException {
        
int buffer = fis.read();
        
if (buffer == -1) {
            
return -1;
        }
        buffer 
|= (fis.read() << 8);
        buffer 
|= (fis.read() << 16);
        buffer 
|= (fis.read() << 24);
        
return buffer;
    }
    
/**
     * 
@param args
     * 
@throws IOException
     
*/
    
public static void main(String[] args) throws IOException {
        File file 
= new File("mainset");
        Random random 
= new Random(System.currentTimeMillis());
        FileOutputStream fw 
= new FileOutputStream(file);
        
for (int i = 0; i < BUFFER_SIZE * 3; i++) {
            
int ger = random.nextInt();
            ger 
= ger < 0 ? -ger : ger;
            fw.write(ger);
            fw.write(ger 
>> 8);
            fw.write(ger 
>> 16);
            fw.write(ger 
>> 24);
        }
        fw.close();
        ExternalSort sort 
= new ExternalSort();
        System.out.println(
"Original:");
        dumpFile(sort, file);
        File f 
= sort.sort(file);
        System.out.println(
"Sorted:");
        dumpFile(sort, f);
    }
    
private static void dumpFile(ExternalSort sort, File f)
            
throws FileNotFoundException, IOException {
        FileInputStream fis 
= new FileInputStream(f);
        
int i = sort.readInt(fis);
        
while (i != -1) {
            System.out.println(Integer.toString(i));
            i 
= sort.readInt(fis);
        }
    }
}

转载地址:http://zrqao.baihongyu.com/

你可能感兴趣的文章
python之端午到了
查看>>
正则表达式
查看>>
TinyMCE下载及使用
查看>>
openstack instance resize
查看>>
Aix 文件名补齐及aix6.1 bash安装
查看>>
C编程技巧
查看>>
week5
查看>>
Unity3D常用网络框架与实战解析 学习
查看>>
继承(原型链继承)
查看>>
如何利用 Visual Studio 自定义项目或工程模板(转载)
查看>>
java.lang.Object底层代码分析-jdk1.8
查看>>
获取函数所在模块的方法
查看>>
Linux信号处理
查看>>
VS预定义宏
查看>>
QtTableView
查看>>
*****运维人员如何最大限度避免误删除文件?
查看>>
Android应用开发基础--Adapter
查看>>
条件随机场
查看>>
别人要访问我的电脑上部署的tomcat,必须关闭防火墙吗?
查看>>
sql server 2005刚安装完找不到服务器
查看>>