欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析java 循序與二元搜索算法

 更新時間:2015年02月02日 11:43:30   投稿:hebedich  
這篇文章主要簡單介紹了java 循序與二元搜索算法,需要的朋友可以參考下

循序搜索法

  就是一個一個去比較,找到時返回;

二元搜索法

  二元搜索算法是在排好序的數(shù)組中找到特定的元素.

  首先, 比較數(shù)組中間的元素,如果相同,則返回此元素的指針,表示找到了. 如果不相同, 此函數(shù)就會繼續(xù)搜索其中大小相符的一半,然后繼續(xù)下去. 如果剩下的數(shù)組長度為0,

  則表示找不到,那么函數(shù)就會結(jié)束.

實(shí)現(xiàn)代碼:

復(fù)制代碼 代碼如下:

package com.zc.manythread;
import java.util.Random;
import java.util.Scanner;
/**
 *
 * @author 偶my耶
 *    循環(huán)查找
 *    二元查找
 */
public class LinearSearch {
    //循序搜索
    public static int LinearSearch(int[] list,int item)
    {
        for(int i = 0 ; i < list.length;i++)
        {
            if(list[i]==item)
                return i;//找到傳回的位置
        }
        return -1;//找不到時
    }
    //二元搜尋,傳入的數(shù)先排序好,由小至大
    public static int BinarySearch(int[] list,int item)
    {
        //初始左右二邊
        int left = 0 ;
        int right = list.length;
        //左邊的索引位置小于右邊的索引的位置
        while(left<=right)
        {
            int mid = (left + right)/2;
            if(list[mid]==item)
                return mid;
            else
            {
                //所查詢值比中間值小,故值會在中間的左邊數(shù)列
                if(list[mid]>item)
                {
                    right = mid -1;
                }else
                {
                    left = mid +1;
                }
            }
        }
        return -1;//找不到時
    }
    /**
     * 產(chǎn)生隨機(jī)數(shù)組
     * @param count
     * @return
     */
    private static int[]  createDate(int count) {
        int[] data=new int[count];
          Random rand = new Random();
          boolean[] bool = new boolean[100];
          int num = 0;
          for (int i = 0; i < count; i++) {
           do {
            // 如果產(chǎn)生的數(shù)相同繼續(xù)循環(huán)
            num = rand.nextInt(100);
           } while (bool[num]);
           bool[num] = true;
           data[i]=num;
          }
          return data;
    }
    public static void main(String args[])
    {
        //輸入要查找的數(shù)
        Scanner in = new Scanner(System.in);
        //循序搜尋案列
        int[] list = createDate(10);
        System.out.println("原始數(shù)列:");
        for(int i = 0 ; i <list.length ; i ++)
        {
            System.out.print(list[i]+" ");
        }
        System.out.println("\r\n請輸入要查詢的數(shù):");
        int searchkey = in.nextInt();
        int ans =  LinearSearch(list,searchkey);
        if(ans>-1)
        {
            System.out.println("找到數(shù),位置在:"+(ans+1)+"位");
        }
        else
            System.out.println("找不著");
        //二元搜尋案列
        int[] list2 = {2,4,6,8,10,12,13,14,15,16};
        System.out.println("原始數(shù)據(jù):");
        for(int i = 0 ; i<list2.length ; i ++)
        {
            System.out.print(list2[i]+" ");
        }
        System.out.println("\r\n請輸入要查詢的數(shù):");
        int searchkey2 = in.nextInt();
        int ans2 =  BinarySearch(list2,searchkey2);
        if(ans2>-1)
        {
            System.out.println("找到數(shù),位置在:"+ans2+"位");
        }
        else
            System.out.println("找不著!");
    }
}

運(yùn)行結(jié)果

以上就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評論