专栏名称: 武哥聊编程
这里有技术,有段子,有生活,也有资源,要不然怎么叫 “私房菜” 呢?
目录
相关文章推荐
每日经济新闻  ·  DeepSeek大消息,7000亿巨头一度涨 ... ·  21 小时前  
贵州市场监管  ·  《哪吒2》登顶!用来重塑哪吒肉身的“藕粉”, ... ·  20 小时前  
贵州市场监管  ·  《哪吒2》登顶!用来重塑哪吒肉身的“藕粉”, ... ·  20 小时前  
每日豆瓣  ·  来看看全家托举我做出来的竹筒饭 ·  4 天前  
方胖子朱雀大街  ·  方大侠:你能做到我这样,那你就能大赚了! ·  3 天前  
51好读  ›  专栏  ›  武哥聊编程

面试热身:5 亿整数的大文件,排个序 ?

武哥聊编程  · 公众号  ·  · 2020-02-09 11:30

正文

来源:

https://www.cnblogs.com/xupengzhang/p/7966755.html


问题

给你1个文件bigdata,大小4663M,5亿个数,文件中的数据随机,如下一行一个整数:

61963023557681612158020393452095006174677379343122016371712330287901712966901...7005375

现在要对这个文件进行排序,怎么搞?

内部排序

先尝试内排,选2种排序方式:

  1. private final int cutoff = 8;


  2. public void perform(Comparable[] a) {

  3. perform(a,0,a.length - 1);

  4. }


  5. private int median3(Comparable[] a,int x,int y,int z) {

  6. if(lessThan(a[x],a[y])) {

  7. if(lessThan(a[y],a[z])) {

  8. return y;

  9. }

  10. else if(lessThan(a[x],a[z])) {

  11. return z;

  12. }else {

  13. return x;

  14. }

  15. }else {

  16. if(lessThan(a[z],a[y])){

  17. return y;

  18. }else if(lessThan(a[z],a[x])) {

  19. return z;

  20. }else {

  21. return x;

  22. }

  23. }

  24. }


  25. private void perform(Comparable[] a,int low,int high) {

  26. int n = high - low + 1;

  27. //当序列非常小,用插入排序

  28. if(n <= cutoff) {

  29. InsertionSort insertionSort = SortFactory.createInsertionSort();

  30. insertionSort.perform(a,low,high);

  31. //当序列中小时,使用median3

  32. }else if(n <= 100) {

  33. int m = median3(a,low,low + (n >>> 1),high);

  34. exchange(a,m,low);

  35. //当序列比较大时,使用ninther

  36. }else {

  37. int gap = n >>> 3;

  38. int m = low + (n >>> 1);

  39. int m1 = median3(a,low,low + gap,low + (gap << 1));

  40. int m2 = median3(a,m - gap,m,m + gap);

  41. int m3 = median3(a,high - (gap << 1),high - gap,high);

  42. int ninther = median3(a,m1,m2,m3);

  43. exchange(a,ninther,low);

  44. }


  45. if(high <= low)

  46. return;

  47. //lessThan

  48. int lt = low;

  49. //greaterThan

  50. int gt = high;

  51. //中心点

  52. Comparablepivot = a[low];

  53. int i = low + 1;


  54. /*

  55. * 不变式:

  56. * a[low..lt-1] 小于pivot -> 前部(first)

  57. * a[lt..i-1] 等于 pivot -> 中部(middle)

  58. * a[gt+1..n-1] 大于 pivot -> 后部(final)

  59. *

  60. * a[i..gt] 待考察区域

  61. */


  62. while (i <= gt) {

  63. if(lessThan(a[i],pivot)) {

  64. //i-> ,lt ->

  65. exchange(a,lt++,i++);

  66. }else if(lessThan(pivot,a[i])) {

  67. exchange(a,i,gt--);

  68. }else{

  69. i++;

  70. }

  71. }


  72. // a[low..lt-1] < v = a[lt..gt] < a[gt+1..high].

  73. perform(a,low,lt - 1);

  74. perform(a,gt + 1,high);

  75. }

归并排序:

  1. /**

  2. * 小于等于这个值的时候,交给插入排序

  3. */

  4. private final int cutoff = 8;


  5. /**

  6. * 对给定的元素序列进行排序

  7. *

  8. * @param a 给定元素序列

  9. */

  10. @Override

  11. public void perform(Comparable[] a) {

  12. Comparable[] b = a.clone();

  13. perform(b, a, 0, a.length - 1);

  14. }


  15. private void perform(Comparable[] src,Comparable[] dest,int low,int high) {

  16. if(low >= high)

  17. return;


  18. //小于等于cutoff的时候,交给插入排序

  19. if(high - low <= cutoff) {

  20. SortFactory.createInsertionSort().perform(dest,low,high);

  21. return;

  22. }


  23. int mid = low + ((high - low) >>> 1);

  24. perform(dest,src,low,mid);

  25. perform(dest,src,mid + 1,high);


  26. //考虑局部有序 src[mid] <= src[mid+1]

  27. if(lessThanOrEqual(src[mid],src[mid+1])) {

  28. System.arraycopy(src,low,dest,low,high - low + 1);

  29. }


  30. //src[low .. mid] + src[mid+1 .. high] -> dest[low .. high]

  31. merge(src,dest,low,mid,high);

  32. }


  33. private void merge(Comparable[] src,Comparable[] dest,int low,int mid,int high) {


  34. for(int i = low,v = low,w = mid + 1; i <= high; i++) {

  35. if(w > high || v <= mid && lessThanOrEqual(src[v],src[w])) {

  36. dest[i] = src[v++];

  37. }else {

  38. dest[i] = src[w++];

  39. }

  40. }

  41. }


数据太多,递归太深 ->栈溢出?加大Xss?数据太多,数组太长 -> OOM?加大Xmx?


耐心不足,没跑出来.而且要将这么大的文件读入内存,在堆中维护这么大个数据量,还有内排中不断的拷贝,对栈和堆都是很大的压力,不具备通用性。

sort命令来跑

跑了多久呢?24分钟.

为什么这么慢?

粗略的看下我们的资源:

内存 jvm-heap/stack,native-heap/stack,page-cache,block-buffer 外存 swap + 磁盘 数据量很大,函数调用很多,系统调用很多,内核/用户缓冲区拷贝很多,脏页回写很多,io-wait很高,io很繁忙,堆栈数据不断交换至swap,线程切换很多,每个环节的锁也很多.

总之,内存吃紧,问磁盘要空间,脏数据持久化过多导致cache频繁失效,引发大量回写,回写线程高,导致cpu大量时间用于上下文切换,一切,都很糟糕,所以24分钟不细看了,无法忍受.

位图法

  1. private BitSet bits;


  2. public void perform(

  3. String largeFileName,

  4. int total,

  5. String destLargeFileName,

  6. Castor<Integer> castor,

  7. int readerBufferSize,

  8. int writerBufferSize,

  9. boolean asc) throws IOException {


  10. System.out.println("BitmapSort Started.");

  11. long start = System.currentTimeMillis();

  12. bits = new BitSet(total);

  13. InputPart<Integer> largeIn = PartFactory.createCharBufferedInputPart(largeFileName, readerBufferSize);

  14. OutputPart<Integer> largeOut = PartFactory.createCharBufferedOutputPart(destLargeFileName, writerBufferSize);

  15. largeOut.delete();


  16. Integer data;

  17. int off = 0;

  18. try {

  19. while (true) {

  20. data = largeIn.read();

  21. if (data == null)

  22. break;

  23. int v = data;







请到「今天看啥」查看全文