归并排序,本质上就是分治算法的一种,那么什么是分治算法呢。在算法与程序设计中,我们采用分而治之的策略就是分治算法,其核心就是把一个大规模的问题分解为若干小规模的相同子问题,分而治之,从而达到减少时间复杂度,提高运算效率的目的。
把要解决的问题分解成若干个规模较小的、相互独立的、与原问题形式相同的子问题。
寻求若干个子问题的解决办法,只要我们把子问题划分的足够小,我们就可以用简单的方法解决问题。
按照原问题的要求,将子问题逐层分解操作之后,再次合并构成原问题的解。
以数组【1, 3, 5, 1, 6, 100, 985, 221】为例子。
核心思想:
#include <iostream>
#include<vector>
using namespace std;
void merge(int* arr, int left, int mid, int right) {
vector<int> help;
int p1 = left;
int p2 = mid + 1;
int k = 0;
while (p1 <= mid && p2 <= right) {
if (arr[p1] <= arr[p2]) {
help.push_back(arr[p1]);
p1++;
}
else {
help.push_back(arr[p2]);
p2++;
}
}
while (p1 <= mid) {
help.push_back(arr[p1]);
p1++;
}
while (p2 <= right) {
help.push_back(arr[p2]);
p2++;
}
for (int i = left; i <= right; i++) {
arr[i] = help[k++];
}
}
void merge_sort(int* arr, int left, int right) {
if (left == right) return;
int mid = left + ((right - left) >> 1);
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
int main() {
int n, nums[INT16_MAX];
cout << "请输入数列中的元素的个数n为:\n";
cin >> n;
cout << "请依次输入数列中的元素为:\n";
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
merge_sort(nums, 0, n-1);
cout << "归并排序的结果为:\n";
for (int i = 0; i < 7; i++) {
cout << nums[i] << " ";
}
return 0;
}
运行结果:
# -*- coding: utf-8 -*-
# @Time : 2024/1/17 14:34
# @File : 归并排序.py
# @Description : None
# ----------------------------------------------
# ☆ ☆ ☆ ☆ ☆ ☆ ☆
# >>> Author : Kinght_123
# >>> Mail : 1304662247@qq.com
# >>> Blog : tim1304662247.blog.csdn.net
# ☆ ☆ ☆ ☆ ☆ ☆ ☆
'''归并排序'''
def merge_sort(arr, left, right):
if left == right:
return
mid = left + ((right - left) >> 1)
merge_sort(arr, left, mid)
merge_sort(arr, mid + 1, right)
merge(arr, left, mid, right)
def merge(arr, left, mid, right):
help = []
p1 = left
p2 = mid + 1
while p1 <= mid and p2 <= right:
if arr[p1] <= arr[p2]:
help.append(arr[p1])
p1 += 1
else:
help.append(arr[p2])
p2 += 1
while p1 <= mid:
help.append(arr[p1])
p1 += 1
while p2 <= right:
help.append(arr[p2])
p2 += 1
arr[left:right + 1] = help
if __name__ == '__main__':
ls = [1, 3, 5, 1, 6, 100, 985, 221]
merge_sort(ls, 0, len(ls) - 1)
print(ls)
运行结果: