最坏与最优情况时间复杂度:
O(nlogn)
思路:首先对数组进行分组(拆解)变成单个,用到辅助数组,后对其合并(也是排序过程)
注意 先建立辅助数组 -----》对数组进行拆分(递归拆分)--------》进行合并(合并到辅助数组里)-----》将辅助数组复制到原数组
#include <iostream>
using namespace std;
void Merge(int arr[], int trr[], int left, int mid, int right) {
int l_pos = left;
int r_pos = mid + 1;
int pos = left;
while (l_pos <= mid && r_pos <= right) {
if (arr[l_pos] < arr[r_pos]) {
trr[pos++] = arr[l_pos++];
} else {
trr[pos++] = arr[r_pos++];
}
}
while (l_pos <= mid) {