希尔排序 Java 示例
谁能举个关于希尔排序的例子吗?我是这里的新人,必须学习希尔排序,但首先我必须找到一个 Java 希尔排序示例。我在谷歌上找到了一个例子,但是太难了。
Can anyone give me example about shell sort? I'm a new person in here who must learn about shell sort, but first I must find a Java shell sort example. I found one example in Google but it's too difficult.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在这里,这段代码非常简单:
我从一本名为 Data Structures and Algorithm Analysis in Java。是一本很容易理解的好书。我建议你读一下。
Here, this code is very simple :
I stole it from a book called Data Structures and Algorithm Analysis in Java. It is very good book easy to understand. I advise you to read it.
也许,这个java代码会对您有所帮助。
May be, this java code will help you.
希尔排序通过比较由多个位置间隔分隔的元素来改进插入排序。
这可以让元素向其预期位置迈出“更大的一步”。以越来越小的间隙尺寸对数据进行多次传递。希尔排序的最后一步是普通的插入排序,但到那时,数据数组就保证几乎已排序。
这段代码可能会帮助您更好地理解逻辑。
Shell sort improves insertion sort by comparing elements separated by a gap of several positions.
This lets an element take "bigger steps" toward its expected position. Multiple passes over the data are taken with smaller and smaller gap sizes. The last step of Shell sort is a plain insertion sort, but by then, the array of data is guaranteed to be almost sorted.
This code might help you in understanding the logic better.
以下是 Python 实现的 shell 排序的可视化:
Here is a visualization of shell sort for a python implementation:
这是一个例子:
Here's an example:
我发现理解希尔排序的最简单方法是将其分成几段:
I find the easiest way to understand shell sort is to break it down into segments:
经典原始类型实现:
PS:检查 此链接用于其他排序算法(尽管它们是用 C++ 编写的,但可以轻松移植到 Java)。
Classic primitive type implementation:
P.S.: Check this link for other sorting algorithms (they are in c++, though, easily portable to java).
列表 - 是 int[];
GapArray 取自 Marcin Ciura 的文章
http://sun.aei.polsl.pl/~mciura/publikacje/shellsort .pdf
list - is int[];
GapArray taken from arcticle of Marcin Ciura
http://sun.aei.polsl.pl/~mciura/publikacje/shellsort.pdf
以下是视频链接:https://youtu.be/SCBf7aqKQEY
这家伙制作了一个很好的 shell sort 视频!
和一个简单的代码:
Here is a video link: https://youtu.be/SCBf7aqKQEY
The guy has made a good video of shell sort!!
And a simple code:
使用这个
Use this
具有 3k+1 间隙的片段。
Snippet with 3k+1 gap.