二分法的代码java 二分法详解

二分法查找的java代码

public class ef {

创新互联公司服务项目包括振安网站建设、振安网站制作、振安网页制作以及振安网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,振安网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到振安省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

public static void main(String[] args) {

int[] a = { 4, 8, 12, 44, 69, 71, 98, 132 ,133};

int m = ef.ef(a, 0, a.length, 71);

if(m!=-1){

System.out.println(a[m]+"====="+m);

}

System.out.println("不存在该数字");

}

public static int ef(int[] a, int from, int to, int b) {

int midel = (from + to) / 2;

if ((to - from) = 1 b != a[midel]) {

return -1;

}

if (b a[midel]) {

return ef(a, midel, to, b);

} else if (b a[midel]) {

return ef(a, from, midel, b);

} else {

return midel;

}

}

}

Java二分法

首先得告诉你,二分法的前提是必须是顺序方式存储,而且必须是排好序了的。比如要从100个数中查找某一个数,前提是这一百个数是排好序(这里假如从小到大)的,然后找到最中间的数,若最中间的数(这里是第50个)比你要找的这个数大那你只需要在1到49个数里找,然后再取最中间的数,再判断,如此往复下去,最多次数,你算算看,

java 二分法 排序

二分排序就是用先用二分查找法来查某一个元素,然后再用别的排序算法来进行排序。

package insert;

public class InsArrayApp {

public static void main(String[] args) {

int size = 100;

InsArray arr = new InsArray(size);

arr.insert(10);

arr.insert(9);

arr.insert(8);

arr.insert(7);

arr.insert(6);

arr.insert(10);

arr.insert(9);

arr.insert(8);

arr.insert(5);

arr.insert(4);

arr.insert(3);

arr.insert(2);

arr.insert(1);

arr.display();

// arr.insertSort();

// arr.display();

// System.out.println(arr.median());

// arr.noDups();

arr.noDups2();

arr.display();

}

}

class InsArray {

private int[] a;

private int nElems;

public InsArray(int size) {

a = new int[size];

nElems = 0;

}

public void insert(int value) {

a[nElems] = value;

nElems++;

}

public void display() {

for (int i = 0; i nElems; i++) {

System.out.print(a[i] + " ");

}

System.out.println();

}

public void insertSort() {

int out, in;

int copy = 0;

int compare = 0;

/* for(out = 1;outnElems;out++){

int tmp = a[out];

in = out;

while(in0a[in-1]=tmp){

a[in] = a[in-1];

--in;

}

a[in] = tmp;

}*/

for(out = 1;outnElems;out++){

int tmp = a[out];

in = out;

while(in0){

if(a[in-1]=tmp){

a[in] = a[in-1];

--in;

++copy;

++compare;}

else{

break;

}

}

++compare;

a[in] = tmp;

}

System.out.println("copy:" + copy + "compare:" + compare);

}

public int median(){

insertSort();

int m = nElems/2;

return a[m];

}

public void noDups(){

insertSort();

/*

InsArray tmp = new InsArray(nElems);

for(int i = 0;inElems;i++){

for(int j = i+1;jnElems;j++)

if(a[i] == a[j]){

a[j] = -1;

}

if(a[i]!=-1)

tmp.insert(a[i]);

}

*/

InsArray tmp = new InsArray(nElems);

int i;

for(int j = 0;jthis.nElems;j++){

/*if(tmp.nElems==tmp.find(this.a[j])) //binary find

tmp.insert(this.a[j]);

else

continue;*/

for( i = 0; i tmp.nElems; i++) { // for each element

if(tmp.a[i]==this.a[j]) // found searchKey?

break;

}

if(i==tmp.nElems) // no

tmp.insert(this.a[j]);

}

this.a = tmp.a;

this.nElems = tmp.nElems;

}

public int find(long searchKey) {

int lowerBound = 0;

int upperBound = nElems-1;

int curIn;

while(true) {

curIn = (lowerBound + upperBound)/2;

if(a[curIn]==searchKey)

return curIn;

else if(lowerBoundupperBound)

return nElems;

else {

if(a[curIn]searchKey)

upperBound = curIn-1;

else

lowerBound = curIn+1;

}

}

}

public void noDups2(){

insertSort();

for(int i = 0;inElems;i++){

for(int j = i+1;jnElems;j++)

if(a[i] == a[j]){

a[j] = -1;

}

}

display();

int index = 0;

for(int i=0;inElems;i++){

if(a[i]!=-1){

index++;

}else{

for(int j=index+1;jnElems;j++){

if(a[j]!=-1){

a[index] = a[j];

a[j]=-1;

index++;

break;

}

}

}

}

nElems = index;

}

}

上面的代码,是我以前敲的,有个find()方法是二分查找,然后再用插入排序去进行排序。


本文题目:二分法的代码java 二分法详解
地址分享:http://myzitong.com/article/hipeoc.html