diff --git a/python/code_challenges/README.md b/python/code_challenges/README.md index 216a03a..5ce6798 100644 --- a/python/code_challenges/README.md +++ b/python/code_challenges/README.md @@ -2,10 +2,8 @@ ## Table of Content - | Name | Challenge| | --- | ----------- | -| Reverse Array | [Ch01](https://eslamakram.github.io/data-structures-and-algorithms/python/code_challenges/array-reverse/README) | -| Insert Shift Array | [Ch02](https://eslamakram.github.io/data-structures-and-algorithms/python/code_challenges/array-insert-shift/README) | -| Binary Search Array | [Ch03](https://eslamakram.github.io/data-structures-and-algorithms/python/code_challenges/array-reverse/README) | - +| Reverse Array | [Ch01](https://github.com/eslamakram/data-structures-and-algorithms/tree/main/python/code_challenges/array-reverse) | +| Insert Shift Array | [Ch02](https://github.com/eslamakram/data-structures-and-algorithms/tree/main/python/code_challenges/array-insert-shift) | +| Binary Search Array | [Ch03](https://github.com/eslamakram/data-structures-and-algorithms/tree/main/python/code_challenges/array-reverse/README.md) | diff --git a/python/code_challenges/array-binary-search/README.md b/python/code_challenges/array-binary-search/README.md index 1142b37..0c2fe3a 100644 --- a/python/code_challenges/array-binary-search/README.md +++ b/python/code_challenges/array-binary-search/README.md @@ -1,15 +1,18 @@ # Code Challange 03 -## Insert to Middle of an Array +## Binary Search of Sorted Array -There is an array and value that want to insert it in the middle index in given array and then shift other elements. +Write a function called BinarySearch which takes in 2 parameters: a sorted array and the search key and returns the index of the array’s element that is equal to the value of the search key, or -1 if the element is not in the array. + +input: a sorted array and the search key +output: intger number that indicates the index of sreach key , or -1 if that isnt in the array ## Whiteboard Process -![codeCh2](codeChallange2.drawio.png) +![codeCh3](ch2.png) ## Approach & Efficiency -iterations and assign new array +used search algorithm diff --git a/python/code_challenges/array-binary-search/array-binary-search.py b/python/code_challenges/array-binary-search/array-binary-search.py index e69de29..43f189d 100644 --- a/python/code_challenges/array-binary-search/array-binary-search.py +++ b/python/code_challenges/array-binary-search/array-binary-search.py @@ -0,0 +1,16 @@ + +def binary_search(array1, searchKey): + start = 0 + end = len(array1) - 1 + while(start <= end): + mid = (start + end) // 2 + if(array1[mid] > searchKey): + end = mid - 1 + elif(array1[mid] < searchKey): + start = mid + 1 + else: + return mid + return -1 + +if __name__ == '__main__': + print(binary_search([11, 23, 36, 47, 51, 66, 73, 83, 92],23)) diff --git a/python/code_challenges/array-binary-search/ch2.png b/python/code_challenges/array-binary-search/ch2.png new file mode 100644 index 0000000..a90ea16 Binary files /dev/null and b/python/code_challenges/array-binary-search/ch2.png differ