Bucket Sort算法实现

Bucket Sort算法实现

/*
桶子法排序的主要思想:(限正整数)
从个位数起向左一位一位的排过去,最终把所有的数都排列完成,
排的是位数,而不是整个数。
时间复杂度:O(n)
*/
#include<cstdio>
#include<cstdlib>
#define LENGTH 4//mask size
#define MAX 16
#define LOOP sizeof(int)*8/LENGTH//整数的位数

using namespace std;

typedef struct Node
{
	int data;//original data
	int x;//working data
	struct Node *next;
}Node;

Node *head;

struct cell
{
	Node *first;
	Node *last;
}Bucket[MAX];

int mask=0x0F;

Node* ListGenerate(int input[],int n)//根据输入数组生成一个链表 
{
	Node *first,*last,*temp;
	int i;
	first=last=(Node *)malloc(sizeof(Node));
	if(!first)
	{
		printf("No enough memory!\n");
		exit(-1);
	}
	first->data=first->x=input[0];
	first->next=NULL;
	for(i=1;i<n;i++)
	{
		temp=(Node *)malloc(sizeof(Node));
		if(!temp)
		{
			printf("No enough memory!\n");
			exit(-1);
		}
		temp->data=temp->x=input[i];
		temp->next=NULL;
		last->next=temp;
		last=temp;
	}
	return first;
}

void Distribute(void)//根据数据各个位上的数字散列到桶中
{   //如果把输入数据看成是十进制的,则应有10组(0-9),如果最多5位数
	Node *temp;//则要分组,收集5次,一次1位;但有更好地办法,把那个数
	int index;//看成16进制,则一位的值为0-15,所以每次分组时用低4位
	while(head!=NULL)//与mask进行&操作,以取出低位的值
	{
		index=(head->x)&mask;
		(head->x)>>=LENGTH;
		if(Bucket[index].first==NULL)
			Bucket[index].first=Bucket[index].last=head;
		else
		{
			(Bucket[index].last)->next=head;
			Bucket[index].last=head;
		}
		temp=head->next;
		if(head->next!=NULL)
			head->next=NULL;
		head=temp;
	}
}

void Recollect(void)//从桶中合并数据到链表
{
	int i,j;
	for(i=0;i<MAX&&Bucket[i].first==NULL;i++)
		;
	head=Bucket[i].first;
	for(j=i+1;j<MAX;j++)
	{
		if(Bucket[j].first!=NULL)
		{
			(Bucket[i].last)->next=Bucket[j].first;
			i=j;
		}
	}
}

void PutBack(int input[])//存回数组
{
	Node *temp;
	int i;
	for(i=0;head!=NULL;i++)
	{
		input[i]=head->data;
		temp=head->next;
		free(head);
		head=temp;
	}
}

void BucketSort(int input[],int n)//排序
{
	int i,j;
	head=ListGenerate(input,n);
	for(i=1;i<=LOOP;i++)
	{
		for(j=0;j<MAX;j++)
			Bucket[j].first=Bucket[j].last=NULL;
		Distribute();
		Recollect();
	}
	PutBack(input);
}

int main(int argc,char *argv[])
{
	int input[]={122,433,234,786,34,23,536,2346,7424,13};
	BucketSort(input,sizeof(input)/sizeof(int));
	for(int i=0;i<sizeof(input)/sizeof(int);i++)
		printf("%d\n",input[i]);
	return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
K_Bucket算法是一种用来管理节点距离的数据结构,常用于P2P网络中。它可以快速找到最近的节点,并且可以动态地维护节点之间的距离。下面是一个简单的K_Bucket算法实现: ```python class Node: def __init__(self, id): self.id = id self.bucket = [] def distance(self, other): return abs(self.id - other.id) def add_node(self, node): if node.id == self.id: return if len(self.bucket) < K: self.bucket.append(node) else: self.bucket.sort(key=lambda n: self.distance(n)) farthest_node = self.bucket[-1] if self.distance(farthest_node) > self.distance(node): self.bucket[-1] = node farthest_node.add_node(self) def remove_node(self, node): self.bucket.remove(node) ``` 在这个实现中,我们定义了一个节点类Node,每个节点都有一个id和一个bucketbucket是一个列表,用来存储其他节点。当我们向一个节点添加新节点时,如果bucket中的节点数小于K,就直接将新节点添加到bucket中;否则,我们会按照距离将bucket中的节点排序,取出最远的节点farthest_node,如果新节点比farthest_node更接近当前节点,就将farthest_node添加到新节点的bucket中,然后将新节点添加到当前节点的bucket中,否则,就不做处理。 当我们从一个节点删除另一个节点时,只需要在bucket中找到这个节点并将其从列表中移除即可。 需要注意的是,这只是一个简单的K_Bucket算法实现,实际应用中还需要考虑一些其他的细节,例如节点的分布情况、节点失效等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值