• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
kohakuhubo
博客园    首页    新随笔    联系   管理    订阅  订阅

希尔排序

希尔排序
 1 package com.berry.algorithm.sort;
 2 
 3 /**
 4  * Created by berry-h on 17-1-17.
 5  */
 6 public class ShellSort implements Sort{
 7 
 8     /**
 9      *希尔排序
10      * @param array
11      */
12     @Override
13     public void sort(int[] array) {
14 
15         int increment = array.length;
16         int i, j;
17 
18         do{
19             //计算增量
20             increment = increment/3 + 1;
21             //从等于增量值的索引处开始循环,与减去增量值得到的索引对应的值比较
22             for(i = increment;i < array.length;i++){
23                 if(array[i - increment] > array[i]){
24                     int temp = array[i];
25                     //以增量作为间隔并替换
26                     for(j = i - increment;j >= 0 && array[j] > temp;j = j - increment){
27                         array[j + increment] = array[j];
28                     }
29                     array[j + increment] = temp;
30                 }
31             }
32 
33         }while(increment > 1);
34 
35     }
36 }

 

posted @ 2017-01-17 16:29  kohakuhubo  阅读(109)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3