Java array in linear search and binary search algorithm

First, the search algorithm for linear array.

Finding: As the name suggests, is what you want to find an element in the array to see if in the array.

Find the principle of linear: linear search is the most basic way to find it is to compare the various elements of the target elements and an array of general they want to get in, than to find the target element for success, otherwise not found.

Let's write an implementation of the method of linear array lookup using java language:

//定义线性查找的方法,参数为目标元素
  public static void LineSearch(int target){
	  //定义目标数组arr,并初始化
		int[] arr = new int[] {1,8,10,6,30,5,12,23};
	  //定义目标元素的下标,初始为-1
		int index=-1;
	  /*进行for循环,将数组中的元素与目标元素对比,如存在,则返回目标元素在
	   * 数组中的下标,若不存在,则返回初始下标-1
	  */
		for(int i=0; i<arr.length; i++) {
			if(arr[i]==target) {
				index = i;
				break;  //终止当前循环
			}		
		}
	  System.out.println("线性查找算法的目标元素在数组中的下标为:"+index);
 }

This code is a linear search method is implemented, if the target element in the array, the output of the target element in the array subscripts, whereas the output of -1.

To run the method in java, you need to call this method in the main method, here are the complete code.

Second, the search algorithm for binary array.

Binary search algorithm works: first of all to ensure that the data in the array are ordered (the default is small to large order), find the target element in the ordered data, look for the above linear algorithm for each ratio compared to efficiency much faster. We must note that the premise is ordered data, if not ordered data, you can not use binary search algorithm.

//定义二分查找的方法,参数为目标元素
  public static void BinarySearch(int target) {
	  //定义目标数组newArr,并初始化,注意,二分查找的前提是:一组数是按从小到大排列的
	  int[] newArr = new int[] {1,2,3,4,5,6,7,8,9};
	  //定义目标元素的下标,初始为-1
	  int index = -1;
	  //记录数组的开始位置
	  int begin = 0;
	  //记录数组的结束位置
	  int end = newArr.length-1;
	  //记录数组的中间位置
	  int mid = (begin+end)/2;
	  
	  //循环查找
	  while(true) {
  //上开始位置在结束位置之后或重合,此时还没查找到目标元素,则代表数组中没有该元素,返回-1
		  if(begin>=end) {
			  return -1;
		  }
		  //判断中间的这个元素是否是要查找的元素
		  if(newArr[mid]==target) {
			  index=mid;
			  break;
	      //中间这个元素不是要查找的元素
		  }else {
			  //如果中间这个元素比目标元素大
			  if(newArr[mid]>target) {
				  end = mid-1;   //把结束位置调整到中间位置的前一个元素
			  }else {
				  begin = mid+1; //把开始位置调整到中间位置的后一个元素
			  }
			 //确定新的中间元素
			  mid = (begin+end)/2;
		  }  
	  }
	  System.out.println("二分查找算法的目标元素的下标为:"+index);
  }

The above code is to find the binary method to achieve, if the target element in the array, the output of the target element in the array subscripts, whereas the output of -1.

To run the method in java, you need to call this method in the main method, here are the complete code.

Complete code:

public class ArraySearch{
	
//定义线性查找的方法,参数为目标元素
  public static void LineSearch(int target){
	  //定义目标数组arr,并初始化
		int[] arr = new int[] {1,8,10,6,30,5,12,23};
	  //定义目标元素的下标,初始为-1
		int index=-1;
	  /*进行for循环,将数组中的元素与目标元素对比,如存在,则返回目标元素在
	   * 数组中的下标,若不存在,则返回初始下标-1
	  */
		for(int i=0; i<arr.length; i++) {
			if(arr[i]==target) {
				index = i;
				break;  //终止当前循环
			}		
		}
	  System.out.println("线性查找算法的目标元素在数组中的下标为:"+index);
 }
  
//定义二分查找的方法,参数为目标元素
  public static void BinarySearch(int target) {
	  //定义目标数组newArr,并初始化,注意,二分查找的前提是:一组数是按从小到大排列的
	  int[] newArr = new int[] {1,2,3,4,5,6,7,8,9};
	  //定义目标元素的下标,初始为-1
	  int index = -1;
	  //记录数组的开始位置
	  int begin = 0;
	  //记录数组的结束位置
	  int end = newArr.length-1;
	  //记录数组的中间位置
	  int mid = (begin+end)/2;
	  
	  //循环查找
	  while(true) {
  //上开始位置在结束位置之后或重合,此时还没查找到目标元素,则代表数组中没有该元素,返回-1
		  if(begin>=end) {
			  return -1;
		  }
		  //判断中间的这个元素是否是要查找的元素
		  if(newArr[mid]==target) {
			  index=mid;
			  break;
	      //中间这个元素不是要查找的元素
		  }else {
			  //如果中间这个元素比目标元素大
			  if(newArr[mid]>target) {
				  end = mid-1;   //把结束位置调整到中间位置的前一个元素
			  }else {
				  begin = mid+1; //把开始位置调整到中间位置的后一个元素
			  }
			 //确定新的中间元素
			  mid = (begin+end)/2;
		  }  
	  }
	  System.out.println("二分查找算法的目标元素的下标为:"+index);
  }
  
 //在主方法中进行调用测试
public static void main(String[] args) {
			//调用线性查找函数,将参数赋为6
			LineSearch(6);	
			//调用二分查找函数,将参数赋为3
			BinarySearch(3);
	}
}

Results of the:

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 20 original articles · won praise 2 · Views 1591

Guess you like

Origin blog.csdn.net/weixin_42132733/article/details/105108132