题目: 给定你一个长度为 n 的整数数列。
请你使用归并排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。第二行包含 n 个整数(所有整数均在 1∼109 范围内),表示整个数列。
输出格式
输出共一行,包含 n 个整数,表示排好序的数列。数据范围: 1≤n≤100000
输入样例: 5 3 1 2 4 5 输出样例: 1 2 3 4 5
归并排序:将两个有序数列合二为一。
原理:
1.确定分界点mid = (l + r) / 2
2.递归排序left, right
3.合二为一
错误代码如下:
#include<iostream>
using namespace std;
const int N = 100010;
int q[N], tmp[N];
int n;
void merge_sort(int* q, int l, int r){
if (l >= r) return ;
int mid = (l + r) >> 1;
merge_sort(q, l, mid);
merge_sort(q, mid + 1, r);
int i = l, j = mid + 1, k = 0, tmp[j - i + 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 (k = 0, i = l; i <= r; i++, k++) q[i] = tmp[k];
}
int main(void){
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%d", &q[i]);
}
merge_sort(q, 0, n - 1);
for (int i = 0; i < n; i++){
printf("%d ", q[i]);
}
}
现象:segementation fault
错误原因:tmp[j - i +1]应为r - l + 1,记录的是从l到r长度的正确排序。正确代码如下:
#include<iostream>
using namespace std;
const int N = 100010;
int q[N], tmp[N];
int n;
void merge_sort(int* q, int l, int r){
if (l >= r) return ;
int mid = (l + r) >> 1;
merge_sort(q, l, mid);
merge_sort(q, mid + 1, r);
int i = l, j = mid + 1, k = 0, tmp[j - i + 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 (k = 0, i = l; i <= r; i++, k++) q[i] = tmp[k];
}
int main(void){
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%d", &q[i]);
}
merge_sort(q, 0, n - 1);
for (int i = 0; i < n; i++){
printf("%d ", q[i]);
}
}