searching for a number in a list

5 visualizaciones (últimos 30 días)
MG
MG el 2 de Nov. de 2015
Comentada: Walter Roberson el 2 de Nov. de 2015
Hi, I made a code below which I need to use to find a value in a list L. Supposing I have the following list L = [1 3 5 9 2 4 0 8]. If I call searchForIt(L, 2) I should get 5. But this is not what happens in matlab. I can't seem to figure out why that is.
function [Ind] = searchForIt(L, num)
% Ind = index of first occurence (if any) of num in L
% L is a list of numbers that is unsorted
% num is the number I am looking for in the list
Ind = [];
left_Ind = 1;
right_Ind = length(L);
while right_Ind > left_Ind;
mid_Ind = floor((left_Ind + right_Ind)/2);
if L(mid_Ind) == num;
elseif L(mid_Ind) < num;
left_Ind = mid_Ind + 1;
else right_Ind = mid_Ind - 1;
end
end
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Nov. de 2015
That is code for a binary search, which requires that the list L be sorted already. As your list is not sorted, you will not get the answers you expect.
  13 comentarios
MG
MG el 2 de Nov. de 2015
Editada: MG el 2 de Nov. de 2015
Do you mean change while right_Ind > left_Ind to while right_Ind >= left_Ind? Sorry if this is a stupid question, there is nothing about that in the description of the algorithm in my assignment.
The weird thing is, my code is correct, because when I call searchForIt(L,2), and L = [1 3 5 9 2 4 0 8], matlab sorts L and it becomes B, then it finds the entry '2' in the sorted list and spits out '3', or the position of '2' in the sorted list. However, my prof wants me to find the position of '2' in the original, scrambled list. So how do I do that? I'm confused. What was the purpose of sorting the list if I have to scramble it again.
Walter Roberson
Walter Roberson el 2 de Nov. de 2015
You can still use "while right_Ind > left_Ind" but if you do then afterwards you need to check in case the two are equal and that the number is found there. But it is easier to just use >= instead of > to cover the situation.
I have no idea what your assignment said. The usual way to handle the situation is to not use a binary search on an unsorted matrix. You might wonder what is wrong with doing the sort followed by a binary search, and the answer to that is that sorting is slower than a linear search for a single target value, as that involves at most length(L) comparisons but the best sort algorithms in the world are never less than length(L)*log(length(L)) for some arrangements of L.
If you are going to use sort() then read the documentation for it more carefully and see what other information you can ask it to return.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 2 de Nov. de 2015
Try it this way:
index = find(L == 2, 1, 'first');

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by