C写个快排就行了。
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int divide(int* A,int head, int tail){
if(head==tail) return head ;
int x =A[head];
while(head < tail){
while(head < tail && A[tail] > x) tail--;
if(head < tail) A[head++]=A[tail];
while(head < tail && A[head] < x) head++;
if(head < tail) A[tail--]=A[head];
}
A[head]=x;
return head;
}
void quicksort(int* A, int head, int tail){
if(head >= tail) return;
int x= divide(A,head,tail);
if(x>head) quicksort(A,head,x-1);
if(x<tail) quicksort(A,x+1,tail);
}
bool judge(int* A , int n ,int x){
int head =0;
int tail=n-1;
while(head<=tail){
int mid = (head+tail)/2;
if(A[mid]==x){
return true;
}else if(A[mid] < x){
head=mid+1;
}else{
tail=mid-1;
}
}
return false;
}
int main(){
int n ;
while(scanf("%d",&n) != EOF){
int* A=(int*)malloc(sizeof(int )*n);
for(int i = 0 ;i < n ;i++){
scanf("%d",&A[i]);
}
quicksort(A,0,n-1);
int m;
scanf("%d",&m);
for(int i =0;i<m;i++){
int t;
scanf("%d",&t);
if(judge(A,n,t)){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
return 0;
}
结果如下:
标签:head,return,int,KY199,while,tail,查找,include From: https://www.cnblogs.com/llllmz/p/17962463