package abc;//先排序,后查找public class ban {private static int[] arr={3,5,54,67,34,78,43,97,23,98,44,89,45};public static void sort(int a[]){for (int i=0;i<a.length-1;i++){for(int j=i+1;j
a[j]){int tem=a[i];a[i]=a[j];a[j]=tem;}}}} public static void sortl(int data[]) { for (int i = 0; i < data.length -1; i++) { for (int j = 0; j < data.length - i - 1; j++) { if (data[j] > data[j + 1]) { int temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; } } } } public static int zhebanfind(int x,int a[]){ int left=0; int right=a.length-1; for(int i=left;i>1; if(a[center]>x){ right=center-1; }else if(a[center]<x){ left=center+1; }else{ return center; } } return -1; } public static void main(String[] args) {sortl(arr);for (int i=0;i<arr.length;i++){System.out.println(arr[i]);}int s=zhebanfind(54,arr);System.out.println(s);}}
折半算法
猜你喜欢
转载自blog.csdn.net/qq_34295546/article/details/52437571
今日推荐
周排行