Skip to content

Commit 46d7514

Browse files
committed
Update the names to make ES5 and ES6 solutions more consistent
I basically change the names to make the function more explicit and clear: 1. list to sortedList 2. mid to middle
1 parent a010e5c commit 46d7514

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

01_introduction_to_algorithms/ES6/01_binary_search.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const binarySearch = (list, item) => {
1+
const binarySearch = (sortedList, item) => {
22
let low = 0;
3-
let high = list.length - 1;
3+
let high = sortedList.length - 1;
44

55
while (low <= high) {
6-
const mid = Math.floor((low + high) / 2);
7-
const guess = list[mid];
6+
const middle = Math.floor((low + high) / 2);
7+
const guess = sortedList[middle];
88

99
if (guess === item) {
10-
return mid;
10+
return middle;
1111
}
1212
if (guess > item) {
13-
high = mid - 1;
13+
high = middle - 1;
1414
} else {
15-
low = mid + 1;
15+
low = middle + 1;
1616
}
1717
}
1818

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
'use strict';
22

3-
function binary_search(list, item) {
4-
let low = 0;
5-
let high = list.length - 1;
3+
function binarySearch(sortedList, item) {
4+
var low = 0;
5+
var high = sortedList.length - 1;
66

77
while (low <= high) {
8-
let mid = Math.floor((low + high) / 2);
9-
let guess = list[mid];
8+
var middle = Math.floor((low + high) / 2);
9+
var guess = sortedList[middle];
10+
1011
  if (guess === item) {
11-
return mid;
12+
return middle;
1213
}
1314
if (guess > item) {
14-
high = mid - 1;
15+
high = middle - 1;
1516
} else {
16-
low = mid + 1;
17+
low = middle + 1;
1718
}
1819
}
1920

2021
return null;
2122
}
2223

23-
const my_list = [1, 3, 5, 7, 9];
24+
var my_list = [1, 3, 5, 7, 9];
2425

25-
console.log(binary_search(my_list, 3)); // 1
26-
console.log(binary_search(my_list, -1)); // null
26+
console.log(binarySearch(my_list, 3)); // 1
27+
console.log(binarySearch(my_list, -1)); // null

0 commit comments

Comments
 (0)