diff --git a/4.shellSort.md b/4.shellSort.md index e6d5f9c..6b6778a 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -9,6 +9,7 @@ 希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行依次直接插入排序。 +![image](https://user-images.githubusercontent.com/20939839/151767449-ea226730-9e45-4eb2-b246-70d9f1e0fed3.png) ## 1. 算法步骤 @@ -26,16 +27,19 @@ function shellSort(arr) { var len = arr.length, temp, gap = 1; - while(gap < len/3) { //动态定义间隔序列 - gap =gap*3+1; + while(gap < len / 3) { //动态定义间隔序列 + gap = gap * 3 + 1; } - for (gap; gap > 0; gap = Math.floor(gap/3)) { + for (gap; gap > 0; gap = Math.floor(gap / 3)) { for (var i = gap; i < len; i++) { temp = arr[i]; - for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) { - arr[j+gap] = arr[j]; + var j = i - gap; + + for (; j >= 0 && arr[j] > temp; j -= gap) { + arr[j + gap] = arr[j]; } - arr[j+gap] = temp; + + arr[j + gap] = temp; } } return arr;