new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
27
Task/Binary-search/Erlang/binary-search.erl
Normal file
27
Task/Binary-search/Erlang/binary-search.erl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
%% Task: Binary Search algorithm
|
||||
%% Author: Abhay Jain
|
||||
|
||||
-module(searching_algorithm).
|
||||
-export([start/0]).
|
||||
|
||||
start() ->
|
||||
List = [1,2,3],
|
||||
binary_search(List, 5, 1, length(List)).
|
||||
|
||||
|
||||
binary_search(List, Value, Low, High) ->
|
||||
if Low > High ->
|
||||
io:format("Number ~p not found~n", [Value]),
|
||||
not_found;
|
||||
true ->
|
||||
Mid = (Low + High) div 2,
|
||||
MidNum = lists:nth(Mid, List),
|
||||
if MidNum > Value ->
|
||||
binary_search(List, Value, Low, Mid-1);
|
||||
MidNum < Value ->
|
||||
binary_search(List, Value, Mid+1, High);
|
||||
true ->
|
||||
io:format("Number ~p found at index ~p", [Value, Mid]),
|
||||
Mid
|
||||
end
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue