Skip to content

Commit

Permalink
Update 1.BinarySearch.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrezzak authored Oct 20, 2017
1 parent c25065c commit bf07845
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions 1.BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -15,28 +14,20 @@
#include <algorithm>
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;
Expand All @@ -55,5 +46,4 @@ int main()
cin >> n;

cout << "The number is in: " << bs(arr, 0, k-1, n) << endl;

}

0 comments on commit bf07845

Please sign in to comment.