From bf07845074ede54147ea1e0afa165bd42f405820 Mon Sep 17 00:00:00 2001 From: abdurrezzak Date: Fri, 20 Oct 2017 21:32:26 +0300 Subject: [PATCH] Update 1.BinarySearch.cpp --- 1.BinarySearch.cpp | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/1.BinarySearch.cpp b/1.BinarySearch.cpp index 4c06f8c..186544f 100644 --- a/1.BinarySearch.cpp +++ b/1.BinarySearch.cpp @@ -1,11 +1,10 @@ /* * This program takes an array from the user * sorts it - * and searches the number user enters in the array using - * Binary Search Algorithm - * + * and searches the number user enters in the array using Binary Search Algorithm * The time complexity is O(nlogn) due to sorting. However, the sorting is just for demonstration of binary search. * Normally, time complexity of binary search is O(logn) + * For more information about Binary Search Algorithm: https://en.wikipedia.org/wiki/Binary_search_algorithm * * Coded by: Abdurrezak EFE * @@ -15,28 +14,20 @@ #include using namespace std; -int bs(int arr[], int l, int r, int n) +int bs(int arr[], int l, int r, int n) //binary search function { - if(r>=l) { int mid = (l+r)/2; - if(arr[mid] == n) return mid; - if(arr[mid] > n) return bs(arr, l, mid-1, n); - return bs(arr,mid+1,r,n); - } - return -1; - } - int main() { int k; @@ -55,5 +46,4 @@ int main() cin >> n; cout << "The number is in: " << bs(arr, 0, k-1, n) << endl; - }