1. 程式人生 > >歸併排序(merge sort)c++實現

歸併排序(merge sort)c++實現

歸併排序(merge sort )

分裂過程圖解:

當i和j重合時,停止分裂。

歸併過程圖解:

c++程式碼實現如下

#include "stdafx.h"//dev下把這句話去掉,vs下保留
#include<iostream>
using namespace std;
void Merge(int* a, int l, int m, int r);
void MergeSort(int* a, int l, int r);


int main(void) {
	int a[8] = { 46,30,82,90,56,17,95,15 };
	MergeSort(a, 0, 7);
	for (int i = 0; i < 8; i++) {
		cout << a[i] << " ";
	}
	getchar();
}


void Merge(int* a , int l,int m,int r) {
	int l1 = l;
	int m1 = m;
	int r1 = r;
	int num = r - l + 1;
	int k = 0;
	int* temp = new int[num];
	while (l1 < m && m1 <= r) {
		if (a[l1] < a[m1]) {
			temp[k++] = a[l1++];
		}
		else {
			temp[k++] = a[m1++];
		}
	}
	while (m1 <= r) {
		temp[k++] = a[m1++];
	}
	while (l1 < m) {
		temp[k++] = a[l1++];
	}
	for (int n = 0; n < num; n++) {
		a[l+n] = temp[n];
	}
}



void MergeSort(int* a, int l, int r) {
	if (l < r) {
		int m = (l + r) / 2;
		MergeSort(a, l, m);
		MergeSort(a, m + 1, r);
		Merge(a, l, m + 1, r);
	}
}

參考部落格: