Search for a setpoint index in a pre-sorted array

Today I went for an interview, and the interviewer asked me how I would find the index of a given value (number) in a pre-sorted array like this:

$preSortedArr=array(23,32,36,41,45,54);

He also said that recursion is not allowed.

I think the function should look like this:

function findIndexByValue($preSortedArray,$value){            
//some codes here       
}

What decision do you think he expected from me?

EDIT: Sorry, I forgot to add that he initially asked me to write pseudo-code, but I said I didn't know. Then I tried to write it in PHP, but I think it expects a language-independent solution.

+3
source share
5 answers

, , , , . ( , - , ), , . , , .

+5

,

. - , PHP. while, next, key current, . , , .

+3
$key = array_search("23", $array);
+2

, - . , , , , .

+1

- . - o (logn)

+1

Source: https://habr.com/ru/post/1737141/


All Articles