10亿个数取前k大的数

10亿个数取前k大的数

解法:PriorityQueue是优先队列,可以当作堆来使用。

这里:利用最小堆来做,维护堆的大小是k,每次读数,让大于堆顶的元素进堆。

public static void main(String[] args) {
		int k = 1000;
		//不写comparator,默认是小根堆
		PriorityQueue pq = new PriorityQueue<>(k, new Comparator() {
			
			public int compare(Integer o1, Integer o2) {
				return o1 - o2;
			}
		});
		
		for(int i = 0; i < 1000000000; i++) {
			pq.offer(i);
			if(pq.size() > k) {
				pq.poll();
			}
		}
		
		for(int i = 0; i < k; i++) {
			if(i % 10 == 0) {
				System.out.print("\n"+ pq.poll());
			}else {
				System.out.print(pq.poll()+" ");
			}
		}
	}

结果:

999999000999999001 999999002 999999003 999999004 999999005 999999006 999999007 999999008 999999009
999999010999999011 999999012 999999013 999999014 999999015 999999016 999999017 999999018 999999019
999999020999999021 999999022 999999023 999999024 999999025 999999026 999999027 999999028 999999029
999999030999999031 999999032 999999033 999999034 999999035 999999036 999999037 999999038 999999039
999999040999999041 999999042 999999043 999999044 999999045 999999046 999999047 999999048 999999049
999999050999999051 999999052 999999053 999999054 999999055 999999056 999999057 999999058 999999059
999999060999999061 999999062 999999063 999999064 999999065 999999066 999999067 999999068 999999069
999999070999999071 999999072 999999073 999999074 999999075 999999076 999999077 999999078 999999079
999999080999999081 999999082 999999083 999999084 999999085 999999086 999999087 999999088 999999089
999999090999999091 999999092 999999093 999999094 999999095 999999096 999999097 999999098 999999099
999999100999999101 999999102 999999103 999999104 999999105 999999106 999999107 999999108 999999109
999999110999999111 999999112 999999113 999999114 999999115 999999116 999999117 999999118 999999119
999999120999999121 999999122 999999123 999999124 999999125 999999126 999999127 999999128 999999129
999999130999999131 999999132 999999133 999999134 999999135 999999136 999999137 999999138 999999139
999999140999999141 999999142 999999143 999999144 999999145 999999146 999999147 999999148 999999149
999999150999999151 999999152 999999153 999999154 999999155 999999156 999999157 999999158 999999159
999999160999999161 999999162 999999163 999999164 999999165 999999166 999999167 999999168 999999169
999999170999999171 999999172 999999173 999999174 999999175 999999176 999999177 999999178 999999179
999999180999999181 999999182 999999183 999999184 999999185 999999186 999999187 999999188 999999189
999999190999999191 999999192 999999193 999999194 999999195 999999196 999999197 999999198 999999199
999999200999999201 999999202 999999203 999999204 999999205 999999206 999999207 999999208 999999209
999999210999999211 999999212 999999213 999999214 999999215 999999216 999999217 999999218 999999219
999999220999999221 999999222 999999223 999999224 999999225 999999226 999999227 999999228 999999229
999999230999999231 999999232 999999233 999999234 999999235 999999236 999999237 999999238 999999239
999999240999999241 999999242 999999243 999999244 999999245 999999246 999999247 999999248 999999249
999999250999999251 999999252 999999253 999999254 999999255 999999256 999999257 999999258 999999259
999999260999999261 999999262 999999263 999999264 999999265 999999266 999999267 999999268 999999269
999999270999999271 999999272 999999273 999999274 999999275 999999276 999999277 999999278 999999279
999999280999999281 999999282 999999283 999999284 999999285 999999286 999999287 999999288 999999289
999999290999999291 999999292 999999293 999999294 999999295 999999296 999999297 999999298 999999299
999999300999999301 999999302 999999303 999999304 999999305 999999306 999999307 999999308 999999309
999999310999999311 999999312 999999313 999999314 999999315 999999316 999999317 999999318 999999319
999999320999999321 999999322 999999323 999999324 999999325 999999326 999999327 999999328 999999329
999999330999999331 999999332 999999333 999999334 999999335 999999336 999999337 999999338 999999339
999999340999999341 999999342 999999343 999999344 999999345 999999346 999999347 999999348 999999349
999999350999999351 999999352 999999353 999999354 999999355 999999356 999999357 999999358 999999359
999999360999999361 999999362 999999363 999999364 999999365 999999366 999999367 999999368 999999369
999999370999999371 999999372 999999373 999999374 999999375 999999376 999999377 999999378 999999379
999999380999999381 999999382 999999383 999999384 999999385 999999386 999999387 999999388 999999389
999999390999999391 999999392 999999393 999999394 999999395 999999396 999999397 999999398 999999399
999999400999999401 999999402 999999403 999999404 999999405 999999406 999999407 999999408 999999409
999999410999999411 999999412 999999413 999999414 999999415 999999416 999999417 999999418 999999419
999999420999999421 999999422 999999423 999999424 999999425 999999426 999999427 999999428 999999429
999999430999999431 999999432 999999433 999999434 999999435 999999436 999999437 999999438 999999439
999999440999999441 999999442 999999443 999999444 999999445 999999446 999999447 999999448 999999449
999999450999999451 999999452 999999453 999999454 999999455 999999456 999999457 999999458 999999459
999999460999999461 999999462 999999463 999999464 999999465 999999466 999999467 999999468 999999469
999999470999999471 999999472 999999473 999999474 999999475 999999476 999999477 999999478 999999479
999999480999999481 999999482 999999483 999999484 999999485 999999486 999999487 999999488 999999489
999999490999999491 999999492 999999493 999999494 999999495 999999496 999999497 999999498 999999499
999999500999999501 999999502 999999503 999999504 999999505 999999506 999999507 999999508 999999509
999999510999999511 999999512 999999513 999999514 999999515 999999516 999999517 999999518 999999519
999999520999999521 999999522 999999523 999999524 999999525 999999526 999999527 999999528 999999529
999999530999999531 999999532 999999533 999999534 999999535 999999536 999999537 999999538 999999539
999999540999999541 999999542 999999543 999999544 999999545 999999546 999999547 999999548 999999549
999999550999999551 999999552 999999553 999999554 999999555 999999556 999999557 999999558 999999559
999999560999999561 999999562 999999563 999999564 999999565 999999566 999999567 999999568 999999569
999999570999999571 999999572 999999573 999999574 999999575 999999576 999999577 999999578 999999579
999999580999999581 999999582 999999583 999999584 999999585 999999586 999999587 999999588 999999589
999999590999999591 999999592 999999593 999999594 999999595 999999596 999999597 999999598 999999599
999999600999999601 999999602 999999603 999999604 999999605 999999606 999999607 999999608 999999609
999999610999999611 999999612 999999613 999999614 999999615 999999616 999999617 999999618 999999619
999999620999999621 999999622 999999623 999999624 999999625 999999626 999999627 999999628 999999629
999999630999999631 999999632 999999633 999999634 999999635 999999636 999999637 999999638 999999639
999999640999999641 999999642 999999643 999999644 999999645 999999646 999999647 999999648 999999649
999999650999999651 999999652 999999653 999999654 999999655 999999656 999999657 999999658 999999659
999999660999999661 999999662 999999663 999999664 999999665 999999666 999999667 999999668 999999669
999999670999999671 999999672 999999673 999999674 999999675 999999676 999999677 999999678 999999679
999999680999999681 999999682 999999683 999999684 999999685 999999686 999999687 999999688 999999689
999999690999999691 999999692 999999693 999999694 999999695 999999696 999999697 999999698 999999699
999999700999999701 999999702 999999703 999999704 999999705 999999706 999999707 999999708 999999709
999999710999999711 999999712 999999713 999999714 999999715 999999716 999999717 999999718 999999719
999999720999999721 999999722 999999723 999999724 999999725 999999726 999999727 999999728 999999729
999999730999999731 999999732 999999733 999999734 999999735 999999736 999999737 999999738 999999739
999999740999999741 999999742 999999743 999999744 999999745 999999746 999999747 999999748 999999749
999999750999999751 999999752 999999753 999999754 999999755 999999756 999999757 999999758 999999759
999999760999999761 999999762 999999763 999999764 999999765 999999766 999999767 999999768 999999769
999999770999999771 999999772 999999773 999999774 999999775 999999776 999999777 999999778 999999779
999999780999999781 999999782 999999783 999999784 999999785 999999786 999999787 999999788 999999789
999999790999999791 999999792 999999793 999999794 999999795 999999796 999999797 999999798 999999799
999999800999999801 999999802 999999803 999999804 999999805 999999806 999999807 999999808 999999809
999999810999999811 999999812 999999813 999999814 999999815 999999816 999999817 999999818 999999819
999999820999999821 999999822 999999823 999999824 999999825 999999826 999999827 999999828 999999829
999999830999999831 999999832 999999833 999999834 999999835 999999836 999999837 999999838 999999839
999999840999999841 999999842 999999843 999999844 999999845 999999846 999999847 999999848 999999849
999999850999999851 999999852 999999853 999999854 999999855 999999856 999999857 999999858 999999859
999999860999999861 999999862 999999863 999999864 999999865 999999866 999999867 999999868 999999869
999999870999999871 999999872 999999873 999999874 999999875 999999876 999999877 999999878 999999879
999999880999999881 999999882 999999883 999999884 999999885 999999886 999999887 999999888 999999889
999999890999999891 999999892 999999893 999999894 999999895 999999896 999999897 999999898 999999899
999999900999999901 999999902 999999903 999999904 999999905 999999906 999999907 999999908 999999909
999999910999999911 999999912 999999913 999999914 999999915 999999916 999999917 999999918 999999919
999999920999999921 999999922 999999923 999999924 999999925 999999926 999999927 999999928 999999929
999999930999999931 999999932 999999933 999999934 999999935 999999936 999999937 999999938 999999939
999999940999999941 999999942 999999943 999999944 999999945 999999946 999999947 999999948 999999949
999999950999999951 999999952 999999953 999999954 999999955 999999956 999999957 999999958 999999959
999999960999999961 999999962 999999963 999999964 999999965 999999966 999999967 999999968 999999969
999999970999999971 999999972 999999973 999999974 999999975 999999976 999999977 999999978 999999979
999999980999999981 999999982 999999983 999999984 999999985 999999986 999999987 999999988 999999989
999999990999999991 999999992 999999993 999999994 999999995 999999996 999999997 999999998 999999999

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