計數排序(線性)

來源:互聯網
上載者:User
public class CountSort {    public static void main(String[] args) {        int[] a = { 3, 1, 6, 0, 3, 0, 1, 5, 3, 6 };        int max = getMax(a);        arrDisplay(a, "Before mySort:");        a = mySort(a, max);        arrDisplay(a, "After  mySort:");    }    public static int[] mySort(int[] a, int max) {        int[] res = new int[a.length];        int[] c = new int[max + 1];        for (int i = 0; i < res.length; i++) {            res[i] = 0;        }        for (int i = 0; i < c.length; i++) {            c[i] = 0;        }        int temp = 0;        /*統計元素出現次數並把次數放在c數組的以該元素為下標的位置        不可以在這個遍曆過程中取最大值,就是說getMax不可合并,因為需要事先c的大小;        能否直接開為整形最大值,這樣不可能,超記憶體*/        for (int i = 0; i < a.length; i++) {            temp = a[i];            c[temp] = c[temp] + 1;        }        for (int i = 1; i < c.length; i++) {            c[i] = c[i] + c[i - 1];        }                //必須從後往前,這樣保證了穩定性        for (int i = a.length - 1; i >= 0; i--) {            temp = a[i];            res[c[temp] - 1] = temp;            //不必擔心小於0,因為一旦等於0就不會再減小了,因為原數組沒該元素了            c[temp] = c[temp] - 1;        }        return res;    }    public static int getMax(int[] a) {        int max = a[0];        for (int i = 1; i < a.length; i++) {            if (a[i] > max)                max = a[i];        }        return max;    }    public static void arrDisplay(int[] a, String str) {            System.out.println(str);        for (int i = 0; i < a.length; i++)            System.out.print(a[i] + " ");        System.out.println();    }}

一.演算法簡介

       通過統計元素出現的次數進而排序,需要一個輔助數組,大小是最大元素值(想想計數的過程),為了更好的理解計數排序,我們先來想象一下如果一個數組裡所有元素都是非負整數(數組下標是整數),而且都在0-max(由於記憶體的原因,這個值要小一些)以內,那對於數組裡每個元素來說,如果我能知道數組裡有多少項小於或等於該元素,就能準確地給出該元素在排序後的數組的位置。

        局限性:通過上面的描述可以看出需要整數(若有負數,則正負數分別排序),而且最大值要在能開數組範圍內。

        演算法是穩定的,演算法第五步從後往前保證了穩定性,希望讀者細細體會……

二.演算法描述

  1. 求得元素最大值max(看演算法實現過程,體會這個方法需要獨立,就是說max需要事先確定)
  2. 開輔助數組c[max]和res[原數組大小]並全部初始化為0
  3. 計數:統計元素出現次數並把次數放在c數組的以該元素為下標的位置
  4. 針對c數組從下標1開始,前一項加上後一項並存入後一項獲得數組裡有多少項小於或等於該元素
  5. 對原數組中的每一個元素temp存入res[c[temp]-1](減一是因為下標從0開始)並更新c[temp]--(不必擔心小於0,因為一旦等於0就不會再減小了,因為原數組沒該元素了)

三.演算法的Java實現如上:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.