You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -69,6 +103,7 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
69
103
* @param value The value to look for.
70
104
* @return The index of the value if found in the array and -1 otherwise.
71
105
*/
106
+
72
107
exportfunctionbinarySearch(
73
108
values: number[],
74
109
start: number,
@@ -80,20 +115,24 @@ export function binarySearch(
80
115
return-1;
81
116
}
82
117
83
-
constpivotIndex=Math.floor((start+end)/2);// The index in the middle of the array.
118
+
constpivotIndex=Math.floor((start+end)/2);// The index in the middle of the array. //this is a variable "pivotIndex" that finds the middle values of our array
119
+
if(values[pivotIndex]===value){
120
+
//this function checks if middle value index of our array is equal to the value in our array
121
+
returnpivotIndex;// returns said value if it is
122
+
}elseif(values[pivotIndex]>value){
123
+
// checks if middle value is greater than our targett value, if it is than our target is in left half
124
+
returnbinarySearch(values,start,pivotIndex-1,value);//calls the binarySearch function but range is narrowed down to left side from start to pivotIndex -1
125
+
}else{
126
+
//if middle value is less than the target value then the target must be in the right half
127
+
returnbinarySearch(values,pivotIndex+1,end,value);// calls function binarySearch again and limit range to right side from pivotIndex +1 to end
128
+
}
84
129
85
130
// TODO(you): Finish implementing this algorithm
86
131
87
132
// If values[pivotIndex] is equal to value then return `pivotIndex`.
88
133
// Else if values[pivotIndex] is greater than the value, then
89
134
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
90
135
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
0 commit comments