Binary Search in Java

Reading time ~1 minute

Binary search is extremely easy to explain, but very tricky to write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int binary_search(List<Integer> list, int value) {
		int lo = 0;
		int hi = list.size() - 1;
		while (lo <= hi){
			//because mid = (hi + lo)/2 might overflow memory
			int mid = lo + (hi - lo) / 2;
			if (value == list.get(mid)) 
				return mid;			
			else if (value > list.get(mid)) 
				lo = mid + 1; //otherwise will stuck on non existing values
			else 
				hi = mid - 1;
		}
		return -1;
	}

How to Download Multiple Instagram Photos at Once?

You want to print your Insta photos or just keep them save and archived? Out of luck - there is no obvious way to download them, unless y...… Continue reading

Polymer and Vulcanization

Published on August 08, 2016

Jeff Dean of Google

Published on August 06, 2016