Correcting a spelling mistake (#19845)
* Correcting a spelling mistake * Formatting and adding missing code * Removing extra code * Update index.md
This commit is contained in:
committed by
Tracey Bushman
parent
d14ec72fb8
commit
b6ad5afb70
@ -85,7 +85,7 @@ The two base cases for recursion would be:
|
||||
* Item is found
|
||||
|
||||
The Power of Binary Search in Data Systems (B+ trees):
|
||||
Binary Search Trees are very powerful because of their O(log n) search times, second to the hashmap data structure which uses a hasing key to search for data in O(1). It is important to understand how the log n run time comes from the height of a binary search tree. If each node splits into two nodes, (binary), then the depth of the tree is log n (base 2).. In order to improve this speed in Data System, we use B+ trees because they have a larger branching factor, and therefore more height. I hope this short article helps expand your mind about how binary search is used in practical systems.
|
||||
Binary Search Trees are very powerful because of their O(log n) search times, second to the hashmap data structure which uses a hashing key to search for data in O(1). It is important to understand how the log n run time comes from the height of a binary search tree. If each node splits into two nodes, (binary), then the depth of the tree is log n (base 2).. In order to improve this speed in Data System, we use B+ trees because they have a larger branching factor, and therefore more height. I hope this short article helps expand your mind about how binary search is used in practical systems.
|
||||
|
||||
The code for recursive binary search is shown below:
|
||||
|
||||
@ -96,18 +96,18 @@ function binarySearch(arr, item, low, high) {
|
||||
if (low > high) { // No more elements in the array.
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Find the middle of the array.
|
||||
var mid = Math.ceil((low + high) / 2);
|
||||
|
||||
if (arr[mid] === item) { // Found the item!
|
||||
return mid;
|
||||
}
|
||||
|
||||
|
||||
if (item < arr[mid]) { // Item is in the half from low to mid-1.
|
||||
return binarySearch(arr, item, low, mid-1);
|
||||
}
|
||||
|
||||
|
||||
else { // Item is in the half from mid+1 to high.
|
||||
return binarySearch(arr, item, mid+1, high);
|
||||
}
|
||||
@ -126,9 +126,9 @@ function binary_search(a, v) {
|
||||
return a[low] === v;
|
||||
} else {
|
||||
var mid = math_floor((low + high) / 2);
|
||||
return (v === a[mid])
|
||||
return (v === a[mid])
|
||||
||
|
||||
(v < a[mid])
|
||||
(v < a[mid])
|
||||
? search(low, mid - 1)
|
||||
: search(mid + 1, high);
|
||||
}
|
||||
@ -163,17 +163,17 @@ end
|
||||
### Example in C
|
||||
|
||||
```C
|
||||
int binarySearch(int a[], int l, int r, int x) {
|
||||
if (r >= l){
|
||||
int mid = l + (r - l)/2;
|
||||
if (a[mid] == x)
|
||||
return mid;
|
||||
if (arr[mid] > x)
|
||||
return binarySearch(arr, l, mid-1, x);
|
||||
return binarySearch(arr, mid+1, r, x);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int binarySearch(int a[], int l, int r, int x) {
|
||||
if (r >= l){
|
||||
int mid = (l + (r - l))/2;
|
||||
if (a[mid] == x)
|
||||
return mid;
|
||||
if (arr[mid] > x)
|
||||
return binarySearch(arr, l, mid-1, x);
|
||||
return binarySearch(arr, mid+1, r, x);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
### Python implementation
|
||||
@ -181,14 +181,14 @@ int binarySearch(int a[], int l, int r, int x) {
|
||||
```Python
|
||||
def binary_search(arr, l, r, target):
|
||||
if r >= l:
|
||||
mid = l + (r - l)/2
|
||||
mid = (l + (r - l))/2
|
||||
if arr[mid] == target:
|
||||
return mid
|
||||
elif arr[mid] > target:
|
||||
return binary_search(arr, l, mid-1, target)
|
||||
else:
|
||||
else:
|
||||
return binary_search(arr, mid+1, r, target)
|
||||
else:
|
||||
else:
|
||||
return -1
|
||||
```
|
||||
|
||||
@ -197,40 +197,41 @@ def binary_search(arr, l, r, target):
|
||||
|
||||
Recursive approach!
|
||||
|
||||
```C++ - Recursive approach
|
||||
int binarySearch(int arr[], int start, int end, int x)
|
||||
{
|
||||
if (end >= start)
|
||||
{
|
||||
int mid = start + (end - start)/2;
|
||||
if (arr[mid] == x)
|
||||
return mid;
|
||||
```C++ -
|
||||
// Recursive approach in C++
|
||||
int binarySearch(int arr[], int start, int end, int x)
|
||||
{
|
||||
if (end >= start)
|
||||
{
|
||||
int mid = (start + (end - start))/2;
|
||||
if (arr[mid] == x)
|
||||
return mid;
|
||||
|
||||
if (arr[mid] > x)
|
||||
return binarySearch(arr, start, mid-1, x);
|
||||
|
||||
return binarySearch(arr, mid+1, end, x);
|
||||
}
|
||||
return -1;
|
||||
if (arr[mid] > x)
|
||||
return binarySearch(arr, start, mid-1, x);
|
||||
|
||||
return binarySearch(arr, mid+1, end, x);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
Iterative approach!
|
||||
|
||||
```C++
|
||||
int binarySearch(int arr[], int start, int end, int x)
|
||||
{
|
||||
while (start <= end)
|
||||
{
|
||||
int mid = start + (end - start)/2;
|
||||
if (arr[mid] == x)
|
||||
return mid;
|
||||
if (arr[mid] < x)
|
||||
start = mid + 1;
|
||||
int binarySearch(int arr[], int start, int end, int x)
|
||||
{
|
||||
while (start <= end)
|
||||
{
|
||||
int mid = (start + (end - start))/2;
|
||||
if (arr[mid] == x)
|
||||
return mid;
|
||||
if (arr[mid] < x)
|
||||
start = mid + 1;
|
||||
else
|
||||
end = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
end = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
@ -252,8 +253,8 @@ func binarySearch(for number: Int, in numbers: [Int]) -> Int? {
|
||||
return nil // the given number was not found
|
||||
}
|
||||
```
|
||||
### Example in Java
|
||||
```Java
|
||||
### Example in Java
|
||||
```Java
|
||||
// Iterative Approach in Java
|
||||
int binarySearch(int[] arr, int start, int end, int element)
|
||||
{
|
||||
@ -270,18 +271,22 @@ int binarySearch(int[] arr, int start, int end, int element)
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
```Java
|
||||
```Java
|
||||
// Recursive Approach in Java
|
||||
int binarySearch(int[] arr, int start,int end , int element)
|
||||
{
|
||||
int mid = ( start + end ) / 2;
|
||||
if(arr[mid] == element)
|
||||
return mid;
|
||||
if(arr[mid] < element)
|
||||
return binarySearch( arr , mid + 1 , end , element );
|
||||
else
|
||||
return binarySearch( arr, start, mid - 1 , element);
|
||||
}
|
||||
if(start <= end)
|
||||
{
|
||||
int mid = ( start + end ) / 2;
|
||||
if(arr[mid] == element)
|
||||
return mid;
|
||||
if(arr[mid] < element)
|
||||
return binarySearch( arr , mid + 1 , end , element );
|
||||
else
|
||||
return binarySearch( arr, start, mid - 1 , element);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user