快速排序
void quick_sort(int q[], int l, int r){
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + (r - l >> 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 arr[], int l, int r){
if(l==r)return;
int mid = l+((r-l) >>1);
merge_sort(arr,l,mid);
merge_sort(arr,mid+1,r);
merge(arr,l,mid,r);
}
void merge(int arr[] ,int l , int m, int r){
int sortedArray[r-l+1];
int lp = l, rp = m +1, i = 0;
while(lp <= m && rp <= r) sortedArray[i++] = arr[lp] < arr[rp]? arr[lp++] : arr[rp++];
while(lp <= m) sortedArray[i++] = arr[lp++];
while(rp <= r) sortedArray[i++] = arr[rp++];
for (i = 0;i<r-l+1;i++) arr[l+i] = sortedArray[i];
}
标签:sort,归并,int,模版,arr,mid,merge,排序
From: https://www.cnblogs.com/yabushier/p/17845626.html