亚洲精品国产精品乱码视色,下载应用成人电影AVapp,成人影院下载视频

      <track id="gfhue"><i id="gfhue"></i></track><input id="gfhue"></input>

      1. 更多精彩內(nèi)容,歡迎關(guān)注:

        視頻號(hào)
        視頻號(hào)

        抖音
        抖音

        快手
        快手

        微博
        微博

        基數(shù)排序法

        文檔

        基數(shù)排序法

        基數(shù)排序是一種非比較型整數(shù)排序算法,其原理是將整數(shù)按位數(shù)切割成不同的數(shù)字,然后按每個(gè)位數(shù)分別比較。由于整數(shù)也可以表達(dá)字符串(比如名字或日期)和特定格式的浮點(diǎn)數(shù),所以基數(shù)排序也不是只能使用于整數(shù)。
        推薦度:
        導(dǎo)讀基數(shù)排序是一種非比較型整數(shù)排序算法,其原理是將整數(shù)按位數(shù)切割成不同的數(shù)字,然后按每個(gè)位數(shù)分別比較。由于整數(shù)也可以表達(dá)字符串(比如名字或日期)和特定格式的浮點(diǎn)數(shù),所以基數(shù)排序也不是只能使用于整數(shù)。
        .example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px}

        排序算法是《數(shù)據(jù)結(jié)構(gòu)與算法》中最基本的算法之一。排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內(nèi)部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。以下是基數(shù)排序算法:

        基數(shù)排序是一種非比較型整數(shù)排序算法,其原理是將整數(shù)按位數(shù)切割成不同的數(shù)字,然后按每個(gè)位數(shù)分別比較。由于整數(shù)也可以表達(dá)字符串(比如名字或日期)和特定格式的浮點(diǎn)數(shù),所以基數(shù)排序也不是只能使用于整數(shù)。

        1. 基數(shù)排序 vs 計(jì)數(shù)排序 vs 桶排序

        基數(shù)排序有兩種方法:

        這三種排序算法都利用了桶的概念,但對(duì)桶的使用方法上有明顯差異:

        基數(shù)排序:根據(jù)鍵值的每位數(shù)字來分配桶;計(jì)數(shù)排序:每個(gè)桶只存儲(chǔ)單一鍵值;桶排序:每個(gè)桶存儲(chǔ)一定范圍的數(shù)值;2. LSD 基數(shù)排序動(dòng)圖演示

        代碼實(shí)現(xiàn)JavaScript實(shí)例 //LSD Radix Sortvar counter = [];function radixSort(arr, maxDigit) {? ? var mod = 10;? ? var dev = 1;? ? for (var i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {? ? ? ? for(var j = 0; j < arr.length; j++) {? ? ? ? ? ? var bucket = parseInt((arr[j] % mod) / dev);? ? ? ? ? ? if(counter[bucket]==null) {? ? ? ? ? ? ? ? counter[bucket] = [];? ? ? ? ? ? }? ? ? ? ? ? counter[bucket].push(arr[j]);? ? ? ? }? ? ? ? var pos = 0;? ? ? ? for(var j = 0; j < counter.length; j++) {? ? ? ? ? ? var value = null;? ? ? ? ? ? if(counter[j]!=null) {? ? ? ? ? ? ? ? while ((value = counter[j].shift()) != null) {? ? ? ? ? ? ? ? ? ? ? arr[pos++] = value;? ? ? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }? ? }? ? return arr;}Java實(shí)例 /**?* 基數(shù)排序?* 考慮負(fù)數(shù)的情況還可以參考: https://code.i-harness.com/zh-CN/q/e98fa9?*/public class RadixSort implements IArraySort {? ? @Override? ? public int[] sort(int[] sourceArray) throws Exception {? ? ? ? // 對(duì) arr 進(jìn)行拷貝,不改變參數(shù)內(nèi)容? ? ? ? int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);? ? ? ? int maxDigit = getMaxDigit(arr);? ? ? ? return radixSort(arr, maxDigit);? ? }? ? /**? ? ?* 獲取最高位數(shù)? ? ?*/? ? private int getMaxDigit(int[] arr) {? ? ? ? int maxValue = getMaxValue(arr);? ? ? ? return getNumLenght(maxValue);? ? }? ? private int getMaxValue(int[] arr) {? ? ? ? int maxValue = arr[0];? ? ? ? for (int value : arr) {? ? ? ? ? ? if (maxValue < value) {? ? ? ? ? ? ? ? maxValue = value;? ? ? ? ? ? }? ? ? ? }? ? ? ? return maxValue;? ? }? ? protected int getNumLenght(long num) {? ? ? ? if (num == 0) {? ? ? ? ? ? return 1;? ? ? ? }? ? ? ? int lenght = 0;? ? ? ? for (long temp = num; temp != 0; temp /= 10) {? ? ? ? ? ? lenght++;? ? ? ? }? ? ? ? return lenght;? ? }? ? private int[] radixSort(int[] arr, int maxDigit) {? ? ? ? int mod = 10;? ? ? ? int dev = 1;? ? ? ? for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {? ? ? ? ? ? // 考慮負(fù)數(shù)的情況,這里擴(kuò)展一倍隊(duì)列數(shù),其中 [0-9]對(duì)應(yīng)負(fù)數(shù),[10-19]對(duì)應(yīng)正數(shù) (bucket + 10)? ? ? ? ? ? int[][] counter = new int[mod * 2][0];? ? ? ? ? ? for (int j = 0; j < arr.length; j++) {? ? ? ? ? ? ? ? int bucket = ((arr[j] % mod) / dev) + mod;? ? ? ? ? ? ? ? counter[bucket] = arrayAppend(counter[bucket], arr[j]);? ? ? ? ? ? }? ? ? ? ? ? int pos = 0;? ? ? ? ? ? for (int[] bucket : counter) {? ? ? ? ? ? ? ? for (int value : bucket) {? ? ? ? ? ? ? ? ? ? arr[pos++] = value;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? ? ? return arr;? ? }? ? /**? ? ?* 自動(dòng)擴(kuò)容,并保存數(shù)據(jù)? ? ?*? ? ?* @param arr? ? ?* @param value? ? ?*/? ? private int[] arrayAppend(int[] arr, int value) {? ? ? ? arr = Arrays.copyOf(arr, arr.length + 1);? ? ? ? arr[arr.length - 1] = value;? ? ? ? return arr;? ? }}PHP實(shí)例 function radixSort($arr, $maxDigit = null){? ? if ($maxDigit === null) {? ? ? ? $maxDigit = max($arr);? ? }? ? $counter = [];? ? for ($i = 0; $i < $maxDigit; $i++) {? ? ? ? for ($j = 0; $j < count($arr); $j++) {? ? ? ? ? ? preg_match_all('/d/', (string) $arr[$j], $matches);? ? ? ? ? ? $numArr = $matches[0];? ? ? ? ? ? $lenTmp = count($numArr);? ? ? ? ? ? $bucket = array_key_exists($lenTmp - $i - 1, $numArr)? ? ? ? ? ? ? ? ? intval($numArr[$lenTmp - $i - 1])? ? ? ? ? ? ? ? : 0;? ? ? ? ? ? if (!array_key_exists($bucket, $counter)) {? ? ? ? ? ? ? ? $counter[$bucket] = [];? ? ? ? ? ? }? ? ? ? ? ? $counter[$bucket][] = $arr[$j];? ? ? ? }? ? ? ? $pos = 0;? ? ? ? for ($j = 0; $j < count($counter); $j++) {? ? ? ? ? ? $value = null;? ? ? ? ? ? if ($counter[$j] !== null) {? ? ? ? ? ? ? ? while (($value = array_shift($counter[$j])) !== null) {? ? ? ? ? ? ? ? ? ? $arr[$pos++] = $value;? ? ? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }? ? }? ? return $arr;}C++實(shí)例 int maxbit(int data[], int n) //輔助函數(shù),求數(shù)據(jù)的最大位數(shù){? ? int maxData = data[0];? ? ? ? ? ? ? ///< 最大數(shù)? ? /// 先求出最大數(shù),再求其位數(shù),這樣有原先依次每個(gè)數(shù)判斷其位數(shù),稍微優(yōu)化點(diǎn)。? ? for (int i = 1; i < n; ++i)? ? {? ? ? ? if (maxData < data[i])? ? ? ? ? ? maxData = data[i];? ? }? ? int d = 1;? ? int p = 10;? ? while (maxData >= p)? ? {? ? ? ? //p *= 10; // Maybe overflow? ? ? ? maxData /= 10;? ? ? ? ++d;? ? }? ? return d;/* ? ?int d = 1; //保存最大的位數(shù)? ? int p = 10;? ? for(int i = 0; i < n; ++i)? ? {? ? ? ? while(data[i] >= p)? ? ? ? {? ? ? ? ? ? p *= 10;? ? ? ? ? ? ++d;? ? ? ? }? ? }? ? return d;*/}void radixsort(int data[], int n) //基數(shù)排序{? ? int d = maxbit(data, n);? ? int *tmp = new int[n];? ? int *count = new int[10]; //計(jì)數(shù)器? ? int i, j, k;? ? int radix = 1;? ? for(i = 1; i <= d; i++) //進(jìn)行d次排序? ? {? ? ? ? for(j = 0; j < 10; j++)? ? ? ? ? ? count[j] = 0; //每次分配前清空計(jì)數(shù)器? ? ? ? for(j = 0; j < n; j++)? ? ? ? {? ? ? ? ? ? k = (data[j] / radix) % 10; //統(tǒng)計(jì)每個(gè)桶中的記錄數(shù)? ? ? ? ? ? count[k]++;? ? ? ? }? ? ? ? for(j = 1; j < 10; j++)? ? ? ? ? ? count[j] = count[j - 1] + count[j]; //將tmp中的位置依次分配給每個(gè)桶? ? ? ? for(j = n - 1; j >= 0; j--) //將所有桶中記錄依次收集到tmp中? ? ? ? {? ? ? ? ? ? k = (data[j] / radix) % 10;? ? ? ? ? ? tmp[count[k] - 1] = data[j];? ? ? ? ? ? count[k]--;? ? ? ? }? ? ? ? for(j = 0; j < n; j++) //將臨時(shí)數(shù)組的內(nèi)容復(fù)制到data中? ? ? ? ? ? data[j] = tmp[j];? ? ? ? radix = radix * 10;? ? }? ? delete []tmp;? ? delete []count;}C實(shí)例 #include#define MAX 20//#define SHOWPASS#define BASE 10void print(int *a, int n) {? int i;? for (i = 0; i < n; i++) {? ? printf("%d ", a[i]);? }}void radixsort(int *a, int n) {? int i, b[MAX], m = a[0], exp = 1;? for (i = 1; i < n; i++) {? ? if (a[i] > m) {? ? ? m = a[i];? ? }? }? while (m / exp > 0) {? ? int bucket[BASE] = { 0 };? ? for (i = 0; i < n; i++) {? ? ? bucket[(a[i] / exp) % BASE]++;? ? }? ? for (i = 1; i < BASE; i++) {? ? ? bucket[i] += bucket[i - 1];? ? }? ? for (i = n - 1; i >= 0; i--) {? ? ? b[--bucket[(a[i] / exp) % BASE]] = a[i];? ? }? ? for (i = 0; i < n; i++) {? ? ? a[i] = b[i];? ? }? ? exp *= BASE;#ifdef SHOWPASS? ? printf(" PASS ? : ");? ? print(a, n);#endif? }}int main() {? int arr[MAX];? int i, n;? printf("Enter total elements (n <= %d) : ", MAX);? scanf("%d", &n);? n = n < MAX ? n : MAX;? printf("Enter %d Elements : ", n);? for (i = 0; i < n; i++) {? ? scanf("%d", &arr[i]);? }? printf(" ARRAY ?: ");? print(&arr[0], n);? radixsort(&arr[0], n);? printf(" SORTED : ");? print(&arr[0], n);? printf(" ");? return 0;}Lua實(shí)例 -- 獲取表中位數(shù)local maxBit = function (tt)? ? local weight = 10; ? ? ?-- 十進(jìn)制? ? local bit = 1;? ? ? ? for k, v in pairs(tt) do? ? ? ? while v >= weight do? ? ? ? ? ? weight = weight * 10;? ? ? ? ? ? bit = bit + 1; ?? ? ? ? end? ? end? ? return bit;end-- 基數(shù)排序local radixSort = function (tt)? ? local maxbit = maxBit(tt); ? ? local bucket = {};? ? local temp = {};? ? local radix = 1;? ? for i = 1, maxbit do? ? ? ? for j = 1, 10 do? ? ? ? ? ? bucket[j] = 0; ? ? ?--- 清空桶? ? ? ? end? ? ? ? for k, v in pairs(tt) do? ? ? ? ? ? local remainder = math.floor((v / radix)) % 10 + 1; ? ?? ? ? ? ? ? bucket[remainder] = bucket[remainder] + 1; ? ? ?-- 每個(gè)桶數(shù)量自動(dòng)增加1? ? ? ? end? ? ? ? ? ? ? ? for j = 2, 10 do? ? ? ? ? ? bucket[j] = bucket[j - 1] + bucket[j]; ?-- 每個(gè)桶的數(shù)量 = 以前桶數(shù)量和 + 自個(gè)數(shù)量? ? ? ? end ? ? ? ? -- 按照桶的位置,排序--這個(gè)是桶式排序,必須使用倒序,因?yàn)榕判蚍椒ㄊ菑男〉酱?,順序下來,?huì)出現(xiàn)大的在小的上面清空。? ? ? ? for k = #tt, 1, -1 do? ? ? ? ? ? local remainder = math.floor((tt[k] / radix)) % 10 + 1;? ? ? ? ? ? temp[bucket[remainder]] = tt[k];? ? ? ? ? ? bucket[remainder] = bucket[remainder] - 1;? ? ? ? end? ? ? ? for k, v in pairs(temp) do? ? ? ? ? ? tt[k] = v;? ? ? ? end? ? ? ? radix = radix * 10;? ? endend;

        參考地址:

        https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/10.radixSort.md

        https://zh.wikipedia.org/wiki/%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F

        以下是熱心網(wǎng)友對(duì)基數(shù)排序算法的補(bǔ)充,僅供參考:

        熱心網(wǎng)友提供的補(bǔ)充1:

        java 代碼里,mod 每次循環(huán)會(huì)乘 10,但 counter 的行數(shù)是不需要變的,能包含 [-9,9] 就可以了。

        for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
                // 考慮負(fù)數(shù)的情況,這里擴(kuò)展一倍隊(duì)列數(shù),其中 [0-9]對(duì)應(yīng)負(fù)數(shù),[10-19]對(duì)應(yīng)正數(shù) (bucket + 10)
            int[][] counter = new int[20][0];
        
            for (int j = 0; j < arr.length; j++) {
                int bucket = ((arr[j] % mod) / dev) + 10;
                counter[bucket] = arrayAppend(counter[bucket], arr[j]);
            }
        
            int pos = 0;
            for (int[] bucket : counter) {
                for (int value : bucket) {
                    arr[pos++] = value;
                }
            }
        }

        熱心網(wǎng)友提供的補(bǔ)充2:

        艾孜爾江補(bǔ)充使用C#基數(shù)排序算法如下:

        ///基數(shù)排序
        static void RadixSort(List list)
        {
            int maxValue = list.Max();//列表內(nèi)部方法拿過來用用(在Linq中)
            int it = 0;//需要幾趟
                        //maxvalue 9-1 99-2 999-3
                        //10^0<=9 10^1>9 it=1
                        //10^0<99 10^1<99 10^2>99 it=2
            while (Math.Pow(10, it) <= maxValue)
            {
                List> buckets = new List>(10);//分10個(gè)桶對(duì)應(yīng)0-9
                for (int i = 0; i < 10; i++)
                {
                    buckets.Add(new List());
                }//列表初始化大小
                for (int i = 0; i < list.Count; i++)//入桶
                {
                    //989 it=0 989/10^it=989 989%10=9;
                    int digit = (int)((list[i]) / (Math.Pow(10, it)) % 10);//得到對(duì)應(yīng)桶
                    buckets[digit].Add(list[i]);
                }//全部入桶
                list.Clear();//依次取出來
                for (int i = 0; i < buckets.Count; i++)
                {
                    list.AddRange(buckets[i]);
                }
                it += 1;//繼續(xù)下一次循環(huán)入桶出桶
            }
        }

        熱心網(wǎng)友提供的補(bǔ)充3:

        補(bǔ)充一下python的基數(shù)排序代碼實(shí)現(xiàn):

        def radix_sort(data):
        
            if not data:
                return []
            max_num = max(data)  # 獲取當(dāng)前數(shù)列中最大值
            max_digit = len(str(abs(max_num)))  # 獲取最大的位數(shù)
        
            dev = 1  # 第幾位數(shù),個(gè)位數(shù)為1,十位數(shù)為10···
            mod = 10  # 求余數(shù)的除法
            for i in range(max_digit):
                radix_queue = [list() for k in range(mod * 2)]  # 考慮到負(fù)數(shù),我們用兩倍隊(duì)列
                for j in range(len(data)):
                    radix = int(((data[j] % mod) / dev) + mod)
                    radix_queue[radix].append(data[j])
        
                pos = 0
                for queue in radix_queue:
                    for val in queue:
                        data[pos] = val
                        pos += 1
        
                dev *= 10
                mod *= 10
            return data

        熱心網(wǎng)友提供的補(bǔ)充4:

        go 的補(bǔ)一個(gè)吧:

        // 基數(shù)排序
        func RadixSort(arr []int) {
            // 計(jì)算最長(zhǎng)的數(shù)字
            var (
                maxVal int
                maxLen int
            )
            for _, v := range arr {
                if maxVal < v {
                    maxVal = v
                }
            }
            for maxVal > 0 {
                maxLen++
                maxVal /= 10
            }
        
            // 循環(huán)進(jìn)行數(shù)據(jù)分配與回歸
            var (
                base    = 1           // 取余基數(shù),初始是1,用于取出每個(gè)元素的倒數(shù)第 i+1 位的值,計(jì)算公式:v / base %10
                buckets = [10][]int{} // 基數(shù)桶,10個(gè)
            )
            for i := 0; i < maxLen; i++ { // 遍歷位
                for _, v := range arr { // 遍歷數(shù)組
                    d := v / base % 10                 // 每個(gè)數(shù)字當(dāng)前位值
                    buckets[d] = append(buckets[d], v) // 存入對(duì)應(yīng)桶中
                }
        
                // 將桶中元素還原到arr
                idx := 0
                for x, bucket := range buckets {
                    if len(bucket) == 0 {
                        continue
                    }
        
                    for _, v := range bucket {
                        arr[idx] = v
                        idx++
                    }
        
                    // 桶清空
                    buckets[x] = []int{}
                }
        
                base *= 10 // 基數(shù)*10
            }
        }

        熱心網(wǎng)友提供的補(bǔ)充5:

        補(bǔ)上python的實(shí)現(xiàn)代碼:

        def radixSort(nums):
            """
            基數(shù)排序,數(shù)組元素必須是正整數(shù)
            >>>nums = [334, 5, 67, 345, 7, 99, 4, 23, 78, 45, 1, 3453, 23424]
            >>>radixSort(nums)
            >>>[1, 4, 5, 7, 23, 45, 67, 78, 99, 334, 345, 3453, 23424]
            """
            #遍歷數(shù)組獲取數(shù)組最大值和最大值對(duì)應(yīng)的位數(shù)
            maxValue = nums[0]
            for n in nums:
                maxValue = max(n, maxValue)
            #迭代次數(shù)
            iterCount = len(str(maxValue))
            for i in range(iterCount):
                #定義桶,大小為10,對(duì)應(yīng)0-9
                bucket = [[] for _ in range(10)]
                for n in nums:
                    index = (n//10**i)%10
                    bucket[index].append(n)
                #nums數(shù)組清零,并合并桶內(nèi)元素至nums
                nums.clear()
                for b in bucket:
                    nums.extend(b)
                print(nums)
            return nums
        nums = [334, 5, 67, 345, 7, 99, 4, 23, 78, 45, 1, 3453, 23424]
        radixSort(nums)

        熱心網(wǎng)友提供的補(bǔ)充6:

        上面 Java 版本有點(diǎn)問題:

        for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
            // 考慮負(fù)數(shù)的情況,這里擴(kuò)展一倍隊(duì)列數(shù),其中 [0-9]對(duì)應(yīng)負(fù)數(shù),[10-19]對(duì)應(yīng)正數(shù) (bucket + 10)
            int[][] counter = new int[mod * 2][0];
        ....
        }

        counter 數(shù)組的定義,會(huì)隨著 mod 不斷乘 10 變得越來越大。理論上 counter 數(shù)組只需要容量為 20 就可以表示負(fù)數(shù)與正數(shù)的所有數(shù)字字符。

        另外,方法 getMaxDigit 計(jì)算數(shù)字的最大長(zhǎng)度,只考慮到最大值的長(zhǎng)度,沒有考慮當(dāng)存在負(fù)數(shù)時(shí),最小值負(fù)數(shù)的字符長(zhǎng)度也可能是最大的長(zhǎng)度。

        更新后的版本:

        /** 基數(shù)排序  */
        public class RadixSort  {
        
          public int[] sort(int[] arr) {
        
            int maxDigit = getMaxDigit(arr);
            return radixSort(arr, maxDigit);
          }
        
          /**   * 獲取最高位數(shù)   */  private int getMaxDigit(int[] arr) {
            int maxValue = getMaxValue(arr);
            int minValue = getMinValue(arr);
            return Math.max(getNumLength(maxValue), getNumLength(minValue));
          }
        
          private int getMaxValue(int[] arr) {
            int maxValue = arr[0];
            for (int value : arr) {
              if (maxValue < value) {
                maxValue = value;
              }
            }
            return maxValue;
          }
        
          private int getMinValue(int[] arr) {
            int minValue = arr[0];
            for (int value : arr) {
              if (minValue > value) {
                minValue = value;
              }
            }
            return minValue;
          }
        
          protected int getNumLength(long num) {
            if (num == 0) {
              return 1;
            }
            int lenght = 0;
            for (long temp = num; temp != 0; temp /= 10) {
              lenght++;
            }
            return lenght;
          }
        
          private int[] radixSort(int[] arr, int maxDigit) {
            int mod = 10;
            int dev = 1;
        
            for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
              // 考慮負(fù)數(shù)的情況,這里擴(kuò)展一倍隊(duì)列數(shù),其中 [0-9]對(duì)應(yīng)負(fù)數(shù),[10-19]對(duì)應(yīng)正數(shù) (bucket + 10)      int[][] counter = new int[20][0];
        
              for (int j = 0; j < arr.length; j++) {
                int bucket = ((arr[j] % mod) / dev) + 10;
                counter[bucket] = arrayAppend(counter[bucket], arr[j]);
              }
        
              int pos = 0;
              for (int[] bucket : counter) {
                for (int value : bucket) {
                  arr[pos++] = value;
                }
              }
            }
        
            return arr;
          }
        
          /**   * 自動(dòng)擴(kuò)容,并保存數(shù)據(jù)   *   * @param arr   * @param value   */  private int[] arrayAppend(int[] arr, int value) {
            arr = Arrays.copyOf(arr, arr.length + 1);
            arr[arr.length - 1] = value;
            return arr;
          }
        }
        以上為基數(shù)排序算法詳細(xì)介紹,插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等排序算法各有優(yōu)缺點(diǎn),用一張圖概括:

        關(guān)于時(shí)間復(fù)雜度

        平方階 (O(n2)) 排序 各類簡(jiǎn)單排序:直接插入、直接選擇和冒泡排序。

        線性對(duì)數(shù)階 (O(nlog2n)) 排序 快速排序、堆排序和歸并排序;

        O(n1+§)) 排序,§ 是介于 0 和 1 之間的常數(shù)。 希爾排序

        線性階 (O(n)) 排序 基數(shù)排序,此外還有桶、箱排序。

        關(guān)于穩(wěn)定性

        穩(wěn)定的排序算法:冒泡排序、插入排序、歸并排序和基數(shù)排序。

        不是穩(wěn)定的排序算法:選擇排序、快速排序、希爾排序、堆排序。

        名詞解釋:

        n:數(shù)據(jù)規(guī)模

        k:"桶"的個(gè)數(shù)

        In-place:占用常數(shù)內(nèi)存,不占用額外內(nèi)存

        Out-place:占用額外內(nèi)存

        穩(wěn)定性:排序后 2 個(gè)相等鍵值的順序和排序之前它們的順序相同

        文檔

        基數(shù)排序法

        基數(shù)排序是一種非比較型整數(shù)排序算法,其原理是將整數(shù)按位數(shù)切割成不同的數(shù)字,然后按每個(gè)位數(shù)分別比較。由于整數(shù)也可以表達(dá)字符串(比如名字或日期)和特定格式的浮點(diǎn)數(shù),所以基數(shù)排序也不是只能使用于整數(shù)。
        推薦度:
        為你推薦
        資訊專欄
        熱門視頻
        相關(guān)推薦
        冒泡排序法例子 桶排序算法的代碼 計(jì)數(shù)排序算法實(shí)例 計(jì)數(shù)排序算法python c語言數(shù)據(jù)結(jié)構(gòu)堆排序算法 實(shí)現(xiàn)快速排序算法 歸并排序算法過程圖解 希爾排序的算法過程 選擇排序是穩(wěn)定的嗎 基數(shù)排序的詳細(xì)過程 冒泡排序代碼解讀 桶排序算法c語言 什么是計(jì)數(shù)排序 堆排序初始堆 快速排序算法python 歸并排序比較次數(shù) 希爾排序算法時(shí)間復(fù)雜度 直接選擇排序算法 基數(shù)排序算法思想 冒泡排序優(yōu)化 選擇排序c語言 希爾排序過程 歸并排序是穩(wěn)定排序嗎 與動(dòng)物有關(guān)的古詩(shī) 快速排序算法圖解 堆排序算法實(shí)現(xiàn) 計(jì)數(shù)排序 桶排序思想 冒泡排序算法是幾層循環(huán) 基數(shù)排序是一種基于 簡(jiǎn)單選擇排序算法 希爾排序法實(shí)例 歸并排序奇數(shù)個(gè)怎么排 有關(guān)動(dòng)物的古詩(shī) 快速排序c++代碼 堆排序第一趟怎么求 c語言計(jì)數(shù)排序算法 桶排序基本算法 c語言冒泡排序法流程圖 基數(shù)排序算法數(shù)據(jù)結(jié)構(gòu)
        Top