桶排序

【0】README

0.1) 本文总结于 数据结构与算法分析,但源代码均为原创;旨在理清 桶排序的具体步骤;
0.2)要知道, 基数排序 等价于 多次桶排序,所以了解基数排序的前提是了解桶排序, 基数排序的详情,参见: http://blog.csdn.net/PacosonSWJTU/article/details/49687193


【1】桶式排序相关

1.1)算法前提:输入数据 A1, A2, A3, A4, …,AN 必须只由小于 M 的正整数组成;(如本源代码中,M=20, 然后 输入数据的大小为14)
1.2)算法描述:使用一个大小为M称为count 的数组, 它被初始化为全0, 于是count 有M个单元(桶) , 这些桶初始化为空; 当读 Ai 时, count[Ai] 自增1(也即是将 输入数据按照其对应的index 填装到桶中); 在所有的输入数据读入后, 扫描数组count, 打印出排序后的表。该算法用时 O(M+N);
1.3)该算法似乎干扰了下界, 但事实上并没有。 因为它使用了比简单比较更为强大的操作, 通过使适当的桶增值, 算法在单位时间内实质上执行了一个 M-路比较;这类似于用在可扩散列上的策略;
1.4)尽管桶式排序看似平凡用处不大, 但是实际上 却存在许多其输入只是一些小的整数的情况(基数排序就应用到了桶排序,如对若个个三位数进行排序),使用像快速排序这样的排序方法真的是小题大做了;
Attention)

  • A1)碰到 相同元素,应该如何处理?(所以,我们建立一个 数组链表 ArrayList, 动态建立的话,就相当于 建立 一个 “二维数组”)

【2】source code + printing result

2.1)download source code :
https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter7/bucketSort
2.2)source code at a glance:
[1st file : p189_ bucketSort.h]

#include <stdio.h>
#include <malloc.h>

#define MAX 10
#define ElementType int
#define Error(str) printf("\n\t error: %s \n",str) 

struct Node;
typedef struct Node *Node;

void bucketSort(Node* buckets, ElementType value);
void printBuckets(Node* data);
void printArray(ElementType data[], int size);
Node makeEmpty();

struct  Node
{
    int value;
    Node next;
};

[2nd file : p189_ bucketSort.c]

 #include "p189_bucketSort.h"

// allocate the memory for the bucket and bucket ptr
Node *initBuckets()
{
    Node* buckets;
    int i;

    buckets = (Node*)malloc(MAX * sizeof(Node));
    if(!buckets)
    {
        Error("out of space, from func bucketSort!");
        return NULL;    
    }   

    for(i=0; i<MAX; i++)    
        buckets[i] = makeEmpty();    
    return buckets;
}

//allocate the memory for the node and make it empty with evaluation of next
Node makeEmpty()
{
    Node temp; 

    temp = (Node)malloc(sizeof(struct Node));
    if(!temp)
    {
        Error("out of space, from func makeEmpty!");
        return NULL;    
    }
    temp->next = NULL;  

    return temp;
}

void bucketsToData(Node* buckets, ElementType *data)
{
    int i;
    int j;
    Node temp;

    i = 0;
    j = 0;
    while(i< MAX) // and now, we update the data array from buckets
    {
        temp = buckets[i]->next;
        while(temp)
        {
            data[j++] = temp->value;
            temp = temp->next;
        }
        i++;
    }
    // updating over    

}

// details of bucketSort for the input array data with size
void bucketSort(Node* buckets, ElementType data, int index)
{           
    Node temp;

    temp = buckets[index];
    while(temp->next)
        temp = temp->next;
    temp->next = makeEmpty();
    temp->next->value = data;
}


int main()
{ 
    int size;
    int i;
    Node* buckets;
    ElementType data[] = {9, 6, 3, 2, 7, 7, 1, 4, 1, 0, 3, 9, 1, 1};

    printf("\n\t====== test for bucket sorting towards the data array ======\n");
    printf("\n\t=== the initial array is as follows ===\n");
    size = 14;
    printArray(data, size);

    buckets = initBuckets();
    for(i=0; i<size; i++)
        bucketSort(buckets, data[i], data[i]);

    bucketsToData(buckets, data);
    printf("\n\t=== the buckets array is as follows ===\n");
    printArray(data, size);

    return 0;
}

void printBuckets(Node* buckets)
{
    int i;
    Node node;

    for(i = 0;i<MAX; i++)
    {   
        if(!buckets[i]->next)
            continue;
        for(node = buckets[i]->next; node != NULL; node = node->next)
            printf("\n\t buckets[%d] = %d", i, node->value);
    }

    printf("\n");
}

void printArray(ElementType data[], int size)
{
    int i;

    for(i = 0; i < size; i++)    
        printf("\n\t data[%d] = %d", i, data[i]);                    
    printf("\n\n");
} 

2.3)printing result:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值