More search can refer to http://www.cnblogs.com/liuling/p/2013-7-24-01.html this is someone else's resources, feel write very full. You can study it carefully. /* Use binary to find the premise that the data is ordered (ascending). */class Halfsearchdemo {public static void main (string[] args) {int[] arr={1,2,3,6,2,3,5,1,8,9,3,5,2};sop ("keyword location:" + Halfsearch_2 (arr,6)); System.out.println ("Hello world!");} public static int halfsearch_2 (int[] arr,int key) {searchsort (arr); int Max=arr.length-1,min=0,mid;while (Min<=max) { Note is "<=" mid= (Min+max)/2;if (Key>arr[min]) Min=mid+1;else if (Key<arr[max]) Max=mid-1;elsereturn mid;} return-1;} public static void Pubblesort (int[] arr)//bubble sort, first determines the last position on the element {for (int i=0;i<arr.length-1;i++) {for (int j=0;j< arr.length-1-i;j++) {if (arr[j]>arr[j+1]) {int temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}} for (int k=0;k<arr.length;k++) SOP (Arr[k]);} public static void Searchsort (int[] arr)//Select sort first to determine the first position of the element {for (int. i=0;i<arr.length-1;i++) {for (int j=i+1;j< arr.length;j++) {if (arr[i]>arr[j]) {int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}}} for (int k=0;k<arr.length;k++) sOP (Arr[k]);} public static void Sop (Object obj) {System.out.println (obj);}}
The following is another representation of the binary lookup:
public static int Halfsearch (int[] arr,int key) {selectsort (arr); int max=arr.length-1;int min=0;int mid= (max+min)/2; while (Arr[mid]!=key) {if (Key>arr[mid]) min=mid+1; if (Key<arr[mid]) max=mid-1;if (Min>max) return-1;//cannot find the target element mid= (max+min)/2;} Return mid;//jumps out of the loop, indicating that the target element has been found, at which point the target element's index is returned. }
Java binary find bubble sort selection sort