美文网首页
Selction Search

Selction Search

作者: 綿綿_ | 来源:发表于2019-04-01 14:21 被阅读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));
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Selction Search

          本文链接:https://www.haomeiwen.com/subject/rtrvbqtx.html