1. 折半查找算法
//折半查找算法 public static int zhebanSearch(int a[],int n, int x){ int low,high,mid; low=0; high=n-1; while(low<=high){ mid=(low+high)/2; if(a[mid]==x){ return mid; }else if(a[mid]>x){ high=mid-1; }else{ low=mid+1; } } return -1; }
2. 折半查找算法使用示例
package com.cn.find;import java.util.Scanner;//折半查找public class ZhebanSearch { public static final int SIZE=10; //折半查找算法 public static int zhebanSearch(int a[],int n, int x){ int low,high,mid; low=0; high=n-1; while(low<=high){ mid=(low+high)/2; if(a[mid]==x){ return mid; }else if(a[mid]>x){ high=mid-1; }else{ low=mid+1; } } return -1; } //快速排序的一次划分 public static int partition(int[] a,int left, int right){ int temp,l,r,p; l=left; r=right; p=(left+right)/2; temp=a[(left+right)/2]; //将中间元作为分界值 while(ltemp&&l