Java ShellSort

/**
* <html>
* <body>
* <P> Copyright 1994-2018 JasonInternational </p>
* <p> All rights reserved.</p>
* <p> Created on 2018年4月10日 </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.algorithm.sorts; import java.util.ArrayList;
import java.util.List; /**
* Shellsort, also known as Shell sort or Shell's method, is an in-place
* comparison sort. It generalizes an exchanging sort, such as insertion or
* bubble sort, by starting the comparison and exchange of elements with
* elements that are far apart before finishing with neighboring elements.
* Starting with far apart elements can move some out-of-place elements into
* position faster than a simple nearest neighbor exchange.
* <p>
* Family: Exchanging.<br>
* Space: In-place.<br>
* Stable: False.<br>
* <p>
* Average case = depends on the gap<br>
* Worst case = O(n * log^2 n)<br>
* Best case = O(n)<br>
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Shell_sort">Shell Sort (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class ShellSort<T extends Comparable<T>> { private ShellSort() { } public static <T extends Comparable<T>> T[] sort(int[] shells, T[] unsorted) {
for (int gap : shells) {
// Allocate arrays
List<List<T>> subarrays = new ArrayList<List<T>>(gap);
for (int i = 0; i < gap; i++) {
subarrays.add(new ArrayList<T>(10));
}
// Populate sub-arrays
int i = 0;
int length = unsorted.length;
while (i < length) {
for (int j = 0; j < gap; j++) {
if (i >= length)
continue;
T v = unsorted[i++];
List<T> list = subarrays.get(j);
list.add(v);
}
}
// Sort all sub-arrays
sortSubarrays(subarrays);
// Push the sub-arrays into the int array
int k = 0;
int iter = 0;
while (k < length) {
for (int j = 0; j < gap; j++) {
if (k >= length)
continue;
unsorted[k++] = subarrays.get(j).get(iter);
}
iter++;
}
}
return unsorted;
} private static <T extends Comparable<T>> void sortSubarrays(List<List<T>> lists) {
for (List<T> list : lists) {
sort(list);
}
} /**
* Insertion sort
*
* @param list
* List to be sorted.
*/
private static <T extends Comparable<T>> void sort(List<T> list) {
int size = list.size();
for (int i = 1; i < size; i++) {
for (int j = i; j > 0; j--) {
T a = list.get(j);
T b = list.get(j - 1);
if (a.compareTo(b) < 0) {
list.set(j - 1, a);
list.set(j, b);
} else {
break;
}
}
}
}
}

  

最新文章

  1. 浅谈UDP(数据包长度,收包能力,丢包及进程结构选择)
  2. Android实现不重复启动APP的方法
  3. Nmap扫描手册
  4. [转]B树、B-树、B+树、B*树
  5. 自动生成pdf书签(仅适用于Adobe Acrobat on windows )
  6. fanghuangscannerV3 字典生成器
  7. php 导出csv文件
  8. table应用之colspan与rowspan
  9. ios开发——实战Swift篇&amp;简单项目的实现
  10. php 数组操作类(整合 给意见)
  11. pythonBasic
  12. 第9课_2_dbsoft安装
  13. Spring配置机制的优缺点 - Annotation vs XML
  14. 程序员必须知道的几个Git代码托管平台(转)
  15. shell脚本操作数据库
  16. 微信jssdk config:invalid signature 签名错误 ,问题排查过程
  17. 用servlet校验密码2
  18. 在Linux服务器上使用Vbox安装虚拟机
  19. Cracking The Coding Interview 9.7
  20. Android系统版本、Platform版本、SDK版本、gradle修改

热门文章

  1. Vue 缓存当前页面keep-alive
  2. 打印变量地址-0x%08x
  3. javascript已存在的对象构造器中是不能添加新的属性的:
  4. pytorch常用normalization函数
  5. Redis Sentinel 高可用服务架构搭建
  6. Qt编写自定义控件43-自绘电池
  7. python基础之内置模块(二)
  8. spring redistemplate中setHashValueSerializer的设置
  9. DELPHI中 screen.Cursor:=crhourglass; adoQuery.close; adoquery.Open; screen.Cursor:=crdefault;啥意思
  10. MySQL建表时添加备注以及查看某一张表的备注信息