-
Notifications
You must be signed in to change notification settings - Fork 2
/
shell_sort.js
223 lines (218 loc) · 4.85 KB
/
shell_sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
*/
/* 1. 希尔排序标准版,基于插入排序进行分组排序,步长按1/2缩减。 */
function shellSort1(arr) {
const len = arr.length
// 设置分组增量值(步长)为1/2的数组长度
let gap = Math.floor(len / 2)
// 根据步长得到子序列,如果间隔大于0,则表示还可以继续分组
while (gap > 0) {
for (let i = 0; i < len; i++) {
const current = arr[i]
let j = i
// 对子序列按照插入排序
while (j >= gap && current < arr[j - gap]) {
console.log('gap=' + gap + ' i=' + i + ' j=' + j + ' (j - gap)=' + (j - gap), 'arr:', arr)
arr[j] = arr[j - gap]
j -= gap
}
// 交换当前项
arr[j] = current
}
// 调整步长为1/2
gap = Math.floor(gap / 2)
}
return arr
}
/* 2. 希尔排序,基于插入排序进行分组排序,步长按3倍递减。 */
function shellSort2(arr) {
const len = arr.length
let gap = 1
// 初始步长按3倍递增,小于1/3数组长度
while (gap < Math.floor(len / 3)) {
gap = gap * 3 + 1
}
// 根据步长得到子序列,如果间隔大于0,则表示还可以继续分组
while (gap > 0) {
for (let i = gap; i < len; i++) {
const current = arr[i]
let j = i - gap
// 对子序列按照插入排序
for (; j >= 0 && arr[j] > current; j -= gap) {
console.log('gap=' + gap + ' i=' + i + ' j=' + j + ' (j + gap)=' + (j + gap), 'arr:', arr)
arr[j + gap] = arr[j]
}
arr[j + gap] = current
}
// 步长按3倍缩减
gap = Math.floor(gap / 3)
}
return arr
}
// test
;
(function () {
const arr1 = [33, 4, 15, 43, 323454, -7, 10.5, 1235, 200, 87431]
console.time('shellSort1')
console.log('origin shellSort1:', arr1)
console.log('shellSort1 sorted:', shellSort1(arr1))
console.timeEnd('shellSort1')
const arr2 = [33, 4, 15, 43, 323454, -7, 10.5, 1235, 200, 87431]
console.time('shellSort2')
console.log('origin shellSort2:', arr2)
console.log('shellSort2 sorted:', shellSort2(arr2))
console.timeEnd('shellSort2')
})()
/*
jarry@jarrys-MacBook-Pro shellsort %
jarry@jarrys-MacBook-Pro shellsort % node shell_sort.js
origin shellSort1: [
33, 4, 15,
43, 323454, -7,
10.5, 1235, 200,
87431
]
gap=5 i=5 j=5 (j - gap)=0 arr: [
33, 4, 15,
43, 323454, -7,
10.5, 1235, 200,
87431
]
gap=5 i=9 j=9 (j - gap)=4 arr: [
-7, 4, 15,
43, 323454, 33,
10.5, 1235, 200,
87431
]
gap=2 i=5 j=5 (j - gap)=3 arr: [
-7, 4, 15,
43, 87431, 33,
10.5, 1235, 200,
323454
]
gap=2 i=6 j=6 (j - gap)=4 arr: [
-7, 4, 15,
33, 87431, 43,
10.5, 1235, 200,
323454
]
gap=2 i=6 j=4 (j - gap)=2 arr: [
-7, 4, 15,
33, 87431, 43,
87431, 1235, 200,
323454
]
gap=2 i=8 j=8 (j - gap)=6 arr: [
-7, 4, 10.5,
33, 15, 43,
87431, 1235, 200,
323454
]
gap=1 i=4 j=4 (j - gap)=3 arr: [
-7, 4, 10.5,
33, 15, 43,
200, 1235, 87431,
323454
]
shellSort1 sorted: [
-7, 4, 10.5,
15, 33, 43,
200, 1235, 87431,
323454
]
shellSort1: 10.306ms
origin shellSort2: [
33, 4, 15,
43, 323454, -7,
10.5, 1235, 200,
87431
]
gap=4 i=5 j=1 (j + gap)=5 arr: [
33, 4, 15,
43, 323454, -7,
10.5, 1235, 200,
87431
]
gap=4 i=6 j=2 (j + gap)=6 arr: [
33, -7, 15,
43, 323454, 4,
10.5, 1235, 200,
87431
]
gap=4 i=8 j=4 (j + gap)=8 arr: [
33, -7, 10.5,
43, 323454, 4,
15, 1235, 200,
87431
]
gap=1 i=1 j=0 (j + gap)=1 arr: [
33, -7, 10.5,
43, 200, 4,
15, 1235, 323454,
87431
]
gap=1 i=2 j=1 (j + gap)=2 arr: [
-7, 33, 10.5,
43, 200, 4,
15, 1235, 323454,
87431
]
gap=1 i=5 j=4 (j + gap)=5 arr: [
-7, 10.5, 33,
43, 200, 4,
15, 1235, 323454,
87431
]
gap=1 i=5 j=3 (j + gap)=4 arr: [
-7, 10.5, 33,
43, 200, 200,
15, 1235, 323454,
87431
]
gap=1 i=5 j=2 (j + gap)=3 arr: [
-7, 10.5, 33,
43, 43, 200,
15, 1235, 323454,
87431
]
gap=1 i=5 j=1 (j + gap)=2 arr: [
-7, 10.5, 33,
33, 43, 200,
15, 1235, 323454,
87431
]
gap=1 i=6 j=5 (j + gap)=6 arr: [
-7, 4, 10.5,
33, 43, 200,
15, 1235, 323454,
87431
]
gap=1 i=6 j=4 (j + gap)=5 arr: [
-7, 4, 10.5,
33, 43, 200,
200, 1235, 323454,
87431
]
gap=1 i=6 j=3 (j + gap)=4 arr: [
-7, 4, 10.5,
33, 43, 43,
200, 1235, 323454,
87431
]
gap=1 i=9 j=8 (j + gap)=9 arr: [
-7, 4, 10.5,
15, 33, 43,
200, 1235, 323454,
87431
]
shellSort2 sorted: [
-7, 4, 10.5,
15, 33, 43,
200, 1235, 87431,
323454
]
shellSort2: 1.791ms
*/