有1亿个浮点数,请找出其中对大的10000个。提示:假设每个浮点数占4个字节,1亿个浮点数就要站到相当大的空间,因此不能一次将全部读入内存进行排序。 /** * */ package com.code; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import junit.framework.Assert; import org.junit.Test; /** * @author Chi * */ public class FloatSelector { // @Test public void testGenerateData(){ this.generate(100000000); } @Test public void select(){ long start = System.currentTimeMillis(); final int SIZE = 10000; Float[] selected = new Float[SIZE]; BufferedInputStream in = new BufferedInputStream(this.getClass().getResourceAsStream("data")); //Set buffer to load array for one time. final int BUFFER_SIZE = SIZE * 4; final byte[] buffer = new byte[BUFFER_SIZE]; //1, Fill selected array int pointer = 0; int read = 0; try { read = in.read(buffer); for(int i = 0; i < read / 4; i++ ){ selected[pointer ++] = Float.intBitsToFloat(bytes2int(buffer, i*4)); } //2, sort the array Arrays.sort(selected, Collections.reverseOrder()); while((read = in.read(buffer)) > 0){ for(int i = 0; i < read / 4; i++ ){ float newValue = Float.intBitsToFloat(bytes2int(buffer, i*4)); int j; for(j = selected.length - 1; j >= 0 && newValue > selected[j]; j--); if(j < selected.length - 1){ //move for(int k = selected.length - 1; k > j + 1; k --){ selected[k] = selected[k - 1]; } selected[j + 1] = newValue; } } } long time = System.currentTimeMillis() - start; System.out.println("Time used: " + time); // System.out.println(Arrays.toString(selected)); } catch (IOException e) { throw new RuntimeException("Run @Test generate to create the data file first", e); }finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void int2bytes(byte[] bytes, int offset, int value) { bytes[offset] = (byte)((value >>> 24) & 0xFF); bytes[offset + 1] = (byte)((value >>> 16) & 0xFF); bytes[offset + 2] = (byte)((value >>> 8) & 0xFF); bytes[offset + 3] = (byte)(value & 0xFF); } public static int bytes2int(byte[] bytes, int offset){ int i = 0; i |= bytes[offset] & 0xFF; i <<= 8; i |= bytes[offset + 1] & 0xFF; i <<= 8; i |= bytes[offset + 2] & 0xFF; i <<= 8; i |= bytes[offset + 3] & 0xFF; return i; } // @Test public void testConvertInteger(){ int i = 1185206571; byte[] array = new byte[4]; int2bytes(array, 0, i); int i2 = bytes2int(array, 0); Assert.assertEquals(i, i2); } public void generate(int count){ BufferedOutputStream out = null; final int BUFFER_SIZE = 10000; final int PER = BUFFER_SIZE / 4; try{ out = new BufferedOutputStream(new FileOutputStream(new File(this.getClass().getResource("").getPath() + "/data"))); byte[] buffer = new byte[BUFFER_SIZE]; float f; for(int i = 0; i < count/PER; i++ ){ for(int j = 0; j < PER; j++ ){ f = (float) Math.random() * 100000; int2bytes(buffer, j*4, Float.floatToIntBits(f)); } out.write(buffer); } for(int i = 0; i < count%PER; i++ ){ f = (float) Math.random() * 100000; int2bytes(buffer, i*4, Float.floatToIntBits(f)); } out.write(buffer, 0, (count%PER) * 4); }catch(Exception e){ e.fillInStackTrace(); throw new RuntimeException(e); }finally{ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }