Selction Search

2019-04-01  本文已影响0人  綿綿_
import java.util.Scanner;

public class selectionSearch {
    static final int N=15;
    static int search(int a[],int n,int x)
    {
        int i;
        int f=-1;
        for(i=0;i<n;i++)
        {
            if(x==a[i])
            {
                f=i;
                break;
            }
        }
        return f;
    }
    public static void main(String[] args)
    {
        int x,n,i;
        int[] array = new int[N];
        
        for(i=0;i<N;i++)
        {
            array[i]=(int)(100+Math.random()*(100+1));
        }
        System.out.println("the original number is :");
        for(i=0;i<N;i++)
        {
            System.out.println(array[i]);
        }
        System.out.println("enter the number to look for");
        Scanner input=new Scanner(System.in);
        x=input.nextInt();
        n=search(array,N,x);
        if (n<0)
        {
            System.out.println("number not found");
        }
        else
        {
            System.out.println("the index of number is :"+(n+1));
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读