【C语言】桶排序

一、算法描述

  • 将数组划分为一个个的桶,桶的结构是链表,对桶内元素进行插入排序

二、桶排序代码

struct node
{
  int data;
  struct node *next;
};
// 对每个链表(桶)进行插入排序 
void insert_node(struct node **bucket, int data)
{
  struct node *p = (struct node *)malloc(sizeof(struct node));
  p->data = data;
  p->next = NULL;
  
  // 桶为空 
  if(*bucket == NULL)
  {
    *bucket = p;
  }else
  {
    struct node *pre = NULL;
    struct node *cur = *bucket;
    
    while(cur != NULL && cur->data <= data)
    {
      pre = cur;
      cur = cur->next;
    }
    // 对插入到第一个结点前的情况处理
    if(pre == NULL)
    {
      *bucket = p;
       p->next = cur;
    }else
    {       
      pre->next = p;
      p->next = cur;
    }
  } 
}

// k表示数据位数,3为表示取值范围[000-999] 
void bucket_sort(int a[], int length, int k)
{
  // 申请桶空间
  struct node **b = (struct node **)calloc(10,sizeof(struct node *));
  int i,j,m;
  // 将待排数据记录分配到桶 
  for(i=0; i<length; i++)
  {
    // 获取对应10个桶的标识 0-9 
    m = a[i];
    for(j=k; j>1; j--)
      m = m/10;
    // 分配到桶链表中
    insert_node(&b[m],a[i]); 
  }
  
  // 方便返回结果,复制到原数组a中 
  // 复制到数组a中
  struct node *p;
  for(i=0,j=0; i<10 && j<length; i++)
  {
    if(b[i] != NULL)
    {
      p = b[i];
      // 遍历每个桶元素 
      while(p != NULL)
      {
        a[j] = p->data;
        j++;
        p = p->next;
      } 
    }
    
  }
  // 释放存储空间
  for(i=0; i<10; i++)
  {
    while(b[i] !=NULL)
    {
      p = b[i];
      b[i] = p->next;
      free(p);
    } 
  }
  free(b);
}

三、测试代码

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

struct node
{
  int data;
  struct node *next;
};
// 对每个链表(桶)进行插入排序 
void insert_node(struct node **bucket, int data)
{
  struct node *p = (struct node *)malloc(sizeof(struct node));
  p->data = data;
  p->next = NULL;
  
  // 桶为空 
  if(*bucket == NULL)
  {
    *bucket = p;
  }else
  {
    struct node *pre = NULL;
    struct node *cur = *bucket;
    
    while(cur != NULL && cur->data <= data)
    {
      pre = cur;
      cur = cur->next;
    }
    // 对插入到第一个结点前的情况处理
    if(pre == NULL)
    {
      *bucket = p;
       p->next = cur;
    }else
    {       
      pre->next = p;
      p->next = cur;
    }
  } 
}

// k表示数据位数,3为表示取值范围[000-999] 
void bucket_sort(int a[], int length, int k)
{
  // 申请桶空间
  struct node **b = (struct node **)calloc(10,sizeof(struct node *));
  int i,j,m;
  // 将待排数据记录分配到桶 
  for(i=0; i<length; i++)
  {
    // 获取对应10个桶的标识 0-9 
    m = a[i];
    for(j=k; j>1; j--)
      m = m/10;
    // 分配到桶链表中
    insert_node(&b[m],a[i]); 
  }
  
  // 方便返回结果,复制到原数组a中 
  // 复制到数组a中
  struct node *p;
  for(i=0,j=0; i<10 && j<length; i++)
  {
    if(b[i] != NULL)
    {
      p = b[i];
      // 遍历每个桶元素 
      while(p != NULL)
      {
        a[j] = p->data;
        j++;
        p = p->next;
      } 
    }
    
  }
  // 释放存储空间
  for(i=0; i<10; i++)
  {
    while(b[i] !=NULL)
    {
      p = b[i];
      b[i] = p->next;
      free(p);
    } 
  }
  free(b);
}

int main()
{
	int N,i;
	printf("请输入需要排序的数的个数:\n");
	scanf("%d",&N);
	int a[N];
	printf("请输入需要排序的数:\n");
	for(i=0;i<N;i++)
		scanf("%d",&a[i]);
	bucket_sort(a,N,3);
	printf("桶排序的结果:\n");
	for(i=0;i<N;i++)
		printf("%d ",a[i]);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ོ栖落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值