The array is arranged in ascending order, moving through the loop array. There must be part of the left half or half or ascending.
Code:
public class Searchrotatearray {public static int search (int a[], int l, int u, int. x) {while (l<=u) {int m = (l+u)/2;if ( X==a[m]) {return m;} else if (A[l]<=a[m]) {///The left half is arranged in ascending order if (X>a[m]) {l=m+1;} else if (X>=a[l]) {u=m-1;} else{//x<a[l]l=m+1;}} else if (A[l]>a[m]) {//Right half ascending if (X>a[u]) {u=m-1;} else if (X>=a[m]) {l=m+1;} else{//x<a[m]u=m-1;}}} return-1;} public static void Main (string[] args) {int a[] = {15,16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}; System.out.println (Search (A, 0, a.length-1, 5));}}
Results:
8
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
However, the specified element is found in the rise period of the moving array