线性排序----计数排序, 基数排序, 桶排序

/*
 描述:计数排序,它要求待排序元素是位于0到len之间的正整数,
主要适用于元素个数多,但是普遍不太大而且总小于len的情况
*/
#include <iostream>
using std::cout;
using std::endl;

void cnt_sort(unsigned int arr[], unsigned int ans[], int len)
{
        unsigned int *tmp = new unsigned int[len];
        unsigned int i = 0;
        // initalize the tmp[]
        for (i = 0; i < len; ++i)
                tmp[i] = 0;
        // calculate the count of the number of arr[]
        for (i = 0; i < len; ++i)
                ++tmp[arr[i]];
        // calculate the count of the number which is less than or equal to i
        for (i = 1; i < len; ++i)
                tmp[i] += tmp[i-1];
        // generate the result of the sorted array
        for (i = 0; i < len; ++i)
        {  
                --tmp[arr[i]];
                ans[tmp[arr[i]]] = arr[i];
        }  
        delete []tmp;
        tmp = NULL;
}

int main()
{
        unsigned int arr[8] = {2, 5, 3, 0, 2, 3, 0, 3};
        unsigned int ans[8] = { 0 };
        cnt_sort(arr, ans, 8);
        for (int i = 0; i < 8; ++i)
                cout << ans[i] << endl;
        return 0;
}

----------------------------------------------
/*
 基数排序:原理是将整数按位数切割成不同的数字,然后按每个位数进行排序
*/


#include <iostream>
using std::cout;
using std::endl;

int getValue(int num, int digit)
{
        for (int i = 1; i < digit; ++i)
                num /= 10;
        return num%10;
}

void cnt_sort(unsigned int arr[], int len, int digit)
{
        unsigned int *tmp = new unsigned int[10];
        unsigned int *ans = new unsigned int[len];
        int i = 0, t = 0;
        // initalize the tmp[]
        for (i = 0; i < 10; ++i)
                tmp[i] = 0;
        // calculate the count of the number of arr[]
        for (i = 0; i < len; ++i)
        {  
                t = getValue(arr[i], digit);
                ++tmp[t];
        }  
        for (i = 1; i < 10; ++i)
                tmp[i] += tmp[i-1];
        // generate the result of the sorted array,because of ascending order, so we should from end to begin
        for (i = len-1; i >= 0; --i)
        {  
                t = getValue(arr[i], digit);
                --tmp[t];
                ans[tmp[t]] = arr[i];
        }  
        // copy the ans[] to arr[]
        for (i = 0; i < len; ++i)
                arr[i] = ans[i];
        delete []tmp;
        tmp = NULL;
        delete []ans;
        ans = NULL;
}

void radix_sort(unsigned int arr[], int len, int width)
{
        for (int i = 1; i <= width; ++i)
                cnt_sort(arr, len, i);

}

int main()
{
        unsigned int arr[7] = {329, 457, 657, 839, 436, 720, 355};
        radix_sort(arr, 7, 3);
        for (int i = 0; i < 7; ++i)
                cout << arr[i] << endl;
        return 0;
}

 

-------------------------------------------
/*
 桶排序:输入是由一个随机过程产生的[0, 1)区间上均匀分布的实数。
将区间[0, 1)划分为n个大小相等的子区间(桶),每桶大小1/n:[0, 1/n),
[1/n, 2/n), [2/n, 3/n),…,[k/n, (k+1)/n ),…将n个输入元素分配到这些桶中,
对桶中元素进行排序
*/


#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <string.h> /* memset */

using std::cout;
using std::endl;

void init(double arr[], int n)
{
        cout << "***********init bucket info*************" << endl;
        srand(time(NULL));
        for (int i = 0; i < n; ++i)
        {  
                *(arr+i) = rand()%100*0.01;
                cout << *(arr+i) << "  ";
        }   
        cout << endl;
}

void insert_sort(double arr[], int n)
{
        int i, j;
        double tmp;
        for (i = 1; i < n; ++i)
        {  
                j = i-1;
                tmp = arr[i];
                while (j >= 0 && arr[j] > tmp)
                {  
                        arr[j+1] = arr[j];
                        --j;
                }  
                arr[j+1] = tmp;
        }   
}

void bucket_sort(double arr[], int n)
{
        int *num = new int[10];
        int *index = new int[10];
        memset(num, 0, 10*sizeof(int));
        memset(index, 0, 10*sizeof(int));
        int i, j, tmp;
        // calculate the size of every bucket
        for (i = 0; i < n; ++i)
        {
                tmp = int(arr[i]*100)/10;
                ++num[tmp];
        }
         // allocate the buckets' buffer
        double **bucket = new double *[n];
        for (i = 0; i < 10; ++i)
        {
                if (num[i] > 0)
                        bucket[i] = new double[num[i]];
                else
                        bucket[i] = NULL;
        }

        // store in the buckets
        for (i = 0; i < n; ++i)
        {
                tmp = int(arr[i]*100)/10;
                bucket[tmp][index[tmp]] = arr[i];
                ++index[tmp];
        }
        cout << "*********before bucket sort***********" << endl;
        for (i = 0; i < 10; ++i)
        {
                if (0 == num[i])
                        cout << "bucket " << i << " is an empty bucket" << endl;
                else
                {
                        cout << "bucket " << i << " elements is following" << endl;
                        for (j = 0; j < num[i]; ++j)
                                cout << bucket[i][j] << "  ";
                        cout << endl;
                }
        }

        for (i = 0; i < 10; ++i)
                insert_sort(bucket[i], num[i]);

        cout << "********after bucket sort***********" << endl;
        for (i = 0; i < 10; ++i)
        {
                if (0 == num[i])
                        cout << "bucket " << i << " is an empty bucket" << endl;
                else
                {
                        cout << "bucket " << i << " elements is following" << endl;
                        for (j = 0; j < num[i]; ++j)
                                cout << bucket[i][j] << "  ";
                        cout << endl;
                }
        }
        delete []num;
        num = NULL;
        delete []index;
        index = NULL;
        for (i = 0; i < 10; ++i)
        {
                delete []bucket[i];
                bucket[i] = NULL;
        }
        delete []bucket;
        bucket = NULL;
}

int main()
{
        double *arr = new double[10];
        init(arr, 10);
        bucket_sort(arr, 10);
        delete []arr;
        arr = NULL;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值