小言_互联网的博客

基数排序和桶排序的c语言实现

417人阅读  评论(0)

头文件

#ifndef _RadixSort_H
#define _RadixSort_H

#define MaxBucketLen 1000
#define RADIX_10 10

typedef int Bucket[MaxBucketLen];
typedef unsigned int Size;
typedef unsigned int MaxPos;

// 获取最高位
MaxPos GetMaxPos(int num);
// 找到数组最大的数
int MaxNum(int arr[], Size size);
// 获取某一位上的数字
int GetNumPos(int num, int pos);
// 基数排序
void RadixSort(int arr[], Size size);
// 桶排序
void BucketSort(int arr[], Size size);
// 打印数组
void Print(int arr[], Size size);
// 求10的次方
int PowTen(int n);

#endif

函数实现

#include "RadixSort.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Bucket bucket = {};

void BucketSort(int arr[], Size size)
{
    for (int i = 0; i != size; ++i) {
        bucket[arr[i]]++;
    }
    size = 0;
    for (int i = 0; i != MaxBucketLen; ++i) {
        if (bucket[i]) {
            for (int j = 0; j != bucket[i]; ++j) {
                arr[size] = i;
                size++;
            }
        }
    }
    memset(bucket, 0, sizeof(Bucket));
}

void Print(int arr[], Size size)
{
    for (int i = 0; i != size; ++i) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int MaxNum(int arr[], Size size)
{
    int max = 0;
    for (int i = 0; i != size; ++i) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    return max;
}

MaxPos GetMaxPos(int num)
{
    MaxPos maxpos = 1;
    if (num == 0) {
        return 1;
    }
    while (num/PowTen(maxpos)) {
        maxpos++;
    }

    return maxpos;
}

int PowTen(int n)
{
    int num = 10;
    if (n == 0) {
        return 1;
    }
    for (int i = 1; i != n; ++i) {
        num *= 10;
    }

    return num;
}

int GetNumPos(int num, int pos) 
{
    return (num / PowTen(pos-1)) % 10;
}

void RadixSort(int arr[], Size size)
{
    int *radixarr[RADIX_10];
    MaxPos maxpos;
    int maxnum;
    for (int i = 0; i != RADIX_10; ++i) {
        radixarr[i] = (int *)malloc(sizeof(int) * (size+1));
        radixarr[i][0] = 0; // 计数器
    }
    maxnum = MaxNum(arr, size);
    maxpos = GetMaxPos(maxnum);
    for (int i = 1; i != maxpos+1; ++i) {
        for (int j = 0; j != size; ++j) {
            int numpos = GetNumPos(arr[j], i);
            radixarr[numpos][0]++;
            radixarr[numpos][radixarr[numpos][0]] = arr[j];
        }
        size = 0;
        for (int j = 0; j != RADIX_10; ++j) {
            for (int k = 1; k <= radixarr[j][0]; ++k) {
                arr[size++] = radixarr[j][k];
            }
            radixarr[j][0] = 0;
        }
    }
}

转载:https://blog.csdn.net/qq_44049351/article/details/101453484
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场