package sort;
public class Shell {
@SuppressWarnings("rawtypes")
public static void sort(Comparable[] a) {
//将a[]按升序排列
int N = a.length; //数组长度
int h = 1;
while(h < N/3)
h = 3*h + 1; //1,4,13,40,121,364,1093,...
while(h >= 1) {
//将数组变为h有序
for(int i = h; i < N; i++) {
//将a[i]插入到a[i-h],a[i-2*h],a[i-3*h]...之中
for(int j = i; j >= h && less(a[j], a[j-h]); j-=h)
exch(a, j, j-h);
}
h = h/3;
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
private static void show(Comparable[] a) {
//在单行中打印数组
for(int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public static boolean isSorted(Comparable[] a) {
//测试数组元素是否有序
for(int i = 1; i < a.length; i++)
if(less(a[i], a[i-1]))
return false;
return true;
}
public static void main(String[] args) {
Comparable []a = {100,99,101,120,98,107,109,81,101,90};
sort(a);
assert isSorted(a);
show(a);
}
}
shell&insertion&selection.zip_排序算法
版权申诉
78 浏览量
2022-09-21
22:00:40
上传
评论
收藏 2KB ZIP 举报
寒泊
- 粉丝: 74
- 资源: 1万+
最新资源
- Screenshot_20240509_034911_com.tencent.mtt.jpg
- 基于python实现的医学影像体脂分割+源代码+文档说明(课程设计)
- 基于python实现的医学影像(MIR, CT )图像分割源码+文档说明(高分课程设计)
- 基于python+JavaScript实现的医学影像分割+源代码+文档说明+截图演示+数据(高分毕业设计)
- 基于U-net+pytorch实现的医学影像分割python源码+文档说明+数据+界面截图+博客介绍
- 课程设计-基于Pytorch实现MNIST数据集的手写数字识别源码+数据(Gui界面)+文档说明+模型
- 软件开发国家标准.xls
- pytorch-CNN-SBATM-ubuntudemo
- matplotlibdemo
- pytorch-CNN-dht11温湿度传感器笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈