diff --git a/guide/english/algorithms/search-algorithms/binary-search/index.md b/guide/english/algorithms/search-algorithms/binary-search/index.md index 7a46ba94cc..0cd937cbf4 100644 --- a/guide/english/algorithms/search-algorithms/binary-search/index.md +++ b/guide/english/algorithms/search-algorithms/binary-search/index.md @@ -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; + } ``` diff --git a/guide/english/c/arrays-and-strings/index.md b/guide/english/c/arrays-and-strings/index.md index fbfb3d4541..24fee812c2 100644 --- a/guide/english/c/arrays-and-strings/index.md +++ b/guide/english/c/arrays-and-strings/index.md @@ -118,12 +118,12 @@ void copy_string(char [] first_string, char [] second_string) ```C 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) { 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]; } @@ -140,7 +140,7 @@ int string_length(char [] string) { int i; - for(int i = 0; string[i]; i++); + for(i = 0; string[i]; i++); return i; } diff --git a/guide/english/c/structured-data-types/index.md b/guide/english/c/structured-data-types/index.md index 9efee06253..3d096befba 100644 --- a/guide/english/c/structured-data-types/index.md +++ b/guide/english/c/structured-data-types/index.md @@ -53,7 +53,7 @@ point img_dim = { .y = 480, .x = 640 }; ``` ## 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 typedef union{ @@ -83,6 +83,6 @@ typedef union{ ``` ## 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. * Structs can be copied or assigned but you can not compare them!