zoukankan      html  css  js  c++  java
  • 桶排序

    桶排序的思想是创建n个桶

    桶排序要求元素的位数相同

    我们取到元素中最大值的最高位和最小值的最高位

    如果他们相同,可以往次高位选取,再相同再向下选取

    如果不同,得到最大值与最小值的那一位,创建a-b+1个桶

    比如158 247 354 666 541 234 980 147

    就要创建9个桶,因为最小值的最高位是1,最大值的最高位是9

    比如251 262 277 291 240 251 232 255 266

    最大值和最小值最高位相同,那么看次高位

    次高位最大值是9,最小值是3,所以要创建7个桶

    代码如下

    int GetTenMi(int count)
    {
        int temp = 1;
        while(count != 0)
        {
            temp *= 10;
            count--;
        }
        return temp;
    }
    
    void BucketSort(int* arr,int length)
    {
        if(arr == NULL || length <= 0) return;
        
        int min;
        int max;
     
        //如果数组长度为奇数
        if(length/2*2 != length)
        {
            max = arr[0];
            min = arr[0];
            //两个数中较大的
            int a;
            //两个数中较小的
            int b;
            for(int i=1;i<length;i+=2)
            {
                a = arr[i]>arr[i+1]?arr[i]:arr[i+1];
                b = arr[i]<arr[i+1]?arr[i]:arr[i+1];
                max = max>a?max:a;
                min = min<b?min:b;
            }
        }
        else
        {
            max = arr[0]>arr[1]?arr[0]:arr[1];
            min = arr[0]<arr[1]?arr[0]:arr[1];
            //两个数中较大的
            int a;
            //两个数中较小的
            int b;
            for(int i=2;i<length;i+=2)
            {
                a = arr[i]>arr[i+1]?arr[i]:arr[i+1];
                b = arr[i]<arr[i+1]?arr[i]:arr[i+1];
                max = max>a?max:a;
                min = min<b?min:b;
            }
        }
        //得到了最大值和最小值
        //开始创建桶,计算位数
    
        //得到最大值的位数
        int count1 = 0;
        int temp1 = max;
        while(temp1 != 0)
        {
            temp1 /=10;
            count1++;
        }
        //得到最小值的位数
        int count2 = 0;
        int temp2 = min;
        while(temp2 != 0)
        {
            temp2 /=10;
            count2++;
        }
        //最大值和最小值位数不同
        if(count1 != count2) return;
    
        temp1 = max;
        temp2 = min;
        //如果最大值的最高位和最小值的最高位相等
        while(temp1/(GetTenMi(count1-1)) == temp2/(GetTenMi(count1 -1)))
        {
            temp1 = temp1%(GetTenMi(count1-1));
            temp2 = temp2%(GetTenMi(count1-1));
            count1--;
        }
    
        //得到桶的长度
        int a = temp1/(GetTenMi(count1-1));
        int b = temp2/(GetTenMi(count1-1));
        Node** ptemp = (Node**)malloc(sizeof(Node*)*(a-b+1));
    
        memset(ptemp,0,sizeof(Node*)*(a-b+1));
        //往桶里装
        for(int i=0;i<length;i++)
        {
            Node* p = (Node*)malloc(sizeof(Node));
            p->nValue = arr[i];
            p->pNext = ptemp[arr[i]/(GetTenMi(count1-1))%10-b];
            ptemp[arr[i]/(GetTenMi(count1-1))%10-b] = p;
        }
        //排序
        for(int i=0;i<=a-b;i++)
        {
            if(ptemp[i] != NULL)
            {
                Node* pFather = ptemp[i];
                Node* pSon = ptemp[i]->pNext;
                int bj  = pFather->nValue;
                while(pSon != NULL)
                {
                    if(pSon->nValue < pFather->nValue)
                    {
                        pFather->nValue = pSon->nValue;
                        pSon->nValue = bj;
                        bj = pSon->nValue;
                    }
                    pFather = pSon;
                    pSon = pSon->pNext;
                }
            }
        }
        //装回原数组
        int j = 0;;
        for(int i=0;i<=a-b;i++)
        {
            if(ptemp[i] != NULL)
            {
                Node* p = ptemp[i];
                while(p != NULL)
                {
                    arr[j] = p->nValue;
                    j++;
                    p = p->pNext;
                }
            
            }
        }
    
    
    }

     没有写删除。。懒得写了

  • 相关阅读:
    windows开启PostgreSQL数据库远程访问
    Git使用介绍
    linux 常用工具记录及简介
    ubuntu18 安装坑点记录(华硕飞行堡垒)
    快手自动视频随机点赞脚本
    接触手机脚本编程------基于触动精灵的lua编程
    使电脑蜂鸣器发声小脚本
    tensorflow--非线性回归
    python笔记--------numpy
    python笔记--------二
  • 原文地址:https://www.cnblogs.com/TheQi/p/9125765.html
Copyright © 2011-2022 走看看