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

本文提供了三种排序算法的C++实现:合并排序(递归与非递归版本)和两种快速排序算法(一种使用固定基准,另一种使用随机基准)。通过这些代码示例,读者可以了解排序算法的工作原理,并学习如何在C++中实现它们。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//合并排序递归
#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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值