位图排序

/**
 * 位图排序,思想来自《编程珠玑》。<br>
 * (功能详细描述)<br>
 * @see          相关函数,对于重要的函数建议注释
 * @since        iNOC V200R003C00, 2012-2-20
 * @param args
 */
public class BitSort
{
    public static void main(String[] args)
    {
        generateRandom();
        
        bitSort();

    }
    
    /**
     * 
     * 生成随机数,并写入文件。<br>
     * (功能详细描述)<br>
     
     * @see          相关函数,对于重要的函数建议注释
     * @since        iNOC V200R003C00, 2012-2-21
     */
    public static void generateRandom()
    {
        BufferedWriter writer = null;
        
        Random random = new Random();
        int num = 0;
        StringBuilder nums  = new StringBuilder();
        
        for(int i = 0; i < 500000; i++)
        {
            num = random.nextInt(500000);
            
            if(num > -1)
            {
                nums.append(num);
                nums.append("\n");
            }
        }

        try
        {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("random.txt")));
            writer.write(nums.toString());
            writer.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                writer.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 
     * 读取文件的数据,排序,并写入另一个文件。<br>
     * (功能详细描述)<br>
     
     * @see          相关函数,对于重要的函数建议注释
     * @since        iNOC V200R003C00, 2012-2-21
     */
    public static void bitSort()
    {
        BitSet bitSet = new BitSet(1000000);
        
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("random.txt")));
            char[] buffer = new char[10000];
            int bufferSize = -1;
            StringBuilder nums = new StringBuilder();
            
            while(-1 != (bufferSize = reader.read(buffer)))
            {
                nums.append(buffer, 0, bufferSize);
            }
            
            String[] tempNums = nums.toString().split("\n");
            
            for(String num : tempNums)
            {
                bitSet.set(Integer.valueOf(num));
            }
            
            nums = new StringBuilder();
            
            for(int i = 0; i < bitSet.length(); i++)
            {
                if(bitSet.get(i))
                {
                    nums.append(i);
                    nums.append("\n");
                }
            }
            
            BufferedWriter writer = null;
            
            try
            {
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("random2.txt")));
                writer.write(nums.toString());
                writer.flush();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    writer.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}


你可能感兴趣的:(排序)