Skip to content

Simplifies the readability of the static method BS #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 21 additions & 28 deletions Searches/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@ class BinarySearch
* @return the location of the key
**/
public static int BS(int array[], int key, int lb, int ub)
{ if (lb>ub)
{
{
if ( lb > ub)
return -1;
}

int mid = (lb + ub) / 2;

int mid=(lb+ub)/2;

if (key<array[mid])
{
if (key < array[mid])
return (BS(array, key, lb, mid-1));
}
else if (key>array[mid])
{
return (BS(array, key, mid+1, ub));
}
else
{
return mid;
}

if (key > array[mid])
return (BS(array, key, mid + 1, ub));

return mid;
}


Expand All @@ -54,22 +48,21 @@ public static void main(String[] args)

//Input
System.out.println("Enter an array of 10 numbers : ");
for (int i=0; i<10 ;i++ )
{
array[i]=input.nextInt();
}

for (int i = 0; i < 10 ; i++ )
array[i] = input.nextInt();

System.out.println("Enter the number to be searched : ");
key=input.nextInt();

key = input.nextInt();

int index=BS(array, key, 0, 9);
if (index!=-1)
{
System.out.println("Number found at index number : " + index);
}
else
{

if (index != -1)
System.out.println("Number found at index number : " + index);
else
System.out.println("Not found");
}

input.close();
}
}