十大基础排序 · 十 --- 基数排序(稳定)

1. 分析

/*10.基数排序(RadixSort)
#分析:    
    1. 分桶: 按照个位分桶
    2. 把数据装入对应桶中;(尾添加)
    3. 把数据倒回原数组
    4. 再按照 十位分桶,重复2-3
    5. 再按照 百位分桶,从复2-3
    6. 直到最高位。

例子:
    原数组:23653121531101548266335336573061
    1. 按个位
    桶: 01234560110301121612482323653536634:
    桶515356336

    放回:110301216148223653536631535336
    2. 按照十位(实际7个桶,在此为便于理解)
    桶0:
    桶111015212123330353364:
    桶5653536616637:
    桶8:482
    放回:110151212330353366535361663482
    3. 按照百位
    桶015233035536111101212:
    桶333644825:
    桶6653663
    放回:1523303553611101213336482653663(有序)

实现方法:循环
*/

2. 源码

typedef struct radix
{
    int nValue;
    struct radix* pNext;
}Radix;

//数组, 数组长度, 表,当前位数(0-个,1-十,2-百, ...)
void LSD(int arr[], int nLength, Radix** pRadix, int nBit)
{
    int nBase = 1;
    int i;
    int nIndex;
    int nTemp;
    Radix* pTemp = NULL;
    Radix* pNode = NULL;
    //1计算基数
    while(nBit)
    {
        nBase *= 10;
        nBit--;
    }
    //2 数据入桶(尾添加)
    for (i = 0; i < nLength; i++)
    {
        nIndex = arr[i] / nBase % 10;
        pNode = (Radix*)malloc(sizeof(Radix));
        pNode->nValue = arr[i];
        pNode->pNext = NULL;

        pTemp = pRadix[nIndex];
        if (pTemp == NULL)
        {
            pRadix[nIndex] = pNode;
        }
        else
        {
            while (pTemp->pNext)
                pTemp = pTemp->pNext;
            pTemp->pNext = pNode;
        }

    }
    //3 数据倒回原数组 //4 释放小链表空间
    for (nTemp = 0, i = 0; i < 10; i++)
    {
        pTemp = pRadix[i];
        while (pTemp)
        {
            arr[nTemp] = pTemp->nValue;
            nTemp++;
            pNode = pTemp;

            pTemp = pTemp->pNext;
            free(pNode);
            pNode = NULL;
        }
    }

    //清空表!!!
    memset(pRadix, 0, sizeof(Radix*)* 10);

}

void RadixSort(int arr[], int nLength)
{
    if(arr == NULL || nLength<=0)
    {
        printf("RadixSort :invalid input!\n");
        return ;
    }
    int Max = arr[0];
    int nCount = 0;
    int nTemp;

    //1. 得到最大值
    for(nTemp=0;nTemp<nLength; nTemp++)
    {
        if(Max < arr[nTemp])
            Max = arr[nTemp];
    }
    //2.计算最大值的位数
    nTemp = Max;
    while(nTemp)
    {
        nTemp /= 10;
        nCount++;
    }
    //3. 来一个表,pRadix指向块指针空间,因此为2级;                        ;
    Radix* * pRadix = (Radix**)malloc( sizeof(Radix*) *10);//数字0-9 = 10个
    memset(pRadix, 0, sizeof(Radix*) *10);

    //4. 根据各位入桶出桶操作
    for(nTemp=0; nTemp < nCount;nTemp++)
    {
        //数组, 数组长度, 表,当前位数(0-个,1-十,2-百, ...)
        LSD(arr, nLength, pRadix, nTemp);
    }
    //5.释放哈希表空间(桶)
    free(pRadix);
    pRadix = NULL;
    return;

}

void Print(char* pStr, int arr[], int nLength)
{
    printf("%s : ", pStr);
    for(int i=0;i<nLength;i++)
    {
        printf("%d  ",arr[i]);
    }
    printf("\n");
}

int main()
{
    int arr[] = {4,5,52,8,4423,43,126,45,187,615,33,18,27,457};
    int nLength = sizeof(arr)/sizeof(arr[0]);
    RadixSort(arr,nLength);
    Print("RadixSort", arr,  nLength);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值