본문 바로가기
💻 하나씩 차곡차곡/자료구조 & 알고리즘(JAVA)

선형검색 (chap03/SeqSearch)

by 뚜루리 2022. 10. 27.
728x90
320x100

while문으로 구현

package chap03;

import java.util.Scanner;

public class SeqSearch {

    static int seqSearchSen(int[] a, int n, int key) {

        int i = 0;

        while(true) {
            if(i == n)
                return  -1; // 검색 실패 -1을 반환.
            if (a[i] == key) //검색 성공.
                return  i; // 검색 실패 -1을 반환.
            i++;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("요솟수 : ");
        int num = scan.nextInt();
        int[] x = new int[num + 1]; //요솟수가 num + 1 인 배열

        for(int i = 0; i < num; i++){
            System.out.println("x[" + i + "] : ");
            x[i] = scan.nextInt();
        }

        System.out.println("검색할 값 : "); //키 값을 입력 받음.
        int ky = scan.nextInt();

        int idx = seqSearchSen(x, num, ky); //배열 x에서 값이 Ky인 요소를 검색

        if (idx == -1)
            System.out.println("그 값의 요소가 없습니다.");
        else
            System.out.println("그 값은 x[" + idx + "]에 있습니다.")

    }

}

 

 

for문으로 구현

package chap03;

import java.util.Scanner;

public class SeqSearchFor {

    static int seqSearchSen(int[] a, int n, int key) {

        for (int i = 0; i < n; i++)
            if(a[i] ==key)
                return i;  //검색 성공.
        return  -1; // 검색 실패 -1을 반환.

    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("요솟수 : ");
        int num = scan.nextInt();
        int[] x = new int[num + 1]; //요솟수가 num + 1 인 배열

        for(int i = 0; i < num; i++){
            System.out.println("x[" + i + "] : ");
            x[i] = scan.nextInt();
        }

        System.out.println("검색할 값 : "); //키 값을 입력 받음.
        int ky = scan.nextInt();

        int idx = seqSearchSen(x, num, ky); //배열 x에서 값이 Ky인 요소를 검색

        if (idx == -1)
            System.out.println("그 값의 요소가 없습니다.");
        else
            System.out.println("그 값은 x[" + idx + "]에 있습니다.");



    }




}

 

 

보초법으로 선형검색 구현

  • 티끌 모아 태산. 조금이라도 리소스 줄이기.
package chap03;

import java.util.Scanner;

public class SeqSearchSen {

    static int seqSearchSen(int[] a, int n, int key) {

        int i = 0;

        a[n] = key; //보초를 추가

        while(true) {
            if (a[i] == key) //검색 성공.
                break;
            i++;
        }
        return i == n ? -1 : i;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("요솟수 : ");
        int num = scan.nextInt();
        int[] x = new int[num + 1]; //요솟수가 num + 1 인 배열

        for(int i = 0; i < num; i++){
            System.out.println("x[" + i + "] : ");
            x[i] = scan.nextInt();
        }

        System.out.println("검색할 값 : "); //키 값을 입력 받음.
        int ky = scan.nextInt();

        int idx = seqSearchSen(x, num, ky); //배열 x에서 값이 Ky인 요소를 검색

        if (idx == -1)
            System.out.println("그 값의 요소가 없습니다.");
        else
            System.out.println("그 값은 x[" + idx + "]에 있습니다.");



    }




}
728x90
320x100