Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.9 kB
3
Indexable
Never
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package linearsearch;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class LinearSearch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        int n = InputNumberArray();
        int[] array = RandomArray(n);
        System.out.println("Enter search value : ");
        int target;
        target = sc.nextInt();
        PrintArray("The array:", array);
        // Call the LinearSearch() function to search for the 'target' in 'array' and store the positions.
        List<Integer> positions = LinearSearch(array, target);
        if (positions.isEmpty()) {
            // If the 'positions' list is empty, the target value was not found.
            System.out.println();
            System.out.println(target + " do not exist");
        } else {
            // If the 'positions' list is not empty, the target value was found.
            System.out.println();
            System.out.println("Found " + target + " at Index " + positions);
        }
    }

    public static int InputNumberArray() {
        Scanner scanner = new Scanner(System.in);
        int size;
        while (true) {  // Start an infinite loop to keep asking for input until a valid value is provided.
            try {
                System.out.println("Input Number of Array : ");
                size = Integer.parseInt(scanner.nextLine());  // Read the user's input and parse it as an integer.
                if (size > 0) {
                    break;
                    // if size > 0 , valid value and loop break 
                } else {
                    System.out.println("You have to input a positive number , Reinpiut : ");
                    // iff size <= 0 , Display notice 
                }
            } catch (NumberFormatException e) {
                System.out.println("Invalid value , Reinput : ");
                // if users input a special character , program will notice to users 
            }
        }
        return size;
    }

    public static int[] RandomArray(int ArraySize) {
        // Create an integer array with the specified size 'ArraySize'.
        int[] array = new int[ArraySize];
        // Create a random object name random 
        Random random = new Random();
        for (int i = 0; i < ArraySize; i++) {
            array[i] = random.nextInt(ArraySize);
        } // loop use to random each element of array 
        return array;
    }

    public static void PrintArray(String variable, int[] array) {
        System.out.print(variable);
        System.out.print("[");
        // loop use to accessed each element of array and display them
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i < array.length - 1) {
                System.out.print(",");
            }
        }// print each element of the array 
        System.out.print("]");
    }

    public static List<Integer> LinearSearch(int[] array, int target) {
        List<Integer> positions = new ArrayList<>(); // Create a list to store positions.

        for (int i = 0; i < array.length; i++) { // Iterate through the array.
            if (array[i] == target) { // Check if the current element equals the target.
                positions.add(i); // If found, add the index to the positions list.
            }
        }

        return positions; // Return the list of positions where the target was found.
    }
}