Skip to content

Commit

Permalink
Added Binary Search implementations in C, C++, Java and Python (Issue…
Browse files Browse the repository at this point in the history
… 42) (Asiatik#43)
  • Loading branch information
GnikDroy authored and tstreamDOTh committed Sep 28, 2018
1 parent 331bf6e commit 49b7377
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Searching/Binary_Search/C++/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>

/*
* Binary search algorithm
* This is the exact implementation of the pseudocode written in README.
* Notice that even if the array list contains multiple items,
* it will return only one index.
* Thus this algorithm is well suited to find if a item is present.
* And not for finding the exact index if multiple items are present.
*
* With C++ you have to be careful of integer overflows.
* So we must make sure that the length of itemList is below that of int.
* Also the size of the array must also be sent.
*/

//Notice that itemList is passed as a pointer here. No expensive copying takes place.
int binarySearch(int itemList[],int itemListSize,int item)
{
int leftIndex = 0;
int rightIndex = itemListSize;
int middleIndex;
while (leftIndex <= rightIndex)
{
middleIndex = (leftIndex+rightIndex)/2;
if (itemList[middleIndex] < item)
{
leftIndex = middleIndex + 1;
}
else if(itemList[middleIndex] > item)
{
rightIndex = middleIndex - 1;
}
else{
return middleIndex;
}
}
return -1;

}

//Simple demonstration for the algorithm
int main(int argc,char* argv[])
{
int arr[] = {1,2,3,4,5};
std::cout << binarySearch(arr,sizeof(arr)/sizeof(arr[0]),3) << std::endl;
std::cout << binarySearch(arr,sizeof(arr)/sizeof(arr[0]),0);

}
48 changes: 48 additions & 0 deletions Searching/Binary_Search/C/BinarySearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "stdio.h"

/*
* Binary search algorithm
* This is the exact implementation of the pseudocode written in README.
* Notice that even if the array list contains multiple items,
* it will return only one index.
* Thus this algorithm is well suited to find if a item is present.
* And not for finding the exact index if multiple items are present.
*
* With C you have to be careful of integer overflows.
* So we must make sure that the length of itemList is below that of int.
* Also the size of the array must also be sent.
*/

//Notice that itemList is passed as a pointer. No expensive copying takes place.
int binarySearch(int itemList[],int itemListSize,int item)
{
int leftIndex = 0;
int rightIndex = itemListSize;
int middleIndex;
while (leftIndex <= rightIndex)
{
middleIndex = (leftIndex+rightIndex)/2;
if (itemList[middleIndex] < item)
{
leftIndex = middleIndex + 1;
}
else if(itemList[middleIndex] > item)
{
rightIndex = middleIndex - 1;
}
else{
return middleIndex;
}
}
return -1;

}

//Simple demonstration for the algorithm
int main(int argc,char* argv[])
{
int arr[] = {1,2,3,4,5};
printf("%d",binarySearch(arr,sizeof(arr)/sizeof(arr[0]),3));
printf("%d",binarySearch(arr,sizeof(arr)/sizeof(arr[0]),0));

}
44 changes: 44 additions & 0 deletions Searching/Binary_Search/Java/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class BinarySearch{

/**
* Binary search algorithm
* This is the exact implementation of the pseudocode written in README.
* Notice that even if the array list contains multiple items,
* it will return only one index.
* Thus this algorithm is well suited to find if a item is present.
* And not for finding the exact index if multiple items are present.
*
* With java you have to be careful of integer overflows.
* So we must make sure that the length of itemList is below that of int.
*
* @param itemList List of all the sorted items
* @param item The item to search for
* @return Index of the item
*/
public static int binarySearch(int itemList[],int item){
int leftIndex=0;
int rightIndex=itemList.length-1;
int middleIndex;

while(leftIndex<=rightIndex){
middleIndex=(leftIndex+rightIndex)/2;

if(itemList[middleIndex]<item){
leftIndex=middleIndex+1;
}
else if(itemList[middleIndex]>item){
rightIndex=middleIndex-1;
}
else{
return middleIndex;
}
}
return -1;
}

//Simple desmonstration of the function
public static void main(String args[]){
System.out.println(binarySearch(new int[]{1,2,3,4,5},6));
System.out.println(binarySearch(new int[]{1,2,3,4,5},3));
}
}
23 changes: 23 additions & 0 deletions Searching/Binary_Search/Python/BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
This is a simple binary search algorithm for python.
This is the exact implementation of the pseudocode written in README.
"""

def binary_search(item_list,item):
left_index = 0
right_index = len(item_list)-1

while left_index <= right_index:
middle_index = (left_index+right_index) / 2
if item_list[middle_index] < item:
left_index=middle_index+1
elif item_list[middle_index] > item:
right_index=middle_index-1
else:
return middle_index
return None

#Simple demonstration of the function
if __name__=='__main__':
print (binary_search([1,3,5,7],3))
print (binary_search([1,2,5,7,97],0))
33 changes: 33 additions & 0 deletions Searching/Binary_Search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Binary Search

>Worst Case Time Complexity: O(log n)
>Space Complexity: (O(1))
[Wikipedia Entry](https://en.wikipedia.org/wiki/Binary_search_algorithm)

## Pseudocode

A is the array, n is the size of array and T is the item


function binary_search(A, n, T):
L := 0
R := n − 1

while L <= R:
m := floor((L + R) / 2)
if A[m] < T:
L := m + 1
else if A[m] > T:
R := m - 1
else:
return m
return unsuccessful

Notice that even if the array list contains multiple items, it will return only one index.
Thus this algorithm is well suited to find if a item is present.
And not for finding the exact index if multiple items are present.

Binary search can also be obtain the indexes of the items present.
And unlike the version here which works on numbers, binary search can work on any sorted array. For example binary search can be used in lexiographically arranged strings.

0 comments on commit 49b7377

Please sign in to comment.