快速排序
void quick_sort(int q[], int l, int r) { if (l >= r) return; int i = l - 1, j = r + 1, x = q[l + r >> 1]; while (i < j) { do i ++ ; while (q[i] < x); do j -- ; while (q[j] > x); if (i < j) swap(q[i], q[j]); } quick_sort(q, l, j); quick_sort(q, j + 1, r); }
归并排序
void merge_sort (int a[],int l,int r){ if (l>=r) return ; int mid=l+r>>1; merge_sort(a,l,mid); merge_sort(a,mid+1,r); int k=0,i=l,j=mid+1; while(i<=mid && j<=r){ if (a[i]<=a[j]) tmp[k++]=a[i++]; else tmp[k++]=a[j++]; } while(i<=mid)tmp[k++]=a[i++]; while(j<=r) tmp[k++]=a[j++]; for (int i=l,j=0;i<=r;i++,j++) a[i]=tmp[j]; }
二分
r=mid
while(l<r){ int mid=l+r>>1; if (q[mid]>=x) r=mid; else l=mid+1; }
l=mid
while(l<r){ int mid=l+r+1>>1; if (q[mid]<=x) l=mid; else r=mid-1; }
标签:二分,sort,归排,merge,int,mid,快排,while,quick From: https://www.cnblogs.com/Map1eaf/p/17152933.html