欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 位圖法排序的使用方法

 更新時間:2013年04月15日 09:44:25   作者:  
本篇文章,小編將為大家介紹關(guān)于Java 位圖法排序的使用方法,有需要的朋友可以參考一下

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

復(fù)制代碼 代碼如下:

/**
     * 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 arrays
        if (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 merge
        if (((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 search
        do {
            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 array
        if ((end - r) <= 0) {
            System.arraycopy(in, start, out, i, med - start);
        } else {
            System.arraycopy(in, r, out, i, end - r);
        }
    }


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

比如{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] = 1

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

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

復(fù)制代碼 代碼如下:

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


如果有重復(fù)數(shù)據(jù),可以修改下:
復(fù)制代碼 代碼如下:

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");
    }
}


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

相關(guān)文章

  • JAVA對list集合進行排序Collections.sort()

    JAVA對list集合進行排序Collections.sort()

    這篇文章主要介紹了JAVA對list集合進行排序Collections.sort(),需要的朋友可以參考下
    2017-01-01
  • SpringBoot整合rockerMQ消息隊列詳解

    SpringBoot整合rockerMQ消息隊列詳解

    今天和大家一起深入生產(chǎn)級別消息中間件 - RocketMQ 的內(nèi)核實現(xiàn),來看看真正落地能支撐萬億級消息容量、低延遲的消息隊列到底是如何設(shè)計的。我會先介紹整體的架構(gòu)設(shè)計,然后再深入各核心模塊的詳細設(shè)計、核心流程的剖析
    2022-07-07
  • JWT Token實現(xiàn)方法及步驟詳解

    JWT Token實現(xiàn)方法及步驟詳解

    這篇文章主要介紹了JWT Token實現(xiàn)方法及步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • springboot整合RabbitMQ 中的 TTL實例代碼

    springboot整合RabbitMQ 中的 TTL實例代碼

    TTL 是 RabbitMQ 中一個消息或者隊列的屬性,表明一條消息或者該隊列中的所有消息的最大存活時間,單位是毫秒,這篇文章主要介紹了springboot整合RabbitMQ 中的 TTL,需要的朋友可以參考下
    2022-09-09
  • java讀取excel文件的兩種方法

    java讀取excel文件的兩種方法

    這篇文章主要為大家詳細介紹了java讀取excel文件的兩種方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • SpringMVC @RequestBody屬性名大寫字母注入失敗的解決

    SpringMVC @RequestBody屬性名大寫字母注入失敗的解決

    這篇文章主要介紹了SpringMVC @RequestBody屬性名大寫字母注入失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java 獲取Html文本中的img標簽下src中的內(nèi)容方法

    Java 獲取Html文本中的img標簽下src中的內(nèi)容方法

    今天小編就為大家分享一篇Java 獲取Html文本中的img標簽下src中的內(nèi)容方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 關(guān)于mybatis的一級緩存和二級緩存的那些事兒

    關(guān)于mybatis的一級緩存和二級緩存的那些事兒

    MyBatis自帶的緩存有一級緩存和二級緩存,今天我們就來學(xué)習(xí)一下,文中有非常詳細的總結(jié),對正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • Java進階必備之多線程編程

    Java進階必備之多線程編程

    今天帶大家來學(xué)習(xí)java多線程編程,文中有非常詳細的代碼示例及介紹,對正在學(xué)習(xí)java多線程的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 關(guān)于java關(guān)鍵字this和super的區(qū)別和理解

    關(guān)于java關(guān)鍵字this和super的區(qū)別和理解

    這篇文章主要給大家介紹了關(guān)于java關(guān)鍵字this和super的區(qū)別和理解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論