BinarySearch::usage = "BinarySearch[list, elem] finds the position of elem
	in the sorted list. If elem does not occur in list, 0 is returned."

BinarySearch[list_, elem_] :=
	Module[{n0 = 1, n1 = Length[list], m},
		While[n0 <= n1,
			m = Floor[(n0 + n1)/2];
			If[ list[[m]] == elem, Return[m] ];        (* found *)
			If[ list[[m]] < elem, n0 = m+1, n1 = m-1 ] (* continue *)
		];
		0  (* not found *)
	]
