Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised January 9, 2026)

Previous: 12. Algorithms

Coding Exercises: Algorithms

Implement a linear search function. Note: You have written similar functions before, finding an object with a specified name in a list, for example. In this case you want to return the index into the array, not the object.

Example:

int find(int[] values, int searchValue);

find({1, 3, 5, 7}, 0) -> -1  // return -1 if not found
find({1, 3, 5, 7}, 1) -> 0
find({1, 3, 5, 7}, 3) -> 1
find({1, 3, 5, 7}, 5) -> 2
find({1, 3, 5, 7}, 7) -> 3

Sorting

Implement a binary search function. Remember: the input list must be sorted in order for this to work.


Next: