Skip to content

Commit

Permalink
输出改变
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokexiang committed Nov 24, 2021
1 parent 66b899d commit d3095b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

/**
* @author xiaokexiang
* @link https://www.jianshu.com/p/d730ae586cf3
* @since 2021/11/15
* 先将整个待排序的记录序列分割成为 若干子序列分别进行直接插入排序,待整个序列中的记录"基本有序"时,再对全体记录进行依次直接插入排序。
* 这里的基本有序是指相隔K个间隔的元素有:数组中任意间隔为h的元素都是有序的
* 希尔排序是插入排序的升级版。
* @link https://www.jianshu.com/p/d730ae586cf3
*/
public class ShellSort implements SortExample<Integer> {

@Override
public void sort(Integer[] ts, boolean show) {
// 相对有序,关键在于这个h间隔
int n = ts.length;
int h = 1;
while (h < n / 3) {
h = h * 3 + 1;
}
// h最后一定是1才行,这样才会执行最终的插入排序
while (h >= 1) {
for (int i = h; i < n; i++) {
for (int j = i; j >= h && less(ts[j], ts[j - h]); j -= h) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.spring.common.algorithm.sort;

import java.util.Arrays;
import java.util.StringJoiner;

/**
* @author xiaokexiang
Expand Down Expand Up @@ -30,7 +31,9 @@ default void exchange(T[] a, int i, int j) {
* 打印数组
*/
default void show(T[] ts) {
Arrays.stream(ts).forEach(System.out::print);
StringJoiner joiner = new StringJoiner(",");
Arrays.stream(ts).forEach(s -> joiner.add(String.valueOf(s)));
System.out.println(joiner.toString());
}

/**
Expand Down

0 comments on commit d3095b1

Please sign in to comment.