cpp-algo-cases/chapter_sorting/merge_sort.cpp

59 lines
1.6 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: merge_sort.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void merge(vector<int> &nums, int left, int mid, int right) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ [left, mid], <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ [mid+1, right]
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> tmp <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>źϲ<C5BA><CFB2><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>
vector<int> tmp(right - left + 1);
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
int i = left, j = mid + 1, k = 0;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B6BC><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>бȽϲ<C8BD><CFB2><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>Ԫ<EFBFBD>ظ<EFBFBD><D8B8>Ƶ<EFBFBD><C6B5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (i <= mid && j <= right) {
if (nums[i] <= nums[j])
tmp[k++] = nums[i++];
else
tmp[k++] = nums[j++];
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3>Ԫ<EFBFBD>ظ<EFBFBD><D8B8>Ƶ<EFBFBD><C6B5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (i <= mid) {
tmp[k++] = nums[i++];
}
while (j <= right) {
tmp[k++] = nums[j++];
}
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> tmp <20>е<EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8>ƻ<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD> nums <20>Ķ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
for (k = 0; k < tmp.size(); k++) {
nums[left + k] = tmp[k];
}
}
/* <20><EFBFBD><E9B2A2><EFBFBD><EFBFBD> */
void mergeSort(vector<int> &nums, int left, int right) {
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
if (left >= right)
return; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4>Ϊ 1 ʱ<><CAB1>ֹ<EFBFBD>ݹ<EFBFBD>
// <20><><EFBFBD>ֽ׶<D6BD>
int mid = (left + right) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>
mergeSort(nums, left, mid); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mergeSort(nums, mid + 1, right); // <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20>ϲ<EFBFBD><CFB2>׶<EFBFBD>
merge(nums, left, mid, right);
}
/* Driver Code */
int main() {
/* <20><EFBFBD><E9B2A2><EFBFBD><EFBFBD> */
vector<int> nums = {7, 3, 2, 6, 0, 1, 5, 4};
mergeSort(nums, 0, nums.size() - 1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD> nums = ";
printVector(nums);
return 0;
}