归并排序
Posted wronin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了归并排序相关的知识,希望对你有一定的参考价值。
#include <iostream> using namespace std; #define maxn 10005 #define inf 999999 int L[maxn], R[maxn],A[maxn],cnt,n; void merge(int left, int mid, int right) //合并 int len1 = mid - left; int len2 = right - mid; for (int i = 0; i< len1; i++) L[i] = A[left + i]; for (int i = 0; i<len2; i++) R[i] = A[mid + i]; L[len1] = R[len2] = inf; int i = 0, j = 0; for (int k = left; k < right; k++) cnt++; if (L[i] <= R[j]) A[k] = L[i++]; else A[k] = R[j++]; void mergesort(int left, int right) if (left + 1 >= right) return; int mid = (left + right) / 2; //分解 mergesort(left, mid); mergesort(mid, right); //合并 merge(left, mid, right); int main() cnt = 0; cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; mergesort(0, n); for (int i = 0; i<n; i++) cout << A[i] << " "; cout << endl; return 0;
以上是关于归并排序的主要内容,如果未能解决你的问题,请参考以下文章