多趟桶式排序bucket sort

      如果有N个整数,范围为1到M(或者0 到M-1),我们可以利用这些信息得到一种快速排序算法,称之为桶式排序 bucket sort。我们初始化以个大小为M的各项值为零COUNT的数组。当第一个数据Ai输入时COUNT[Ai]加一。这种算法可以对于输入数据之间相差较小时,运行速度很快。如果数据的最大值和最小值相差很大时,算法运行时会很费内存。如只输入2个数据,范围为0-999。按照上述的说明,要定义一个大小为1000的数组。

      鉴于该算法的缺点,提出了一种多趟桶式排序算法。设我们有10个数,范围为0-999,用多趟桶式排序算法进行排序。多趟桶式排序的算法文字说明详见《数据结构与算法描述》的P55.

数据输入:有10个数,范围为0到999。程序运行按照下述格式输入数据:
[root@lumotuwe] gcc cas.c -o cas
[root@lumotuwe] ./cas 64 8 216 512 27 729 0 1 343 125


代码如下:

cas.h

#ifndef _CARDSORT_H
struct cardsort 
{
	int number;
	int * data;
	struct cardsort * next;
};

typedef struct cardsort * list;



/***************************************
function: get the pointer on position in the list
***************************************/
list get_pointer(list head,int position)
{
	list tmp;
	tmp=head;
	while(position--)
	{
	tmp = tmp->next;
	}
	return tmp;
}
void send_data_to_list(int data,int position,list head)
{
	list tmp;	
	tmp=get_pointer(head,position);
	*(tmp->data+tmp->number)=data;
        tmp->number++;
}

void printf_data_of_list(list head,int num[])
{	
	int i=0,j=0,count=0;
	list tmp = head->next;
	for(j=0;j<10;j++)
	{
		printf("the data in list %d are",j);
		for(i=0;i<tmp->number;i++)
		{
		printf("--%3d--",*(tmp->data+i));
		num[count++]=*(tmp->data+i);
		}	
		tmp = tmp->next;
		printf("\n");
	}
	printf("the nun[] are");
	for(j=0;j<10;j++)
	{
		printf("--%d--",num[j]);
	}
	printf("\n");
        tmp=head->next;
	for(i=0;i<10;i++)
	{
		tmp->number = 0;
	        tmp = tmp->next;
	}
}


#endif


 

cas.c

/************************************************************************
*	File Name 			:multi card sort
*	Copyright			:scut,All Rights Reserved.
*	Create Data			:2012/11/20	
*	Author				:lumotuwe	
*	Abstruct Description		:
*
************************************************************************/

#include<stdio.h>
#include<string.h>
#include "cas.h"
#include<stdlib.h>
int main(int argc, char ** argv)
{
	int max,lev0;
	list head,tmp;
	int i,j,len,num[100];
	for(i=0;i<=99;i++) num[i]=0;		
	if(argc != 11)
		printf("input erro\n");
	/*
	Converting string to int;
	*/
	for(i=1 ; i<=argc-1 ;i++)
	{
		len = strlen(argv[i]);
		for(j=0; j<len; )
			{
				num[i-1]+=(int)(argv[i][j]-48);
				if(j<len-1)num[i-1]*=10;
				j++;
			}
	}
	//init the head pointer
	head = (struct cardsort *)malloc(sizeof(struct cardsort));
	head->number = 10;
	//init for list
	tmp = head;
	for(i=0;i<=9;i++)
	{
		tmp->next = (struct cardsort *)malloc(sizeof(struct cardsort));
	        tmp = tmp->next;
		tmp->number = 0;
		tmp->next   = NULL;
		tmp->data   =(int *)malloc(40);
	}
	//the first step to card sort
        for(i=0;i<=9;i++)
	{
		lev0 = num[i]%10;	
		switch (lev0)
		{
		case 0 : send_data_to_list(num[i],1,head);break;
		case 1 : send_data_to_list(num[i],2,head);break;
		case 2 : send_data_to_list(num[i],3,head);break;
		case 3 : send_data_to_list(num[i],4,head);break;
		case 4 : send_data_to_list(num[i],5,head);break;
		case 5 : send_data_to_list(num[i],6,head);break;
		case 6 : send_data_to_list(num[i],7,head);break;
		case 7 : send_data_to_list(num[i],8,head);break;
		case 8 : send_data_to_list(num[i],9,head);break;
		case 9 : send_data_to_list(num[i],10,head);break;
		}
	}
	printf("the result of the first step to card sort\n");
	printf_data_of_list(head,num);
	printf("------------------------------------------\n");
	//the second step to card sort
        for(i=0;i<=9;i++)
	{
		lev0 = (num[i]/10)%10;	
		switch (lev0)
		{
		case 0 : send_data_to_list(num[i],1,head);break;
		case 1 : send_data_to_list(num[i],2,head);break;
		case 2 : send_data_to_list(num[i],3,head);break;
		case 3 : send_data_to_list(num[i],4,head);break;
		case 4 : send_data_to_list(num[i],5,head);break;
		case 5 : send_data_to_list(num[i],6,head);break;
		case 6 : send_data_to_list(num[i],7,head);break;
		case 7 : send_data_to_list(num[i],8,head);break;
		case 8 : send_data_to_list(num[i],9,head);break;
		case 9 : send_data_to_list(num[i],10,head);break;
		}
	}
	printf("the result of the second step to card sort\n");
	printf_data_of_list(head,num);
        printf("------------------------------------------\n");
	//the third step to card sort
        for(i=0;i<=9;i++)
	{
		lev0 = num[i]/100;	
		switch (lev0)
		{
		case 0 : send_data_to_list(num[i],1,head);break;
		case 1 : send_data_to_list(num[i],2,head);break;
		case 2 : send_data_to_list(num[i],3,head);break;
		case 3 : send_data_to_list(num[i],4,head);break;
		case 4 : send_data_to_list(num[i],5,head);break;
		case 5 : send_data_to_list(num[i],6,head);break;
		case 6 : send_data_to_list(num[i],7,head);break;
		case 7 : send_data_to_list(num[i],8,head);break;
		case 8 : send_data_to_list(num[i],9,head);break;
		case 9 : send_data_to_list(num[i],10,head);break;
		}
	}
	printf("the result of the third step to card sort\n");
	printf_data_of_list(head,num);
 	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值