File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change 22
22
## 2. JavaScript 代码实现
23
23
24
24
``` js
25
- function shellSort (arr ) {
25
+ /**
26
+ * arr: 排序数组
27
+ * gapLen: 排序增量
28
+ **/
29
+ function shellSort (arr , gapLen ) {
26
30
var len = arr .length ,
27
31
temp,
28
32
gap = 1 ;
29
- while (gap < len/ 3 ) { // 动态定义间隔序列
30
- gap = gap* 3 + 1 ;
33
+ while (gap < len / gapLen ) { // 动态定义间隔序列
34
+ gap = gap * gapLen + 1 ;
31
35
}
32
- for (gap; gap > 0 ; gap = Math .floor (gap/ 3 )) {
36
+ for (gap; gap > 0 ; gap = Math .floor (gap / gapLen )) {
33
37
for (var i = gap; i < len; i++ ) {
34
38
temp = arr[i];
35
- for (var j = i- gap; j >= 0 && arr[j] > temp; j-= gap) {
36
- arr[j+ gap] = arr[j];
39
+ for (var j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
40
+ arr[j + gap] = arr[j];
37
41
}
38
- arr[j+ gap] = temp;
42
+ arr[j + gap] = temp;
39
43
}
40
44
}
41
45
return arr;
You can’t perform that action at this time.
0 commit comments