C 桶排序算法

C 桶排序算法,c,algorithm,data-structures,C,Algorithm,Data Structures,如何更改此处的代码以处理数组中的浮点值,当我试图编译代码时,出现了一个错误 所以我需要的是,我的代码可以处理浮点值,不仅仅是int,如果我添加了一个包含int值的数组,它就可以正常工作,但是对于浮点值,它会给我一个错误 如何更改此处的代码以处理数组中的浮点值,当我试图编译代码时,出现了一个错误 所以我需要的是,我的代码可以处理浮点值,不仅仅是int,如果我添加了一个包含int值的数组,它就可以正常工作,但是对于浮点值,它会给我一个错误 #包括 #包括 #定义NARRAY 100//数组大小 #定

如何更改此处的代码以处理数组中的浮点值,当我试图编译代码时,出现了一个错误

所以我需要的是,我的代码可以处理浮点值,不仅仅是int,如果我添加了一个包含int值的数组,它就可以正常工作,但是对于浮点值,它会给我一个错误

如何更改此处的代码以处理数组中的浮点值,当我试图编译代码时,出现了一个错误

所以我需要的是,我的代码可以处理浮点值,不仅仅是int,如果我添加了一个包含int值的数组,它就可以正常工作,但是对于浮点值,它会给我一个错误

#包括
#包括
#定义NARRAY 100//数组大小
#定义NBUCKET 100//桶数
#定义间隔100//每个铲斗容量
结构节点{
int数据;
结构节点*下一步;
};
空箱排序(整数arr[]);
结构节点*插入排序(结构节点*列表);
作废打印(int arr[]);
作废打印桶(结构节点*列表);
int getBucketIndex(int值);
//排序功能
无效BucketSort(内部阵列[]){
int i,j;
结构节点**bucket;
//创建存储桶并分配内存大小
bucket=(结构节点**)malloc(sizeof(结构节点*)*NBUCKET);
//初始化空桶
对于(i=0;i数据=arr[i];
当前->下一步=存储桶[位置];
桶[pos]=当前;
}
//打印桶及其元素
对于(i=0;i数据;
节点=节点->下一步;
}
}
返回;
}
//函数对每个桶的元素进行排序
结构节点*插入排序(结构节点*列表){
结构节点*k,*节点列表;
如果(列表==0 | |列表->下一步==0){
退货清单;
}
节点列表=列表;
k=列表->下一步;
节点列表->下一步=0;
while(k!=0){
结构节点*ptr;
if(节点列表->数据->k->数据){
结构节点*tmp;
tmp=k;
k=k->next;
tmp->next=节点列表;
节点列表=tmp;
继续;
}
对于(ptr=nodeList;ptr->next!=0;ptr=ptr->next){
如果(ptr->next->data->k->data)
打破
}
如果(ptr->next!=0){
结构节点*tmp;
tmp=k;
k=k->next;
tmp->next=ptr->next;
ptr->next=tmp;
继续;
}否则{
ptr->next=k;
k=k->next;
ptr->next->next=0;
继续;
}
}
返回节点列表;
}
int getBucketIndex(int值){
返回值/间隔;
}
无效打印(整型ar[]){
int i;
对于(i=0;idata);
cur=cur->next;
}
}
//驱动程序代码
内部主(空){
int数组[NARRAY]={0.50,100.00,99.97,51.20,53.90,28.10,25.50,66.40,65.70,0.00};
printf(“初始数组:”);
打印(阵列);
printf(“--------------\n”);
BucketSort(数组);
printf(“--------------\n”);
printf(“排序数组:”);
打印(阵列);
返回0;
}

使用此代码。我已经创建了自己的数据类型
iORf
。分别对int和float使用
typedef int iORf
typedef float iORf
。为此,您必须手动切换

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

#define NARRAY 100   // Array size
#define NBUCKET 100  // Number of buckets
#define INTERVAL 100 // Each bucket capacity

typedef int iORf; //float or int, currently int

struct Node
{
    iORf data;
    struct Node *next;
};

void BucketSort(iORf arr[]);
struct Node *InsertionSort(struct Node *list);
void print(iORf arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(iORf value);

// Sorting function
void BucketSort(iORf arr[])
{
    int i, j;
    struct Node **buckets;

    // Create buckets and allocate memory size
    buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);

    // Initialize empty buckets
    for (i = 0; i < NBUCKET; ++i)
    {
        buckets[i] = NULL;
    }

    // Fill the buckets with respective elements
    for (i = 0; i < NARRAY; ++i)
    {
        struct Node *current;
        int pos = getBucketIndex(arr[i]);
        current = (struct Node *)malloc(sizeof(struct Node));
        current->data = arr[i];
        current->next = buckets[pos];
        buckets[pos] = current;
    }

    // Print the buckets along with their elements
    for (i = 0; i < NBUCKET; i++)
    {
        printf("Bucket[%d]: ", i);
        printBuckets(buckets[i]);
        printf("\n");
    }

    // Sort the elements of each bucket
    for (i = 0; i < NBUCKET; ++i)
    {
        buckets[i] = InsertionSort(buckets[i]);
    }

    printf("-------------\n");
    printf("Bucktets after sorting\n");
    for (i = 0; i < NBUCKET; i++)
    {
        printf("Bucket[%d]: ", i);
        printBuckets(buckets[i]);
        printf("\n");
    }

    // Put sorted elements on arr
    for (j = 0, i = 0; i < NBUCKET; ++i)
    {
        struct Node *node;
        node = buckets[i];
        while (node)
        {
            arr[j++] = node->data;
            node = node->next;
        }
    }

    return;
}

// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list)
{
    struct Node *k, *nodeList;
    if (list == 0 || list->next == 0)
    {
        return list;
    }

    nodeList = list;
    k = list->next;
    nodeList->next = 0;
    while (k != 0)
    {
        struct Node *ptr;
        if (nodeList->data > k->data)
        {
            struct Node *tmp;
            tmp = k;
            k = k->next;
            tmp->next = nodeList;
            nodeList = tmp;
            continue;
        }

        for (ptr = nodeList; ptr->next != 0; ptr = ptr->next)
        {
            if (ptr->next->data > k->data)
                break;
        }

        if (ptr->next != 0)
        {
            struct Node *tmp;
            tmp = k;
            k = k->next;
            tmp->next = ptr->next;
            ptr->next = tmp;
            continue;
        }
        else
        {
            ptr->next = k;
            k = k->next;
            ptr->next->next = 0;
            continue;
        }
    }
    return nodeList;
}

int getBucketIndex(iORf value)
{
    return (int)value / INTERVAL;
}

void print(iORf ar[])
{
    int i;
    int flag = 0;
    iORf dummy = 1.5;
    if (dummy > 1)
        flag++;
    for (i = 0; i < NARRAY; ++i)
    {
        if (flag > 0)
            printf("%f ", ar[i]);
        else
            printf("%d ", ar[i]);
    }
    printf("\n");
}

// Print buckets
void printBuckets(struct Node *list)
{
    struct Node *cur = list;
    while (cur)
    {
        printf("%d ", cur->data);
        cur = cur->next;
    }
}

// Driver code
int main(void)
{
    iORf array[NARRAY] = {0.5, 100.00, 99.97, 51.20, 53.90, 28.10, 25.50, 66.40, 65.70, 0.00};

    printf("Initial array: ");
    print(array);
    printf("-------------\n");

    BucketSort(array);
    printf("-------------\n");
    printf("Sorted array: ");
    print(array);
    return 0;
}
#包括
#包括
#定义NARRAY 100//数组大小
#定义NBUCKET 100//桶数
#定义间隔100//每个铲斗容量
typedef-intiorf//float或int,当前为int
结构体类型
{
iORf数据;
结构节点*下一步;
};
无效BucketSort(iORf arr[]);
结构节点*插入排序(结构节点*列表);
无效打印(iORf arr[]);
作废打印桶(结构节点*列表);
int getBucketIndex(iORf值);
//排序功能
无效BucketSort(iORf arr[]
{
int i,j;
结构节点**bucket;
//创建存储桶并分配内存大小
bucket=(结构节点**)malloc(sizeof(结构节点*)*NBUCKET);
//初始化空桶
对于(i=0;i数据=arr[i];
当前->下一步=存储桶[位置];
桶[pos]=当前;
}
//打印桶及其元素
对于(i=0;i数据;
节点=节点->下一步;
}
}
返回;
}
//函数对每个桶的元素进行排序
结构节点*插入排序(结构节点*列表)
{
结构节点*k,*节点列表;
如果(列表==0 | |列表->下一步==
#include <stdio.h>
#include <stdlib.h>

#define NARRAY 100   // Array size
#define NBUCKET 100  // Number of buckets
#define INTERVAL 100 // Each bucket capacity

typedef int iORf; //float or int, currently int

struct Node
{
    iORf data;
    struct Node *next;
};

void BucketSort(iORf arr[]);
struct Node *InsertionSort(struct Node *list);
void print(iORf arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(iORf value);

// Sorting function
void BucketSort(iORf arr[])
{
    int i, j;
    struct Node **buckets;

    // Create buckets and allocate memory size
    buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);

    // Initialize empty buckets
    for (i = 0; i < NBUCKET; ++i)
    {
        buckets[i] = NULL;
    }

    // Fill the buckets with respective elements
    for (i = 0; i < NARRAY; ++i)
    {
        struct Node *current;
        int pos = getBucketIndex(arr[i]);
        current = (struct Node *)malloc(sizeof(struct Node));
        current->data = arr[i];
        current->next = buckets[pos];
        buckets[pos] = current;
    }

    // Print the buckets along with their elements
    for (i = 0; i < NBUCKET; i++)
    {
        printf("Bucket[%d]: ", i);
        printBuckets(buckets[i]);
        printf("\n");
    }

    // Sort the elements of each bucket
    for (i = 0; i < NBUCKET; ++i)
    {
        buckets[i] = InsertionSort(buckets[i]);
    }

    printf("-------------\n");
    printf("Bucktets after sorting\n");
    for (i = 0; i < NBUCKET; i++)
    {
        printf("Bucket[%d]: ", i);
        printBuckets(buckets[i]);
        printf("\n");
    }

    // Put sorted elements on arr
    for (j = 0, i = 0; i < NBUCKET; ++i)
    {
        struct Node *node;
        node = buckets[i];
        while (node)
        {
            arr[j++] = node->data;
            node = node->next;
        }
    }

    return;
}

// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list)
{
    struct Node *k, *nodeList;
    if (list == 0 || list->next == 0)
    {
        return list;
    }

    nodeList = list;
    k = list->next;
    nodeList->next = 0;
    while (k != 0)
    {
        struct Node *ptr;
        if (nodeList->data > k->data)
        {
            struct Node *tmp;
            tmp = k;
            k = k->next;
            tmp->next = nodeList;
            nodeList = tmp;
            continue;
        }

        for (ptr = nodeList; ptr->next != 0; ptr = ptr->next)
        {
            if (ptr->next->data > k->data)
                break;
        }

        if (ptr->next != 0)
        {
            struct Node *tmp;
            tmp = k;
            k = k->next;
            tmp->next = ptr->next;
            ptr->next = tmp;
            continue;
        }
        else
        {
            ptr->next = k;
            k = k->next;
            ptr->next->next = 0;
            continue;
        }
    }
    return nodeList;
}

int getBucketIndex(iORf value)
{
    return (int)value / INTERVAL;
}

void print(iORf ar[])
{
    int i;
    int flag = 0;
    iORf dummy = 1.5;
    if (dummy > 1)
        flag++;
    for (i = 0; i < NARRAY; ++i)
    {
        if (flag > 0)
            printf("%f ", ar[i]);
        else
            printf("%d ", ar[i]);
    }
    printf("\n");
}

// Print buckets
void printBuckets(struct Node *list)
{
    struct Node *cur = list;
    while (cur)
    {
        printf("%d ", cur->data);
        cur = cur->next;
    }
}

// Driver code
int main(void)
{
    iORf array[NARRAY] = {0.5, 100.00, 99.97, 51.20, 53.90, 28.10, 25.50, 66.40, 65.70, 0.00};

    printf("Initial array: ");
    print(array);
    printf("-------------\n");

    BucketSort(array);
    printf("-------------\n");
    printf("Sorted array: ");
    print(array);
    return 0;
}