Skip to content

Commit 7b23fcb

Browse files
authored
Update 3.insertionSort.md
1 parent da9dcac commit 7b23fcb

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

3.insertionSort.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121

2222
```js
2323
function insertionSort(arr) {
24-
var len = arr.length;
25-
var preIndex, current;
26-
for (var i = 1; i < len; i++) {
24+
var preIndex;
25+
for (var i = 1; i < arr.length; i++) {
2726
preIndex = i - 1;
28-
current = arr[i];
29-
while(preIndex >= 0 && arr[preIndex] > current) {
30-
arr[preIndex+1] = arr[preIndex];
27+
while(preIndex >= 0 && arr[preIndex] > arr[i]) {
28+
arr[preIndex + 1] = arr[preIndex];
3129
preIndex--;
3230
}
33-
arr[preIndex+1] = current;
31+
arr[preIndex + 1] = arr[i];
3432
}
3533
return arr;
3634
}

0 commit comments

Comments
 (0)