Practice of linear search method

Summary of what we did

I wrote the algorithm of the Basic Information Technology Engineer Examination as a practice in my own way.

Common parts for use with other algorithms

A class made so that it can be used for practice such as the binary search method.

A class that prints the search results.

MessagePrinter.java


/**
 *A class that prints a message of search results
 * 
 * @author hatena
 *
 */
import java.util.List;

public class MessagePrinter {

    public void printFoundMessages(int targetNumber, List<Integer> targetList) {
        System.out.println("Number of objects to search in the list" + targetNumber + "Existed.");
        System.out.println("The search target list is" + targetList + "was.");
        System.out.println("The index of the number of search targets is" + targetList.indexOf(targetNumber) + "is.");
    }

    public void printNotFoundMessages(int targetNumber, List<Integer> targetList) {
        System.out.println("Number of objects to search in the list" + targetNumber + "Did not exist.");
        System.out.println("The list to be searched is" + targetList + "was.");
    }

}

A class that creates a list of random integers.

The java.util.Random class had an ints () method that returned a specified range of IntStream. I feel that the random numbers are biased. .. ..

RandomListCreator.java


import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
/**
 *A class that creates a list of random positive integers.
 *
 * @author hatena
 *
 */
@AllArgsConstructor
public class RandomListCreator {

    public int lengthOfList;
    public int maximumNumber;//ex:If you specify 50, the list generated will include 50.

    /**
     *A method that returns an unsorted list of integers of a specified length.
     *
     *
     */
    public List<Integer> createRandUnorderdList() {

        //Random number generation class
        Random rand = new Random();

      //@formatter:off
        List<Integer> unorderdRandlist =
                rand
                .ints(this.lengthOfList, 0, this.maximumNumber + 1)
                .boxed()
                .collect(Collectors.toList());
      //@formatter:on

        return unorderdRandlist;
    }

}

Linear search method main part

Simply look for matching numbers from the beginning.

Class with linear search function

SeqSearch.java


import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 *A class that performs a linear search method (because it is an extended for statement, there is no sentinel turn.)
 *
 * @author hatena
 */
@Data
@AllArgsConstructor
public class SeqSearch {

    private List<Integer> targetList;

    /**
     *Method of the linear search method body. Search in order from the beginning.
     *
     * @return Matched index. If they don't match-1 is returned.
     *
     */
    public int findTargetNum(int targetNumber) {

        //Check if they match in order from the top of the list.
        for (int num : this.targetList) {

            //If the target number is included in the list, return the index number.
            if (num == targetNumber) {
                return this.targetList.indexOf(targetNumber);
            }
        }
        //If they do not match to the end.
        return -1;
    }
}

The main class that performs the linear search method

MainSeq.java


import java.util.List;
import java.util.Scanner;
import common.MessagePrinter;
import common.RandomListCreator;
import seqsearch.SeqSearch;

/**
 *Binary search method execution main class
 *
 * @author hatena
 *
 */
public class MainSeq {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        //Have the user enter the information (length, maximum number in the list) needed to create a random list
        System.out.print("Enter the length of the list as a positive integer:");
        int lengthOfList = scanner.nextInt();
        System.out.print("Enter the maximum number to include in the list:");
        int maximumNumber = scanner.nextInt();


        //Create a random list
        RandomListCreator listCreator = new RandomListCreator(lengthOfList, maximumNumber);
        List<Integer> targetList = listCreator.createRandUnorderdList();
        System.out.println("I created a list of random numbers.");

        //Enter the number you want to search as standard
        System.out.print("Enter the number you want to linearly search:");
        int targetNumber = scanner.nextInt();
        System.out.println("In this list" + targetNumber + "Linear search for any.");

        //Pass the list and the search target to the class of the linear search body.
        SeqSearch seqSearch = new SeqSearch(targetList);

        //Class instantiation for displaying result messages
        MessagePrinter messagePrinter = new MessagePrinter();

        //If it matches the list, an integer greater than or equal to 0 is returned and the result is printed.
        if (seqSearch.findTargetNum(targetNumber) >= 0) {
            messagePrinter.printFoundMessages(targetNumber, seqSearch.getTargetList());
        } else {
            messagePrinter.printNotFoundMessages(targetNumber, seqSearch.getTargetList());
        }

        scanner.close();

    }

}

Result example

■ When the target number exists in the list

Enter the length of the list as a positive integer: 10
Enter the maximum number to include in the list: 15
I created a list of random numbers.
Enter the number you want to linearly search:9
Linear search for 9s in this list.
There were 9 search targets in the list.
The search target list is[0, 14, 14, 9, 6, 0, 4, 7, 15, 7]was.
The index of the number of objects to be searched is 3.

■ When the target number does not exist in the list

Enter the length of the list as a positive integer: 10
Enter the maximum number to include in the list: 15
I created a list of random numbers.
Enter the number you want to perform a linear search.:9
Linear search for 9s in this list.
The number 9 to be searched was not found in the list.
The list to be searched is[11, 7, 10, 10, 14, 4, 11, 3, 11, 1]was.

Recommended Posts

Practice of linear search method
Practice of binary search method
Linear search
Binary search binary search method
[Practice] Map method
Binary search method
Implementation of search function
definition of ruby method
[Java] Practice of exception handling [Exception]
Implementation of sequential search function
[Practice! 】 Execution of SQL statement
Combination of search and each_with_index
Main functions of scope method
Benefits of Java static method
ArchUnit Practice: Architecture Testing of Onion Architecture
Consideration of Routing of form_with helper method
About the role of the initialize method
[Ruby] Search problem using index method
What kind of method is define_method?
[Ruby] Let's examine the inheritance tree while watching the flow of method search