Problem Description:
The array elements are searched and sorted, and the binary method and recursive implementation are used.
Full Example 1:
Public class sortdemo{ public static void main (String[] args) { int[] arr={10,2,300,41,15,6}; for (Int a:arr) { system.out.print ("[" +a+ "]"); } // new sortdemo (). Insertsort (arr); new sortdemo (). Binaryinsertsort (arr); system.out.println (); for (Int a:arr) { system.out.print ("[" +a+ "]"); } } //the public void of the binary sorting method binaryinsertsort (int[] a) { for (int i=1;i<a.length;i++) { int temp=a[i]; int low=0; int high=i-1; while (Low<=high) { int mid = (Low+high)/2; if (a[mid]<temp) { low=mid+1; } else { high=mid-1; } } for (int j=i-1;j>=low;j--) { a[j+1]=a[j]; } a[low]=temp; } } //Insert Sort public void Insertsort (int[] a) { int i;//the coordinates of the previous element of the element that participates in the comparison int key;//elements currently participating in the comparison for (int j=1;j<a.length;j++) { key=a[j]; i=j-1; while (i>=0 &&a[i]>key) { a[i+1]=a[i];//Move Right One i--;//i Front Shift } a[i+1]=key; } }}
Full instance 2:
Binary search method import java.util.*;p ublic class binarysearchdemo{ public static void main (String[] args) { int [] arr={10,2,300,41,15,6}; arrays.sort (arr);//natural sorting of arrays //system.out.println (New binarysearchdemo (). METHOD1 (arr, )); system.out.println (new Binarysearchdemo (). METHOD2 (arr.length-1,0,arr,41)); } //recursive implementation of binary lookup public string method2 (int b,int s,int[] a,int g) { int m; //the middle value coordinates if (s<b) {in the Find section m= (B+s)/2; if (a[m]==g) { return "You find" "+g+" "is the size in the array" "+ (m+1) +" "bit. "; } else if (A[M]>G)//If the median value is larger than the target value, compare the left half, { b=m-1;//the top coordinate in the Find section to the previous one in the middle value &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;METHOD2 (B,S,A,G); } else //If the median value is smaller than the target value, then the right half is compared { s=m+1;//the lowest bit of the Find section becomes the last of the median value &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;METHOD2 (b,s,a,g); } } return "You are looking for" +g+ "is not in the array. The } //parameter is an array and a target value public string method1 (int[] a,int g) { arrays.sort (a);// Natural sorting of an array int b=a.length-1;//the top-level coordinates in the Find section int s=0; //the lowest bit coordinates in the Find section int m; //find middle-value coordinates in section while (b>=s) { &nbsP;m= (b+s)/2; if (a[m]==g) { return "You find" "+g+" "is the size in the array" "+ (m+1) +" "bit. "; } else if (A[M]>G)//If the median value is larger than the target value, compare the left half, { b=m-1;//the top coordinate in the Find section to the previous one in the middle value } else //If the median value is smaller than the target value, then the right half is compared { s=m+1;//the lowest bit of the Find section becomes the last of the median value } } return "+g+" "You are looking for" is not in the array. "; } }
This article is from the "2019i" blog, be sure to keep this source http://2019i.blog.51cto.com/9962687/1617724
Java Sort Lookup algorithm--two-point method and Recursive application example