/*C++实现快速排序*/ #include<iostream> using namespace std; int partition(int a[],int p,int r) { int x=a[r]; int i=p-1; int j=p; for(;j<r;j++) { if(a[j]<x) { i++; int temp=a[j]; a[j]=a[i]; a[i]=temp; } } int temp=a[r]; a[r]=a[i+1]; a[i+1]=temp; return i+1; } void quicksort(int a[],int p,int r) { if(p<r) { int q=partition(a,p,r); quicksort(a,p,q-1); quicksort(a,q+1,r); } return ; } int findin(int a[],int length,int start,int n) { int b=start+length/2; if(n==a[b]) { return b; } else if(n<a[b]) { return findin(a,b-start,start,n); } else if(n>a[b]) { return findin(a,length-b-1,b+1,n); } cout<<"can't find the num:"<<n<<endl; return -1; } int find(int a[],int length,int n) { return findin(a,length,0,n); } int main() { int a[8]={112,12,3,56,2,12,32,20}; quicksort(a,0,7); for(int i=0;i<8;i++) { cout<<a[i]<<" "; } cout<<endl; cout<<find(a,8,2)<<endl; return 0; }