zoukankan      html  css  js  c++  java
  • 希尔排序

    基于插入排序,添加新的特性。
    插入排序缺陷,多次排序。希尔排序保证右边不会有小的数据
    间隔计算:h=3h+1
    间隔减少计算:h=(h-1)/3

     1 public class ShellSort {
     2     
     3     /**
     4      * 
     5     * @Description: 希尔排序
     6     * @author dongye  
     7     * @date 2016年3月4日 上午10:41:56 
     8      */
     9     public static void sort(long[] arr){
    10         int h=1;//初始化条件
    11         //计算最大间隔
    12         while(h<arr.length/3){
    13             h=h*3+1; 
    14         }
    15         while(h>0){
    16             //进行插入排序
    17             long tmp=0;
    18             for (int i = h; i < arr.length; i++) {
    19                 tmp=arr[i];
    20                 int j=i;
    21                 while(j>h-1&&arr[j-h]>=tmp){
    22                     arr[j]=arr[j-h];
    23                     j-=h;
    24                 }
    25                 arr[j]=tmp;
    26             }
    27             //减少间隔
    28             h=(h-1)/3;
    29         }
    30         
    31     }
    32 
    33 }
  • 相关阅读:
    python 时间 时间戳 转换
    jsp mysql
    multi struts config
    mysql start
    struts logic tag
    jsp setProperty
    jstl fn tag
    write jsp tag
    use Bean in JSP
    jsp mysql JavaBean
  • 原文地址:https://www.cnblogs.com/snow1314/p/5241423.html
Copyright © 2011-2022 走看看