赞
踩
数组 {1,3, 8, 10, 11, 67, 100}, 编程实现二分查找, 要求使用非递归的方式完成.
package com.qf.math; public class BinaryMathDemo { public static void main(String[] args) { int[] arr={1,3, 8, 10, 11, 67, 100}; int i = binaryFind(arr, 10); System.out.println("index="+i); } //二分查找 public static int binaryFind(int[] arr,int target){ int left=0; int right=arr.length-1; while (left<=right){ int mid=(left+right)/2; if (arr[mid]==target){ return mid; }else if (arr[mid]>target){ right=mid-1; }else{ left=mid+1; } } return -1; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。