日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

如何在10亿数中找出前1000大的数

發布時間:2025/5/22 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在10亿数中找出前1000大的数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

1.排序方法

首先能想到的就是先排序,然后取前1000個數,或者部分排序,只排出前1000個數就行

缺點:這些方法的時間復雜度都比較高

2,分治法

可以使用分治法,這有點類似快排中partition的操作,隨機選一個數t,然后對整個數組進行partition,會得到兩部分,前一部分的數都大于t,后一部分的數都小于t。

如果說前一部分總數大于1000個,那就繼續在前一部分進行partition尋找。如果前一部分的數小于1000個,那就在后一部分再進行partition,尋找剩下的數。

首先,partition的過程,時間是o(n)。我們在進行第一次partition的時候需要花費n,第二次partition的時候,數據量減半了,所以只要花費n/2,同理第三次的時候只要花費n/4,以此類推。而n+n/2+n/4+...顯然是小于2n的,所以這個方法的漸進時間只有o(n)

3.可以用分布式思想,將數據切分,然后在多臺機器上分別計算前1000大的數,最后再把這些數匯總

4.堆排序法(推薦)

如果只有一臺機器,可以在內存中維護一個1000數的小頂堆,根據堆得性質,每一個節點都比它的左右子節點要小

1.先去前N個數,構成小頂堆

2.然后從文件中讀取數據,并且和堆頂大小相比,如果比對頂還小,就直接丟棄

3.如果比堆頂大,就替換堆頂,并調整最小堆

TopN.java

/*** @author xiaoshi on 2018/10/14.*/ public class TopN {// 父節點private int parent(int n) {return (n - 1) / 2;}// 左孩子private int left(int n) {return 2 * n + 1;}// 右孩子private int right(int n) {return 2 * n + 2;}// 構建堆private void buildHeap(int n, int[] data) {for(int i = 1; i < n; i++) {int t = i;// 調整堆while(t != 0 && data[parent(t)] > data[t]) {int temp = data[t];data[t] = data[parent(t)];data[parent(t)] = temp;t = parent(t);}}}// 調整data[i]private void adjust(int i, int n, int[] data) {if(data[i] <= data[0]) {return;}// 置換堆頂int temp = data[i];data[i] = data[0];data[0] = temp;// 調整堆頂int t = 0;while( (left(t) < n && data[t] > data[left(t)])|| (right(t) < n && data[t] > data[right(t)]) ) {if(right(t) < n && data[right(t)] < data[left(t)]) {// 右孩子更小,置換右孩子temp = data[t];data[t] = data[right(t)];data[right(t)] = temp;t = right(t);} else {// 否則置換左孩子temp = data[t];data[t] = data[left(t)];data[left(t)] = temp;t = left(t);}}}// 尋找topN,該方法改變data,將topN排到最前面public void findTopN(int n, int[] data) {// 先構建n個數的小頂堆buildHeap(n, data);// n往后的數進行調整for(int i = n; i < data.length; i++) {adjust(i, n, data);}}// 打印數組public void print(int[] data) {for(int i = 0; i < data.length; i++) {System.out.print(data[i] + " ");}System.out.println();}}

Main.java

import java.util.Random;/*** @author xiaoshi on 2018/10/14.*/ public class Main {public static void main(String[] args) {TopN topN = new TopN();// 第一組測試int[] arr1 = new int[]{56, 30, 71, 18, 29, 93, 44, 75, 20, 65, 68, 34};System.out.println("原數組:");topN.print(arr1);topN.findTopN(5, arr1);System.out.println("調整后數組:");topN.print(arr1);// 第二組測試int[] arr2 = new int[1000];for(int i=0; i<arr2.length; i++) {arr2[i] = i + 1;}System.out.println("原數組:");topN.print(arr2);topN.findTopN(50, arr2);System.out.println("調整后數組:");topN.print(arr2);// 第三組測試Random random =new Random();int[] arr3 = new int[1000];for(int i=0; i<arr3.length; i++) {arr3[i] = random.nextInt();}System.out.println("原數組:");topN.print(arr3);topN.findTopN(50, arr3);System.out.println("調整后數組:");topN.print(arr3);}}

運行結果:

原數組: 56 30 71 18 29 93 44 75 20 65 68 34 調整后數組: 65 68 71 75 93 18 29 30 20 44 56 34 原數組: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 調整后數組: 951 952 955 954 953 956 957 963 968 964 972 961 979 959 958 967 966 969 974 965 970 973 988 962 983 993 986 981 987 992 960 976 1000 982 978 977 975 985 984 990 971 997 996 991 989 999 998 980 994 995 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 原數組: 835636999 -90522479 -769818338 -1416245398 1041573706 1812203535 896607110 1789246766 1774883884 26722194 -418633859 1344767118 1570967674 1316806558 -1435502178 1473470755 -918439629 1869702742 2101404773 -1296472335 649689400 1153902366 -1052670714 498645159 -1530905537 -1159220094 -154125959 -868393434 -504505075 -1106082731 -494609447 -1406763201 -750828828 -342445539 -744595730 -1920006464 -1230413255 -1426324562 -1277878264 474935729 -2029054806 447026196 -1121975794 -1448358181 1957166126 1336854710 …… 調整后數組: 1960093727 1964906931 1970688292 1975808864 1981745799 1991241336 2022661667 1981095418 1998580270 1988169309 2008783098 2027208746 2136972032 2062185334 2058314391 2003774732 2022245178 2022674254 2076406457 2024695646 2106449320 2016192325 2039153729 2043057170 2042482994 2138695524 2139809413 2121618159 2067898415 2122335504 2101895240 2100462015 2054036800 2037921932 2067998974 2117710597 2133594097 2101404773 2077564852 2033907579 2035097590 2108250457 2122256662 2138496647 2088084171 2107415856 2077919162 ……

?

劃船不用槳、楊帆不等風、一生全靠浪

轉載于:https://www.cnblogs.com/williamjie/p/11195186.html

總結

以上是生活随笔為你收集整理的如何在10亿数中找出前1000大的数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。