dimanche 20 avril 2014

Java - Can une recherche linéaire permettant de trouver le plus petit nombre dans un tableau (circulaire) cyclique - Stack Overflow


I am writing a program that uses a circular array to hold the int values and was just wondering if it was possible to use a linear search to find the lowest number in the circular array. I have used linear search before on 1d and 2d arrays but this is the first time i have used a circular array.


Thanks !!




Searching a cyclical array should be the same as a 1d array. The only difference is your starting and ending points.


For a 1d array, your search is probably something like this:


for (int i=0; i<array.length; i++) ...

For a cyclical array, the search should stop when you return to your starting element.




All you have to do is cycle through all the elements and remember the smallest value you found (and maybe its index). So, yes, it is possible!



I am writing a program that uses a circular array to hold the int values and was just wondering if it was possible to use a linear search to find the lowest number in the circular array. I have used linear search before on 1d and 2d arrays but this is the first time i have used a circular array.


Thanks !!



Searching a cyclical array should be the same as a 1d array. The only difference is your starting and ending points.


For a 1d array, your search is probably something like this:


for (int i=0; i<array.length; i++) ...

For a cyclical array, the search should stop when you return to your starting element.



All you have to do is cycle through all the elements and remember the smallest value you found (and maybe its index). So, yes, it is possible!


0 commentaires:

Enregistrer un commentaire