合并排序的递归算法和非递归算法、快速排序算法

//合并排序递归
#include<iostream>
using namespace std;

template<class Type>
void Merge(Type c[], Type d[], int l, int m, int r)
{
	int i = l, j = m + 1, k = l;
	while ((i <= m) && (j <= r))
	{
		if (c[i] <= c[j])
		{
			d[k++] = c[i++];
		}
		else
		{
			d[k++] = c[j++];
		}
	}
	if (i>m)
	{
		for (int q = j; q <= r; q++)
		{
			d[k++] = c[q];
		}
	}
	else
	{
		for (int q = i; q <= m; q++)
		{
			d[k++] = c[q];
		}
	}
}

template<class Type>
void MergeSort(Type a[], Type b[], int left, int right)
{
	if (left<right)
	{
		int i = (left + right) / 2;
		MergeSort(a, b, left, i);
		MergeSort(a, b, i + 1, right);
		Merge(a, b, left, i, right);
		for (int i = left; i <= right; i++)
		{
			a[i] = b[i];
		}
	}
}

int main()
{
	int n, data[100], result[100];
	cout << "请输入数组的长度" << endl;
	cin >> n;
	cout << "请输入数组" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> data[i];
		result[i] = data[i];
	}
	MergeSort(data, result, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		cout << data[i] << "  ";
	}
	cout << endl;
	system("pause");
	return 0;
}

//合并排序非递归
#include<iostream>
using namespace std;

template<class Type>
void Merge(Type c[], Type d[], int l, int m, int r)
{
	int i = l, j = m + 1, k = l;
	while ((i <= m) && (j <= r))
	{
		if (c[i] <= c[j])
		{
			d[k++] = c[i++];
		}
		else
		{
			d[k++] = c[j++];
		}
	}
	if (i>m)
	{
		for (int q = j; q <= r; q++)
		{
			d[k++] = c[q];
		}
	}
	else
	{
		for (int q = i; q <= m; q++)
		{
			d[k++] = c[q];
		}
	}
}


template<class Type>
void MergePass(Type x[], Type y[], int s, int n)
{
	int i = 0;
	while (i <= n - 2 * s)
	{
		Merge(x, y, i, i + s - 1, i + 2 * s - 1);
		i = i + 2 * s;
	}
	if (i + s<n)Merge(x, y, i, i + s - 1, n - 1);
	else for (int j = i; j <= n - 1; j++)y[j] = x[j];
}
template<class Type>
void MergeSort(Type a[], int n)
{
	Type *b = new Type[n];
	int s = 1;
	while (s<n)
	{
		MergePass(a, b, s, n);
		s += s;
		MergePass(b, a, s, n);
		s += s;
	}
}

int main()
{
	int n, data[100];
	cout << "请输入数组的长度" << endl;
	cin >> n;
	cout << "请输入数组" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> data[i];
	}
	MergeSort(data, n);
	for (int i = 0; i < n; i++)
	{
		cout << data[i] << "  ";
	}
	cout << endl;
	system("pause");
	return 0;
}

//快速排序
#include<iostream>
#include <algorithm>
using namespace std;
template<class Type>
int Partition(Type a[], int p, int r)
{
	int i = p, j = r + 1;
	Type x = a[p];
	while (i<j)
	{
		while (a[++i]<x&&i<r);
		while (a[--j]>x);
		if (i >= j)break;
		swap(a[i], a[j]);
	}
	a[p] = a[j];
	a[j] = x;
	return j;

}
template<class Type>
void QuickSort(Type a[], int p, int r)
{
	if (p<r)
	{
		int q = Partition(a, p, r);
		QuickSort(a, p, q - 1);
		QuickSort(a, q + 1, r);
	}
}

int main()
{
	int n, data[100];
	cout << "请输入数组的长度" << endl;
	cin >> n;
	cout << "请输入数组" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> data[i];
	}
	QuickSort(data, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		cout << data[i] << "  ";
	}
	cout << endl;
	system("pause");
	return 0;
}
//以随机元素为基准的快速排序
#include<iostream>
#include <algorithm>
using namespace std;
template<class Type>
int Partition(Type a[], int p, int r)
{
	int i = p, j = r + 1;
	Type x = a[p];
	while (i<j)
	{
		while (a[++i]<x&&i<r);
		while (a[--j]>x);
		if (i >= j)break;
		swap(a[i], a[j]);
	}
	a[p] = a[j];
	a[j] = x;
	return j;

}
template<class Type>
int RandomizedPartition(Type a[], int p, int r)
{
	int x = r - p;
	int i = rand() % x + p;
	swap(a[i], a[p]);
	return Partition(a, p, r);
}
template<class Type>
void RandomizedQuickSort(Type a[], int p, int r)
{
	if (p<r){
		int q = RandomizedPartition(a, p, r);
		RandomizedQuickSort(a, p, q - 1);
		RandomizedQuickSort(a, q + 1, r);
	}
}

int main()
{
	int n, data[100];
	cout << "请输入数组的长度" << endl;
	cin >> n;
	cout << "请输入数组" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> data[i];
	}
	RandomizedQuickSort(data, 0, n - 1);
	for (int i = 0; i < n; i++)
	{
		cout << data[i] << "  ";
	}
	cout << endl;
	system("pause");
	return 0;
}

点赞