标签:c++
链接:
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int a[1000050]; int _sort(int left,int right) { int x=a[left]; while(left<right) { while(a[right]>x&&left<right) right--; if(left<right) { a[left]=a[right]; left++; } while(a[left]<x&&left<right) left++; if(left<right) { a[right]=a[left]; right--; } } a[left]=x; //left==right return left; } int unsorted_binarysearch(int left,int right,int key) //每次都选择起点作为“枢纽” { if(left>right) return -1; int x=a[left]; int mid_loc=_sort(left,right); if(x==key) return mid_loc; else if(x>key) unsorted_binarysearch(left,mid_loc-1,key); else unsorted_binarysearch(mid_loc+1,right,key); } int main() { // freopen("in.txt","r",stdin); int n,k; scanf("%d%d",&n,&k); for(int i=1; i<=n; i++) scanf("%d",&a[i]); cout<<unsorted_binarysearch(1,n,k)<<endl; return 0; }
标签:c++
原文地址:http://blog.csdn.net/axuan_k/article/details/46482759