Skip to content

Commit c286ba4

Browse files
committed
Fix the indentation & codding style of insertion binary sort
1 parent 276ede0 commit c286ba4

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var array = [5,6,3,3,6,8,9,4,3];
2-
31
/**
42
* Modified version of insertionsort. It uses binary search for finding
53
* where the current element should be inserted. It's correct because
@@ -11,27 +9,27 @@ var array = [5,6,3,3,6,8,9,4,3];
119
* @param {array} array Sorted array
1210
*/
1311
function insertionBinarySort(array) {
14-
var current,
15-
middle,
16-
left,
17-
right;
18-
for (var i = 1; i < array.length; i += 1) {
19-
current = array[i];
20-
left = 0;
21-
right = i;
22-
middle = Math.floor((left + right) / 2);
23-
while (left < right) {
24-
if (array[middle] <= current)
25-
left = middle + 1;
26-
else
27-
right = middle - 1;
28-
middle = Math.floor((left + right) / 2);
29-
}
30-
for (var j = i; j > middle; j -= 1)
31-
array[j] = array[j - 1];
32-
array[j] = current;
12+
var current,
13+
middle,
14+
left,
15+
right;
16+
for (var i = 1; i < array.length; i += 1) {
17+
current = array[i];
18+
left = 0;
19+
right = i;
20+
middle = Math.floor((left + right) / 2);
21+
while (left < right) {
22+
if (array[middle] <= current) {
23+
left = middle + 1;
24+
} else {
25+
right = middle - 1;
26+
}
27+
middle = Math.floor((left + right) / 2);
3328
}
34-
return array;
35-
}
36-
37-
console.log(insertionBinarySort(array));
29+
for (var j = i; j > middle; j -= 1) {
30+
array[j] = array[j - 1];
31+
}
32+
array[j] = current;
33+
}
34+
return array;
35+
}

0 commit comments

Comments
 (0)