4.14 Searching Algorithms

8 阅读2分钟

1. Exam Points

  • Implement linear search of arrays or lists using selection and loop statements.

2. Knowledge Points

(1) Implement Searching Algorithms

- Linear search algorithms(线性检索算法) are standard algorithms that check each element in order until the desired value is found or all elements in the array or ArrayList have been checked. (found or not found)

  • Linear search algorithms can begin the search process from either end of the array or ArrayList. (from first to last, or last to first)
  • When applying linear search algorithms to 2D arrays, each row must be accessed, then linear search applied to each row of the 2D array.

(1) Examples

  • Example 1: search in an 1D array

    public static boolean findTarget(String[] data, String target) {
    
        for (int i = 0; i < data.length; i++) {
            
            if(data[i].equals(target)) { // if a value equals the target is found
                return true;
            }
        }
    
        // if the for loop is over, and return true is not executed,
        // it means the target is not found, then we return false
            return false;
    }
    
    public static void main(String[] args) {
    
            String[] words = { "dog", "pig", "cat", "snake" };
    
            System.out.println(findTarget(words, "snake"));
            System.out.println(findTarget(words, "crocodile"));
    }
    
  • Example 2: search in a 2D array

    public static boolean findTarget(String[][] data, String target) {
    
        // i: row index, j: column index
        for (int i = 0; i < data.length; i++) { // traverse each row
    
            for (int j = 0; j < data[0].length; j++) { // traverse each column
                if(data[i][j].equals(target)) {
                return true;
            }
        }
    }
    
        // return false if the target is not found in the array
        return false;
    }
    
    public static void main(String[] args) {
    
        String[][] words = { 
                { "dog", "pig", "cat", "snake" }, 
                { "apple", "orange", "pear", "banana" },
                { "black", "white", "pink", "red" } 
        };
    
        System.out.println(findTarget(words, "white"));
        System.out.println(findTarget(words, "blue"));
    }
    
  • Example 3: search in an ArrayList

    public static boolean findTarget(ArrayList<Integer> list, int target) {
    
        for (int i = 0; i < list.size(); i++) {
                int x = list.get(i);
                if (x == target) {
                    return true;
                }
        }
    	
        return false;
    }
    
    public static void main(String[] args) {
    
        ArrayList<Integer> numList = new ArrayList<Integer>();
        numList.add(11);
        numList.add(21);
        numList.add(33);
        numList.add(56);
        System.out.println(findTarget(numList, 33));
        System.out.println(findTarget(numList, 88));
    }
    
  • Example 4: search a target Dog object

    class Dog {
    
        private String name;
        private int age;
    
        public Dog(String n, int a) {
            name = n;
            age = a;
        }
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    public class MainClass {
    
        // Search a target Dog with a given name
        public static boolean search(ArrayList<Dog> data, String name) {
    
            for (Dog d : data) { 
                if (name.equals(d.getName())) { // if found, return true
                    return true;
                }
            }
    
            // if not found after the loop is over, return false
            return false;
    
        }
    
        public static void main(String[] args) {
    
            ArrayList<Dog> list = new ArrayList<Dog>();
    
            list.add(new Dog("lele", 3));
            list.add(new Dog("haha", 1));
            list.add(new Dog("hehe", 4));
            list.add(new Dog("doudou", 5));
            list.add(new Dog("keke", 6));
            list.add(new Dog("koko", 8));
    
            // call the method to search for a dog named koko : returns true
            System.out.println(search(list, "koko"));
            
            // call the method to search for a dog named maomao : returns false
            System.out.println(search(list, "maomao"));
    
        }
    }
    

3. Exercises