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:
|
||||||
|
|
||||||
@ -165,7 +165,7 @@ end
|
|||||||
```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)
|
||||||
@ -181,7 +181,7 @@ 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:
|
||||||
@ -197,12 +197,13 @@ def binary_search(arr, l, r, target):
|
|||||||
|
|
||||||
Recursive approach!
|
Recursive approach!
|
||||||
|
|
||||||
```C++ - Recursive approach
|
```C++ -
|
||||||
|
// Recursive approach in C++
|
||||||
int binarySearch(int arr[], int start, int end, int x)
|
int binarySearch(int arr[], int start, int end, int x)
|
||||||
{
|
{
|
||||||
if (end >= start)
|
if (end >= start)
|
||||||
{
|
{
|
||||||
int mid = start + (end - start)/2;
|
int mid = (start + (end - start))/2;
|
||||||
if (arr[mid] == x)
|
if (arr[mid] == x)
|
||||||
return mid;
|
return mid;
|
||||||
|
|
||||||
@ -222,7 +223,7 @@ 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)
|
||||||
@ -274,6 +275,8 @@ int binarySearch(int[] arr, int start, int end, int element)
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
if(start <= end)
|
||||||
|
{
|
||||||
int mid = ( start + end ) / 2;
|
int mid = ( start + end ) / 2;
|
||||||
if(arr[mid] == element)
|
if(arr[mid] == element)
|
||||||
return mid;
|
return mid;
|
||||||
@ -281,7 +284,9 @@ int binarySearch(int[] arr, int start,int end , int element)
|
|||||||
return binarySearch( arr , mid + 1 , end , element );
|
return binarySearch( arr , mid + 1 , end , element );
|
||||||
else
|
else
|
||||||
return binarySearch( arr, start, mid - 1 , element);
|
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