基數排序

基數排序:先按照最低位進行排序,而後對倒數第二位,以此類推。基於計數排序的基礎上的一種排序方法。屬於穩定排序,時間複雜度O(n),以空間換時間,須要額外的輔助空間。ios

計數排序:假設n個輸入元素的每個都是在0到k區間內的一個整數,其中k是一個整數。運行時間是O(n).數組

計數排序的基本思想是:對每個輸入元素x,肯定小於x的元素個數,從而,直接把x放在它輸出數組中的位置。ide

計數排序僞代碼:測試

CountingSort( A, B, k )     // A:輸入數組; B輸出數組;k 數組元素最大值spa

  C[ k ] = { 0 }code

  for    j  =  1  to  A.lengthblog

               C[A[j]] = C[A[j]] + 1                    // 統計數組中某一元素的個數排序

     for   i  = 2  to  kio

         C[ i ] = C[ i ] + C[ i - 1 ]                  // 計算數組中小於C[i] 的元素的個數event

  for   j  =  A.length  downto  1

         B[ C[ A[ j ] ] ] = A[ j ]                    //注:在代碼中,我在這裏是先進行:C[A[j]] = C[A[j]] - 1 , 而後 B[C[A[j]]] = A[j] 。C[A[j]]  的最大值是 A.length,會超出數組B的下標範圍。

         C[ A[ j ] ] = C[ A[ j ] ] - 1 

計數排序代碼:

 

#include<iostream>
#include<vector>
using namespace std;
vector<int> CountingSort(vector<int>A,int Max,int  Number)   //參數: K,最大數值; Number 數組中元素個數
{
    int max = Max + 1;
    vector<int> B(Number);
    vector<int>C(max, 0);
    
    for (int i = 0; i < Number; i++)
        C[A[i]] ++;

    for (int i = 1; i < max; i++)
        C[i] += C[i - 1];
    
    for (int j = Number - 1; j >= 0; j--)
    {
        C[A[j]]--;
        B[C[A[j]]] = A[j];
    }
    return B;
}

 

測試代碼:

int main()
{
    vector<int> arry = { 21, 97, 9, 1, 5, 8, 19, 4, 7, 10, 15, 17, 21, 41, 51, 61, 81, 71, 91, 100 };
    cout << "排序前" << endl;
    for (int i = 0; i < 20; i++)
        cout << arry[i] << " ";
    cout << endl;
    int Max = 100;
    int Number = 20;
    vector<int>B = CountingSort(arry, Max, Number);
    for (int i = 0; i < 20; i++)
        cout << B[i] << " ";
    cout << endl;
    cout << "BigThink" << endl;
    system("pause");
    return 0;
}
View Code

基數排序:

RadixSort(A,d)         // A 是輸入數組,d是數組中最大值的位數

for i=1 to d

     使用穩定排序法對A中數組的第 i位進行排序

基數排序代碼:

 

#include<iostream>
#include<vector>
using namespace std;
//求取輸入輸入的最大數字的位數
int GetInt(vector<int>A,int Number)    //Number 輸入數組個數
{
    int d = 1;
    int Max = A[0];
    for (int i = 0; i < Number; i++)
        if (A[i]>Max)
            Max = A[i];
    while (Max > 10)
    {
        Max = Max / 10;
        d++;
    }
    return d;
}

//基數排序法
vector<int> Radix_Sort(vector<int>A, int Number)     //參數 d 最大元素值的位數,Number是數組中元素個數
{
    int d = GetInt(A, Number);
    vector<int>B(Number);
    int radix = 1;
    for (int i = 1; i <= d; i++)
    {
        vector<int> Arry(Number, 0);
        vector<int> C(10, 0);
        for (int j = 0; j < Number; j++)
        {
            Arry[j] = (A[j] / radix) % 10;
            C[Arry[j]]++;
        }
        for (int j = 1; j < 10; j++)
            C[j] += C[j - 1];
        for (int j = Number - 1; j >= 0; j--)
        {
            B[C[Arry[j]]-1] = A[j];
            C[Arry[j]]--;
        }
        for (int j = 0; j < Number; j++)
            A[j] = B[j];
        radix = radix * 10;
    }
    return B;
}

 

測試代碼:

int main()
{
    vector<int>arry = { 3, 7, 9, 14, 25, 52, 36, 78, 95, 28, 11, 32, 24, 250, 620, 140, 8, 91, 20,620 };
    cout << "排序前" << endl;
    for (int i = 0; i < 20; i++)
        cout << arry[i] << " ";
    cout << endl;
    vector<int>B =  Radix_Sort(arry, 20);
    cout << "排序後:" << endl;
    for (int i = 0; i < 20; i++)
        cout << B[i] << " ";
    cout << endl;
    cout << "BigThink" << endl;
    system("pause");
    return 0;
}
View Code
相關文章
相關標籤/搜索