Comments (5)
@keqizwl 没有错的
from cs-notes.
import java.util.Arrays;
public class Shell {
public static void sort(Comparable[] a) {
int N = a.length;
int h = 1;
while (h < N / 3) {
h = 3 * h + 1; // 1, 4, 13, 40, ...
}
while (h >= 1) {
for (int i = h; i < N; i++) {
for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
exch(a, j, j - h);
}
}
h = h / 3;
}
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
private static boolean less(Comparable x, Comparable y) {
return x.compareTo(y) < 0;
}
public static void main(String[] args) {
Integer[] a = {1, 3, 2, 4, 8, 7, 0};
sort(a);
System.out.println(Arrays.toString(a));
}
}
[0, 1, 2, 3, 4, 7, 8]
from cs-notes.
排序没错,因为当h为1的时候就是插入排序,肯定能排正确,但是算法不是正确的shell算法,你可以搜索下shell算法实现,对比下。
from cs-notes.
@keqizwl 麻烦给以下比较权威的资料,我找到的方法都是这么实现的。
from cs-notes.
不好意思,是我弄错了,你实现是对的
from cs-notes.
Related Issues (20)
- no se vale
- 题解-排序Kth Largest Element in an Array翻译有误 HOT 2
- 基础知识
- 我也在打造这方面的知识库:www.yuque.com/it-coach 能一起合作吗
- Kaschey758
- Suggestion HOT 1
- Kaschey758
- 89840200099962655961 HOT 1
- 0888999999*89840200011162655961# HOT 2
- 学习
- leetcode最新pdf还有吗 HOT 10
- 0888999999#89840200011162655961
- 计算机网络 - 应用层.md -- 2. ARP 解析 MAC 地址
- 计算机操作系统 - 进程管理 进程调度算法部分,“先来先服务 first-come first-serverd”,serverd拼写错误
- 关于微服务的开发相关知识整理有没有 HOT 8
- 剑指Offer的 [树][二叉搜索树的第K的结点] 链接被newcoder修改 HOT 9
- java
- N
- 在线阅读网址打不开了 HOT 7
- 标题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cs-notes.