归并排序模版
#include <iostream>
using namespace std;
int n;
const int N = 1e6+10;
int q[N],tmp[N];
void merge_sort(int l,int r,int q[]){
if(l>=r) return;
int mid =l+r>>1;
merge_sort(l,mid,q);
merge_sort(mid+1,r,q);
//归并的的过程
int k=0,i=l,j=mid+1;
while(i<=mid&&j<=r){
if(q[i]<=q[j]) tmp[k++]=q[i++];
else tmp[k++]=q[j++];
}
while(i<=mid) tmp[k++]=q[i++];
while(j<=r) tmp[k++]=q[j++];
for(i=l,j=0;i<=r;i++,j++) q[i]=tmp[j];
}
int main()
{
scanf("%d", &n);
for (int i = 0; i <n; i ++ ){
scanf("%d",&q[i]);
}
merge_sort(0,n-1,q);
for(int i=0;i<n;i++){
printf("%d ",q[i]);
}
}
快排和二分
#include <iostream>
using namespace std;
const int N = 100010;
int n,m;
void quick_sort(int l,int r,int p[]){
if(l>=r) return;
int i=l-1,j=r+1;
int x=p[i+j>>1];
while(i<j){
do i++;while(p[i]<x);
do j--;while(p[j]>x);
if(i<j) swap(p[i],p[j]);
}
quick_sort(l,j,p);
quick_sort(j+1,r,p);
}
int main()
{
scanf("%d", &n);
int p[n];
for (int i = 0; i < n; i ++ ){
scanf("%d", &p[i]);
}
quick_sort(0,n-1,p);
scanf("%d",&m);
int x;
for(int i=0;i<m;i++){
scanf("%d", &x);
int l=0,r=n-1;
while(l<r){
int mid=l+r >>1;
if(p[mid]>=x) r=mid;
else l=mid+1;
}
if(p[l]!=x) printf("NO\n");
// else{
// int l=0,r=n-1;
// while(l<r){
// int mid=l+r+1 >> 1;
// if(p[mid]<=x) l=mid;
// else r=mid-1;
// }
// }
else{
printf("YES\n");
}
}
}