From 7924704c90275ed534b411c7640bcd420b07081b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=89=B4=E5=AE=A2?= Date: Mon, 18 May 2020 17:12:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=8C=E5=B0=94=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ShellSort.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/ShellSort.java diff --git a/src/ShellSort.java b/src/ShellSort.java new file mode 100644 index 0000000..eaf7ccc --- /dev/null +++ b/src/ShellSort.java @@ -0,0 +1,31 @@ +/** + * 希尔排序 + */ + public class ShellSort { + public static void shellSort(int[] a){ + int length = a.length; + for (int gap = length/2; gap > 0; gap /= 2){ + for (int i = 0; i < gap; i++) { + for (int j = i + gap; j < length; j += gap){ //每个子序列都从第二个开始 + if (a[j] < a[j - gap]){ + int temp = a[j]; + int k = j; + while ( k >= gap && a[k-gap] > temp){ + a[k] = a[k-gap]; + k -= gap; + } + a[k] = temp; + } + } + } + } + + } + public static void main(String[] args){ + int [] a = {9,2,5,3,10,1,4,8,0,7,6}; + shellSort(a); + for (int anA : a) { + System.out.print(anA + " "); + } + } +} \ No newline at end of file