zoukankan      html  css  js  c++  java
  • 桶排序(BucketSort)

      桶排序:工作的原理是将数组分到有限数量的桶里。每个桶再个别排序(可能在使用别的排序算法,我这里用的单向链表在放入时就排好了顺序),最后依次把各个桶中的记录列出来得到有序序列。分布均匀的一组数,用桶排序效率很高。

      

      1.桶的个数是用这组数的个数确定的

      2.每个数值入桶的位置(桶的下标),是由(数值*数组长度/(最大数值+1))这个确定的。

      桶排序(BucketSort)概略图

      1.LinkedNode类

     1 public class LinkedNode {
     2     private int value;
     3     private LinkedNode next;
     4     
     5     public LinkedNode(int value)
     6     {
     7         this.value=value;
     8     }
     9 
    10     public int getValue() {
    11         return value;
    12     }
    13 
    14     public void setValue(int value) {
    15         this.value = value;
    16     }
    17 
    18     public LinkedNode getNext() {
    19         return next;
    20     }
    21 
    22     public void setNext(LinkedNode next) {
    23         this.next = next;
    24     }
    25     
    26     
    27 }

      2.BucketSort类

      

     1 import java.util.Arrays;
     2 
     3 public class BucketSort {
     4     //计算出入那桶(桶的下标)
     5     public static int hash(int value,int length,int max)
     6     {
     7         return ((value*length)/(max+1));
     8     }
     9 
    10     public static void main(String[] args) {
    11         int arr[]= {1045,3434,3566,4325,3234,6545,8237,4540,1550,5653,43534};
    12         System.out.println("开始:"+Arrays.toString(arr));
    13         BucketSort.sort(arr);
    14         System.out.println("最终;"+Arrays.toString(arr));
    15     }
    16     
    17     public static void sort(int arr[])
    18     {
    19         //1定义桶的大小
    20         int length=arr.length;
    21         LinkedNode bucket[]=new LinkedNode[length];
    22         //2求最大值
    23         int max=arr[0];
    24         for(int i=1;i<length;i++)
    25         {
    26             if(arr[i]>max)
    27                 max=arr[i];
    28         }
    29         //入桶
    30         for(int i=0;i<length;i++)
    31         {
    32             int value=arr[i];
    33             //得到入桶的下标
    34             int hash=hash(value,length,max);  
    35             if(bucket[hash]==null)
    36             {
    37                 bucket[hash]=new LinkedNode(value);
    38             }else {
    39                 insertInto(value,bucket[hash],bucket,hash);  //桶有值时入桶方法
    40             }
    41             
    42         }
    43         
    44         //出桶
    45         int i=0;
    46         for(LinkedNode node:bucket)
    47         {
    48             while(node!=null)
    49             {
    50                 arr[i++]=node.getValue();
    51                 node=node.getNext();
    52             }
    53         }
    54         
    55         
    56     }
    57     
    58     
    59     public static void insertInto(int value,LinkedNode head,LinkedNode bucket[],int hash)
    60     {
    61         LinkedNode newNode=new LinkedNode(value);
    62         if(value<head.getValue())
    63         {
    64             newNode.setNext(head);
    65             bucket[hash]=newNode;
    66             return ;
    67         }
    68         LinkedNode p=head;
    69         LinkedNode pre=null; 
    70         while(p!=null&&value>p.getValue())
    71         {
    72             pre=p;
    73             p=p.getNext();
    74         }
    75         if(p==null) 
    76         {
    77             pre.setNext(newNode);
    78         }else {
    79             pre.setNext(newNode);
    80             newNode.setNext(p);
    81         }
    82         
    83     }
    84 
    85 }

      总结:

    桶排序是以空间消耗换时间消耗,桶划分的越小,各个桶之间的数据越少,排序所用的时间也会越少,但相应的空间消耗就会增大。

  • 相关阅读:
    1、购买阿里云服务器
    struts2页面乱码解决方法
    struts第一次编写怎么都无法跳转页面
    时间转换与map集合
    struts2addActionError 、addFieldError、addActionMessage方法
    struts2基本流程
    struts2自定义类型转换器
    jsp聊天窗口随笔
    conn!=null与conn.isClosed的区别
    应用表单隐藏域进行提交
  • 原文地址:https://www.cnblogs.com/LgxBoKeYuan/p/10179510.html
Copyright © 2011-2022 走看看