日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 位图法排序

發布時間:2025/3/21 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 位图法排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????? java JDK里面容器類的排序算法使用的主要是插入排序和歸并排序,可能不同版本的實現有所不同,關鍵代碼如下:

?

/*** Performs a sort on the section of the array between the given indices* using a mergesort with exponential search algorithm (in which the merge* is performed by exponential search). n*log(n) performance is guaranteed* and in the average case it will be faster then any mergesort in which the* merge is performed by linear search.* * @param in -* the array for sorting.* @param out -* the result, sorted array.* @param start* the start index* @param end* the end index + 1*/@SuppressWarnings("unchecked")private static void mergeSort(Object[] in, Object[] out, int start,int end) {int len = end - start;// use insertion sort for small arraysif (len <= SIMPLE_LENGTH) {for (int i = start + 1; i < end; i++) {Comparable<Object> current = (Comparable<Object>) out[i];Object prev = out[i - 1];if (current.compareTo(prev) < 0) {int j = i;do {out[j--] = prev;} while (j > start&& current.compareTo(prev = out[j - 1]) < 0);out[j] = current;}}return;}int med = (end + start) >>> 1;mergeSort(out, in, start, med);mergeSort(out, in, med, end);// merging// if arrays are already sorted - no mergeif (((Comparable<Object>) in[med - 1]).compareTo(in[med]) <= 0) {System.arraycopy(in, start, out, start, len);return;}int r = med, i = start;// use merging with exponential searchdo {Comparable<Object> fromVal = (Comparable<Object>) in[start];Comparable<Object> rVal = (Comparable<Object>) in[r];if (fromVal.compareTo(rVal) <= 0) {int l_1 = find(in, rVal, -1, start + 1, med - 1);int toCopy = l_1 - start + 1;System.arraycopy(in, start, out, i, toCopy);i += toCopy;out[i++] = rVal;r++;start = l_1 + 1;} else {int r_1 = find(in, fromVal, 0, r + 1, end - 1);int toCopy = r_1 - r + 1;System.arraycopy(in, r, out, i, toCopy);i += toCopy;out[i++] = fromVal;start++;r = r_1 + 1;}} while ((end - r) > 0 && (med - start) > 0);// copy rest of arrayif ((end - r) <= 0) {System.arraycopy(in, start, out, i, med - start);} else {System.arraycopy(in, r, out, i, end - r);}}

?

?

??????? 看到編程珠璣上有一個很有趣的排序算法-位圖法其思想是用1位來表示[0~n-1]中的整數是否存在。1表示存在,0表示不存在。即將正整數映射到bit集合中,每一個bit代表其映射的正整數是否存在。

? 比如{1,2,3,5,8,13}使用下列集合表示:

? 0 1 1 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0

偽代碼如下:

for (i in [0~n-1]) bit[i] = 0; for(i in [0~n-1]) if (i in input file) bit[i] = 1for(i in [0~n-1])if(bit[i] == 1) output i

?????? 用java 代碼嘗試下,效率果然不錯:

public class javaUniqueSort {public static int[] temp = new int[1000001];public static List<Integer> tempList = new ArrayList<Integer>();public static int count;public static void main(final String[] args) {List<Integer> firstNum = new ArrayList<Integer>();List<Integer> secondNum = new ArrayList<Integer>();for (int i = 1; i <= 1000000; i++) {firstNum.add(i);secondNum.add(i);}Collections.shuffle(firstNum);Collections.shuffle(secondNum);getStartTime();Collections.sort(firstNum);getEndTime("java sort run time ");getStartTime();secondNum = uniqueSort(secondNum);getEndTime("uniqueSort run time ");}public static List<Integer> uniqueSort(final List<Integer> uniqueList) {javaUniqueSort.tempList.clear();for (int i = 0; i < javaUniqueSort.temp.length; i++) {javaUniqueSort.temp[i] = 0;}for (int i = 0; i < uniqueList.size(); i++) {javaUniqueSort.temp[uniqueList.get(i)] = 1;}for (int i = 0; i < javaUniqueSort.temp.length; i++) {if (javaUniqueSort.temp[i] == 1) {javaUniqueSort.tempList.add(i);}}return javaUniqueSort.tempList;}public static void getStartTime() {javaShuffle.start = System.nanoTime();}public static void getEndTime(final String s) {javaShuffle.end = System.nanoTime();System.out.println(s + ": " + (javaShuffle.end - javaShuffle.start) + "ns");} }運行時間:java sort run time : 1257737334ns uniqueSort run time : 170228290ns java sort run time : 1202749828ns uniqueSort run time : 169327770ns

?


如果有重復數據,可以修改下:

public class javaDuplicateSort {public static List<Integer> tempList = new ArrayList<Integer>();public static int count;public static void main(final String[] args) {Random random = new Random();List<Integer> firstNum = new ArrayList<Integer>();List<Integer> secondNum = new ArrayList<Integer>();for (int i = 1; i <= 100000; i++) {firstNum.add(i);secondNum.add(i);firstNum.add(random.nextInt(i + 1));secondNum.add(random.nextInt(i + 1));}Collections.shuffle(firstNum);Collections.shuffle(secondNum);getStartTime();Collections.sort(firstNum);getEndTime("java sort run time ");getStartTime();secondNum = uniqueSort(secondNum);getEndTime("uniqueSort run time ");}public static List<Integer> uniqueSort(final List<Integer> uniqueList) {javaDuplicateSort.tempList.clear();int[] temp = new int[200002];for (int i = 0; i < temp.length; i++) {temp[i] = 0;}for (int i = 0; i < uniqueList.size(); i++) {temp[uniqueList.get(i)]++;}for (int i = 0; i < temp.length; i++) {for (int j = temp[i]; j > 0; j--) {javaDuplicateSort.tempList.add(i);}}return javaDuplicateSort.tempList;}public static void getStartTime() {javaShuffle.start = System.nanoTime();}public static void getEndTime(final String s) {javaShuffle.end = System.nanoTime();System.out.println(s + ": " + (javaShuffle.end - javaShuffle.start) + "ns");} }

?

????? 這種算法還是有很明顯的局限性的,比如說要知道數據中最大的數值,更重要的是數據的疏密程度,比如說最大值為1000000而要數組大小只有100,那么效率會下降的非常明顯。。。。。但是,使用位圖法進行排序,確實讓人眼前一亮。位圖法通常是用來存儲數據,判斷某個數據存不存在或者判斷數組是否存在重復 。

?????

?


?

?

?

轉載于:https://www.cnblogs.com/Kingle/archive/2012/03/09/2994679.html

總結

以上是生活随笔為你收集整理的Java 位图法排序的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 污片在线观看 | 校园sm主奴调教1v1罚视频 | 高清视频一区 | 久草视频中文在线 | 国产91在线高潮白浆在线观看 | 成人一区二区三区在线 | 日本三级不卡 | 精品国产中文字幕 | 成人黄色三级 | 天堂网在线最新版www中文网 | 国产精品自产拍高潮在线观看 | 在线精品视频免费观看 | 五月六月婷婷 | 久久综合久久综合久久综合 | av视| 超碰啪啪| 99视频精品免费 | 久久久国产免费 | 日韩成人在线免费观看 | 色婷婷www | 五月天在线 | 天天视频黄 | 色666| 午夜激情视频在线 | av不卡网站 | 亚洲一级片 | 日韩亚洲区 | 少妇把腿扒开让我舔18 | av精选 | 日噜噜夜噜噜 | 91视频 - 88av | 97av视频| 日韩一区二区在线播放 | 免费一二区 | 伊伊成人| 99久久久无码国产精品性青椒 | av中文资源网 | 熟妇高潮一区二区三区 | 欧美一区二区三区免费在线观看 | 国产日韩欧美高清 | 天天干天天操天天舔 | 精品人妻一区二区免费 | 婷婷激情四射 | 97网站| 国产伦精品一区 | 少妇真实被内射视频三四区 | 中文字字幕在线中文乱码电影 | 色婷婷在线视频 | 香蕉网址 | 日本国产一区二区 | 中国女人av | 涩色视频 | 午夜在线观看免费视频 | 一区二区三区日韩视频 | 少妇肥臀大白屁股高清 | 国产成人精品一区二区三区免费 | 免费看黄在线看 | 18色av| 精品欧美黑人一区二区三区 | 日日摸天天爽天天爽视频 | 美女狂揉羞羞的视频 | 午夜亚洲福利在线老司机 | 成人国产一区二区三区精品麻豆 | 亚洲aⅴ| 国产精品高潮呻吟久久久 | 青青久久av北条麻妃黑人 | 啪啪福利社 | 黄色网在线 | 日p视频在线观看 | 婷婷亚洲综合五月天小说 | 在线观看免费视频国产 | 欲乱美女| 日本精品不卡 | 婷婷伊人综合中文字幕 | 影音先锋成人 | 国产成人亚洲精品无码h在线 | 涩涩视频网 | 国产乱码精品一区二区三区亚洲人 | 干极品美女 | 午夜精品久久久久久久99老熟妇 | 婷婷毛片 | www麻豆| 欧美久久综合 | 国产视频一级 | 超碰三级| 黄片毛片在线免费观看 | 国产亚洲精品成人无码精品网站 | 欧美一区二区三区观看 | 国产精品xxx | 国产在线不卡一区 | 亚洲24p | 欧美99| 成年人的免费视频 | 爱情岛论坛永久入口 | 久久蜜臀| 日日av| 黄色网占 | 亚洲一区二区网站 | 小仙女av|