归并排序 <-- 递归思想 分治思想 C++

【算法分析】
归并算法基于分治策略,分治策略利用递归实现。
归并排序理解之后,可以解决“
小和问题”、“逆序对问题”等经典问题。
下文归并排序的动画来源于:https://blog.csdn.net/kevinmeanscool/article/details/87916085



【算法代码一】
算法代码一是归并排序的一种实现方法。若去掉其中的第26行语句
ans+=mid-ls+1;  及第42行语句 cout<<ans; 前的注释,并在第40行语句 for(int i=1; i<=n; i++) cout<<a[i]<<" "; 前添加注释之后,算法代码一便成为求逆序对的代码。

#include <bits/stdc++.h>
using namespace std;

const int maxn=100005;
int a[maxn];
int t[maxn];

long long ans;

void merge_sort(int low,int high) {
	if(low>=high) return; //"low>=high" equivalent to "low==high"

	int mid=(low+high)>>1;
	merge_sort(low,mid);
	merge_sort(mid+1,high);

	for(int i=low; i<=high; i++) t[i]=a[i];

	int ls=low,hs=mid+1; //ls:low half interval's starting, hs:high half interval's starting
	for(int i=low; i<=high; i++) {
		if(ls>mid) a[i]=t[hs++];
		else if(hs>high) a[i]=t[ls++];
		else if(t[ls]<=t[hs]) a[i]=t[ls++];
		else {
			if(t[ls]>t[hs]) {
				//ans+=mid-ls+1;
				a[i]=t[hs++];
			}
		}
	}
}

int main() {
	int n;
	cin>>n;
	for(int i=1; i<=n; i++) cin>>a[i];

	merge_sort(1,n);

	for(int i=1; i<=n; i++) cout<<a[i]<<" ";

	//cout<<ans;

	return 0;
}

/*
in:
6
2 3 4 5 6 1
out:
1 2 3 4 5 6

in:
9
8 4 5 7 1 3 6 2 9
out:
1 2 3 4 5 6 7 8 9
*/

【归并排序代码执行模拟图】


【算法代码二】

#include <bits/stdc++.h>
using namespace std;

void merge(vector<int> &v, int low, int mid, int high) {
	vector<int> t(v); // Using v to define a vector copy t
	
	// k means where to start modifying the original array, 
	// i means to the start of the left half interval, 
	// j means to the start of the right half interval.
	int k=low,i=low,j=mid+1;
	while(k<=high) {
		if(i>mid) { // Only leave the right half interval's numbers not to be processed. 
			v[k++]=t[j++]; // Just copy the numbers on the right one by one.
		} else if(j>high) { // Only leave the left half interval's numbers not to be processed.
			v[k++]=t[i++]; // Just copy the numbers on the left one by one.
		} else if(t[j]<t[i]) { //The number on the right is less than the left.
			v[k++]=t[j++]; // Copy a number on the right.
		} else { //The number on the left is less than the right.
			v[k++]=t[i++]; // Copy a number on the left.
		}
	}
}

void merge_sort(vector<int> &v, int low, int high) {
	if(low>=high) return;

	int mid=(low+high)>>1;
	merge_sort(v,low,mid);
	merge_sort(v,mid+1,high);

	merge(v,low,mid,high); //Merge sorted arrays
}

int main() {
	vector<int> v;
	int x;
	while(scanf("%d",&x)!=EOF) { //while(cin>>x)
		v.push_back(x);
	}

	merge_sort(v,0,v.size()-1);

	for(int i=0; i<v.size(); i++) {
		printf("%d ",v[i]); //cout<<v[i]<<" ";
	}

	return 0;
}


/*
in:
8 4 5 7 1 3 6 2 0 9

out:
0 1 2 3 4 5 6 7 8 9
*/


【参考文献】
https://blog.csdn.net/qionggaobi9328/article/details/105676962
https://blog.csdn.net/zpznba/article/details/88395447
https://blog.csdn.net/zpznba/article/details/83745205
https://blog.csdn.net/kevinmeanscool/article/details/87916085

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值