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
|
* Item is found
|
||||||
|
|
||||||
The Power of Binary Search in Data Systems (B+ trees):
|
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:
|
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.
|
if (low > high) { // No more elements in the array.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the middle of the array.
|
// Find the middle of the array.
|
||||||
var mid = Math.ceil((low + high) / 2);
|
var mid = Math.ceil((low + high) / 2);
|
||||||
|
|
||||||
if (arr[mid] === item) { // Found the item!
|
if (arr[mid] === item) { // Found the item!
|
||||||
return mid;
|
return mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item < arr[mid]) { // Item is in the half from low to mid-1.
|
if (item < arr[mid]) { // Item is in the half from low to mid-1.
|
||||||
return binarySearch(arr, item, low, mid-1);
|
return binarySearch(arr, item, low, mid-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
else { // Item is in the half from mid+1 to high.
|
else { // Item is in the half from mid+1 to high.
|
||||||
return binarySearch(arr, item, mid+1, high);
|
return binarySearch(arr, item, mid+1, high);
|
||||||
}
|
}
|
||||||
@ -126,9 +126,9 @@ function binary_search(a, v) {
|
|||||||
return a[low] === v;
|
return a[low] === v;
|
||||||
} else {
|
} else {
|
||||||
var mid = math_floor((low + high) / 2);
|
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(low, mid - 1)
|
||||||
: search(mid + 1, high);
|
: search(mid + 1, high);
|
||||||
}
|
}
|
||||||
@ -163,17 +163,17 @@ end
|
|||||||
### Example in C
|
### Example in C
|
||||||
|
|
||||||
```C
|
```C
|
||||||
int binarySearch(int a[], int l, int r, int x) {
|
int binarySearch(int a[], int l, int r, int x) {
|
||||||
if (r >= l){
|
if (r >= l){
|
||||||
int mid = l + (r - l)/2;
|
int mid = (l + (r - l))/2;
|
||||||
if (a[mid] == x)
|
if (a[mid] == x)
|
||||||
return mid;
|
return mid;
|
||||||
if (arr[mid] > x)
|
if (arr[mid] > x)
|
||||||
return binarySearch(arr, l, mid-1, x);
|
return binarySearch(arr, l, mid-1, x);
|
||||||
return binarySearch(arr, mid+1, r, x);
|
return binarySearch(arr, mid+1, r, x);
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Python implementation
|
### Python implementation
|
||||||
@ -181,14 +181,14 @@ int binarySearch(int a[], int l, int r, int x) {
|
|||||||
```Python
|
```Python
|
||||||
def binary_search(arr, l, r, target):
|
def binary_search(arr, l, r, target):
|
||||||
if r >= l:
|
if r >= l:
|
||||||
mid = l + (r - l)/2
|
mid = (l + (r - l))/2
|
||||||
if arr[mid] == target:
|
if arr[mid] == target:
|
||||||
return mid
|
return mid
|
||||||
elif arr[mid] > target:
|
elif arr[mid] > target:
|
||||||
return binary_search(arr, l, mid-1, target)
|
return binary_search(arr, l, mid-1, target)
|
||||||
else:
|
else:
|
||||||
return binary_search(arr, mid+1, r, target)
|
return binary_search(arr, mid+1, r, target)
|
||||||
else:
|
else:
|
||||||
return -1
|
return -1
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -197,40 +197,41 @@ def binary_search(arr, l, r, target):
|
|||||||
|
|
||||||
Recursive approach!
|
Recursive approach!
|
||||||
|
|
||||||
```C++ - Recursive approach
|
```C++ -
|
||||||
int binarySearch(int arr[], int start, int end, int x)
|
// Recursive approach in C++
|
||||||
{
|
int binarySearch(int arr[], int start, int end, int x)
|
||||||
if (end >= start)
|
{
|
||||||
{
|
if (end >= start)
|
||||||
int mid = start + (end - start)/2;
|
{
|
||||||
if (arr[mid] == x)
|
int mid = (start + (end - start))/2;
|
||||||
return mid;
|
if (arr[mid] == x)
|
||||||
|
return mid;
|
||||||
|
|
||||||
if (arr[mid] > x)
|
if (arr[mid] > x)
|
||||||
return binarySearch(arr, start, mid-1, x);
|
return binarySearch(arr, start, mid-1, x);
|
||||||
|
|
||||||
return binarySearch(arr, mid+1, end, x);
|
return binarySearch(arr, mid+1, end, x);
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Iterative approach!
|
Iterative approach!
|
||||||
|
|
||||||
```C++
|
```C++
|
||||||
int binarySearch(int arr[], int start, int end, int x)
|
int binarySearch(int arr[], int start, int end, int x)
|
||||||
{
|
{
|
||||||
while (start <= end)
|
while (start <= end)
|
||||||
{
|
{
|
||||||
int mid = start + (end - start)/2;
|
int mid = (start + (end - start))/2;
|
||||||
if (arr[mid] == x)
|
if (arr[mid] == x)
|
||||||
return mid;
|
return mid;
|
||||||
if (arr[mid] < x)
|
if (arr[mid] < x)
|
||||||
start = mid + 1;
|
start = mid + 1;
|
||||||
else
|
else
|
||||||
end = mid - 1;
|
end = mid - 1;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -252,8 +253,8 @@ func binarySearch(for number: Int, in numbers: [Int]) -> Int? {
|
|||||||
return nil // the given number was not found
|
return nil // the given number was not found
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### Example in Java
|
### Example in Java
|
||||||
```Java
|
```Java
|
||||||
// Iterative Approach in Java
|
// Iterative Approach in Java
|
||||||
int binarySearch(int[] arr, int start, int end, int element)
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
```Java
|
```Java
|
||||||
// Recursive Approach in Java
|
// Recursive Approach in Java
|
||||||
int binarySearch(int[] arr, int start,int end , int element)
|
int binarySearch(int[] arr, int start,int end , int element)
|
||||||
{
|
{
|
||||||
int mid = ( start + end ) / 2;
|
if(start <= end)
|
||||||
if(arr[mid] == element)
|
{
|
||||||
return mid;
|
int mid = ( start + end ) / 2;
|
||||||
if(arr[mid] < element)
|
if(arr[mid] == element)
|
||||||
return binarySearch( arr , mid + 1 , end , element );
|
return mid;
|
||||||
else
|
if(arr[mid] < element)
|
||||||
return binarySearch( arr, start, mid - 1 , element);
|
return binarySearch( arr , mid + 1 , end , element );
|
||||||
}
|
else
|
||||||
|
return binarySearch( arr, start, mid - 1 , element);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -118,12 +118,12 @@ void copy_string(char [] first_string, char [] second_string)
|
|||||||
```C
|
```C
|
||||||
strcat(first, second);
|
strcat(first, second);
|
||||||
```
|
```
|
||||||
Here is an example of manual implementation of fuction strcat:
|
Here is an example of manual implementation of function strcat:
|
||||||
|
|
||||||
void string_concatenate(char [] s1, char [] s2)
|
void string_concatenate(char [] s1, char [] s2)
|
||||||
{
|
{
|
||||||
int i = strlen(s1), j;
|
int i = strlen(s1), j;
|
||||||
for(int j = 0; s2[j]; j++, i += 1)
|
for(j = 0; s2[j]; j++, i += 1)
|
||||||
{
|
{
|
||||||
s1[i] = s2[j];
|
s1[i] = s2[j];
|
||||||
}
|
}
|
||||||
@ -140,7 +140,7 @@ int string_length(char [] string)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(int i = 0; string[i]; i++);
|
for(i = 0; string[i]; i++);
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ point img_dim = { .y = 480, .x = 640 };
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Unions vs Structures
|
## Unions vs Structures
|
||||||
Unions are declared in the same was as structs, but are different because only one item within the union can be used at any time.
|
Unions are declared in the same way as structs, but are different because only one item within the union can be used at any time.
|
||||||
|
|
||||||
```C
|
```C
|
||||||
typedef union{
|
typedef union{
|
||||||
@ -83,6 +83,6 @@ typedef union{
|
|||||||
```
|
```
|
||||||
|
|
||||||
## A few more tricks
|
## A few more tricks
|
||||||
* When you create a pointer to a structure using the `&` operator you can use the special `->` infix operator to deference it. This is very used for example when working with linked lists in C
|
* When you create a pointer to a structure using the `&` operator you can use the special `->` infix operator to deference it. This is used for example when working with linked lists in C
|
||||||
* The new defined type can be used just as other basic types for almost everything. Try for example to create an array of type `student` and see how it works.
|
* The new defined type can be used just as other basic types for almost everything. Try for example to create an array of type `student` and see how it works.
|
||||||
* Structs can be copied or assigned but you can not compare them!
|
* Structs can be copied or assigned but you can not compare them!
|
||||||
|
Reference in New Issue
Block a user