算法程序

  算法程序  



1、//	从小到大排序的函数模版
template <class T> void Sort(T arr[], int size)
{
	T temp;
	for (int i = 0; i != size - 1; ++i)
		for (int j = i + 1; j != size; ++j)
			if (arr[i] > arr[j])
			{
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
}



2、//	二分查找法的函数模版
//	arr中的元素应按从小到大排序,返回目标的索引值
template <class T> int BinarySearch(T *arr, T target_value, int arr_size)
{
	int mid_idx, left_idx = 0, right_idx = arr_size - 1;

	while (left_idx <= right_idx)
	{
		mid_idx = left_idx + ((right_idx - left_idx) >> 1);

		if (*(arr + mid_idx) == target_value)
			return mid_idx;		// 找到,则返回目标的索引值
		else if (*(arr + mid_idx) < target_value)
			left_idx = mid_idx + 1;
		else
			right_idx = mid_idx - 1;
	}

	return -1;					// 如果没有找到,返回 -1
}



3、// 归并排序  非递归方式-推荐
#include <iostream>

using namespace std;


//================================================================
void Merge(int sr[], int tr[], unsigned int idx_start, unsigned int idx_mid, unsigned int idx_end)
{
	unsigned int n = idx_start;
	unsigned int i = idx_start, j = idx_mid + 1;

	for (; i <= idx_mid && j <= idx_end; )
	{
		if (sr[i] <= sr[j])
			tr[n++] = sr[i++];
		else
			tr[n++] = sr[j++];
	}

	while (i <= idx_mid)
	{
		tr[n++] = sr[i++];
	}
	while (j <= idx_end)
	{
		tr[n++] = sr[j++];
	}
}

void MergePass(int sr[], int tr[], unsigned int step, unsigned int n_total)
{
	unsigned int i = 0;
	while (i + 2 * step - 1 <= n_total-1)
	{
		Merge(sr, tr, i, i + step - 1, i + 2 * step - 1);
		i += 2 * step;
	}
	if (i + step - 1 < n_total - 1)
		Merge(sr, tr, i, i + step - 1, n_total - 1);
	else
		for (unsigned int j = i; j != n_total; ++j)
			tr[j] = sr[j];
}

void MergeSort(int x[], const unsigned int n_total)
{
	int *tr = new int[n_total]();
	unsigned int step = 1;

	while (step < n_total)
	{
		MergePass(x, tr, step, n_total);
		step <<= 1;				// 左移一位
		MergePass(tr, x, step, n_total);
		step <<= 1;				// 左移一位
	}
}
//================================================================


int main(int argc, char *argv[])
{
	int x[] = { 50, 10, 90, 30, 70, 40, 80, 60, 20 };
	const unsigned int n_total = sizeof(x) / sizeof(int);	// 得到元素数量

	for (unsigned int i = 0; i != n_total; ++i)
		cout << x[i] << '\t';
	cout << endl << endl;

	MergeSort(x, n_total);
	for (unsigned int i = 0; i != n_total; ++i)
		cout << x[i] << '\t';
	cout << endl << endl;

	system("pause");
	return 0;
}



4、// 快速排序
#include <iostream>
#include <vector>

using namespace std;


//================================================================
int Partition(vector<int> &vec, int idx_low, int idx_high)
{
	int pivot_key = vec[idx_low];
	while (idx_low < idx_high)
	{
		// 注意下面两个 while 的顺序,才能防止 x[idx_low] 不是最小时能够被交换
		// 否则 x[idx_low] 将一直处于最左端
		while (idx_low < idx_high && pivot_key <= vec[idx_high])
			--idx_high;
		swap(vec[idx_low], vec[idx_high]);
		while (idx_low < idx_high && pivot_key >= vec[idx_low])
			++idx_low;
		swap(vec[idx_low], vec[idx_high]);
	}

	return idx_low;
}

void QSort(vector<int> &vec, int idx_low, int idx_high)
{
	if (idx_low < idx_high)
	{
		int pivot = Partition(vec, idx_low, idx_high);	// 左小右大
		QSort(vec, idx_low, pivot - 1);
		QSort(vec, pivot + 1, idx_high);
	}
}
//================================================================


int main(int argc, char *argv[])
{
	vector<int> vec = { 50, 10, 90, 30, 70, 40, 80, 60, 20 };

	QSort(vec, 0, vec.size() - 1);
	for (auto &ele : vec)
		cout << ele << "  ";

	cout << endl << endl;
	system("pause");
	return 0;
}



5、// 冒泡排序
#include <iostream>

using namespace std;


//================================================================
void BubbleSort(int x[], const unsigned int n_total)
{
	for (unsigned int i = 0; i != n_total - 1; ++i)
		for (unsigned int j = n_total - 1; j > i; --j)
		{
			if (x[j] < x[j - 1])
				swap(x[j], x[j - 1]);
		}
}
//================================================================


int main(int argc, char *argv[])
{
	int x[] = { 50, 10, 90, 30, 70, 40, 80, 60, 20 };
	const unsigned int n_total = sizeof(x) / sizeof(int);	// 得到元素数量

	for (unsigned int i = 0; i != n_total; ++i)
		cout << x[i] << '\t';
	cout << endl << endl;

	BubbleSort(x, n_total);
	for (unsigned int i = 0; i != n_total; ++i)
		cout << x[i] << '\t';
	cout << endl << endl;

	system("pause");
	return 0;
}



6、/**********  动态规划  01 背包  **********/
/*************************************************
Description:  有N个物品,每个物品的重量为 w[j]、价值为 v[j];
现在有一个背包,它所能容纳的重量为 bag_capacity。
求:该背包所能容纳的最大价值?

参考:https://www.jianshu.com/p/d9a0624c05e7
*************************************************/
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


/*************************************************
Function: GetMaxValue_ZeroOneBag
Description: 得到01背包所能容纳的最大价值
Input:  vec_vec_01goods: 各单件物品的 体积/质量、价值
bag_capacity:背包容量
Return: 01背包能够容纳的最大价值
*************************************************/
template<class T> size_t GetMaxValue_ZeroOneBag(const vector<vector<T>> &vec_vec_01goods, const size_t &bag_capacity)
{
	size_t max_val = 0;
	if (!vec_vec_01goods.empty() && bag_capacity > 0)
	{
		vector<size_t> vec_dp(bag_capacity + 1, 0);

		vector<T> good_cur;		// 存放当前物品的属性  体积/质量、价值
		const size_t goods_kinds = vec_vec_01goods.size();
		for (size_t j = 0; j != goods_kinds; ++j)
		{
			good_cur = vec_vec_01goods[j];

			for (size_t i = bag_capacity; i >= good_cur[0]; --i)
				vec_dp[i] = max(vec_dp[i], good_cur[1] + vec_dp[i - good_cur[0]]);
		}

		max_val = vec_dp.back();
	}

	return max_val;
}


int main(int argc, char* argv[])
{
	// 体积/质量、价值
	vector<vector<int>> vec_vec_01goods = { { 2, 6 },{ 2, 3 },{ 6, 5 },{ 5, 4 },{ 4, 6 } };

	size_t max_val = GetMaxValue_ZeroOneBag(vec_vec_01goods, 10);
	cout << max_val << endl;

	system("pause");
	return 0;
}



7、/**********  动态规划  完全背包  **********/
/*************************************************
Description:  有N种物品(各自都有无限个),每种物品的重量为 w[j]、价值为 v[j];
现在有一个背包,它所能容纳的重量为 bag_capacity。
求:该背包所能容纳的最大价值?
*************************************************/
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;


/*************************************************
Function: GetMaxValue_CompletelyBag
Description: 得到完全背包所能容纳的最大价值
Input:  vec_vec_completelygoods: 各种物品的 体积/质量、价值
bag_capacity:背包容量
Return: 完全背包能够容纳的最大价值
*************************************************/
template<class T> size_t GetMaxValue_CompletelyBag(
	const vector<vector<T>> &vec_vec_completelygoods, const size_t &bag_capacity)
{
	size_t max_val = 0;
	if (!vec_vec_completelygoods.empty() && bag_capacity > 0)
	{
		vector<size_t> vec_dp(bag_capacity + 1, 0);

		vector<T> good_cur;		// 存放当前物品的属性  体积/质量、价值
		const size_t goods_kinds = vec_vec_completelygoods.size();
		for (size_t j = 0; j != goods_kinds; ++j)
		{
			good_cur = vec_vec_completelygoods[j];

			for (size_t i = good_cur[0]; i <= bag_capacity; ++i)	// 从左往右
				vec_dp[i] = max(vec_dp[i], good_cur[1] + vec_dp[i - good_cur[0]]);
		}

		max_val = vec_dp.back();
	}

	return max_val;
}


int main(int argc, char* argv[])
{
	// 体积 价值
	vector<vector<int>> vec_vec_completelygoods = { { 6, 7 },{ 5, 4 },{ 5, 5 }};

	size_t max_val = GetMaxValue_CompletelyBag(vec_vec_completelygoods, 10);
	cout << max_val << endl;

	system("pause");
	return 0;
}



8、/**********  动态规划  多重背包  **********/
/*************************************************
Description:  有N种物品,每个物品的重量为 w[j]、价值为 v[j]、数量为 c[j];
现在有一个背包,它所能容纳的重量为 bag_capacity。
求:该背包所能容纳的最大价值?
*************************************************/
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


/*************************************************
Function: GetMaxValue_MultiBag
Description: 得到多重背包所能容纳的最大价值
Input:  vec_vec_multigoods: 各种物品的 体积/质量、价值、数量
bag_capacity:背包容量
Return: 多重背包能够容纳的最大价值
*************************************************/
template<class T> size_t GetMaxValue_MultiBag(
	const vector<vector<T>> &vec_vec_multigoods, const size_t &bag_capacity)
{
	size_t max_val = 0;
	if (!vec_vec_multigoods.empty() && bag_capacity > 0)
	{
		vector<size_t> vec_dp(bag_capacity + 1, 0);

		const size_t goods_kinds = vec_vec_multigoods.size();
		for (size_t j = 0; j != goods_kinds; ++j)
		{
			vector<T> good_cur = vec_vec_multigoods[j];	// 存放当前物品的属性  体积/质量、价值、数量

			T cnt_threshold;	// 该物品在容量为 i 的背包里可带走的最大数量
			for (size_t i = bag_capacity; i >= good_cur[0]; --i)
			{
				cnt_threshold = min(T(i/good_cur[0]), good_cur[2]);
				for (size_t k = 1; k <= cnt_threshold; ++k)
					vec_dp[i] = max(vec_dp[i], k*good_cur[1] + vec_dp[i - k*good_cur[0]]);
			}
		}

		max_val = vec_dp.back();
	}

	return max_val;
}


int main(int argc, char* argv[])
{
	// 体积/质量、价值、数量
	vector<vector<int>> vec_vec_multigoods = { { 1, 6, 10 },{ 2, 10, 5 }, {2, 20, 2} };

	size_t max_val = GetMaxValue_MultiBag(vec_vec_multigoods, 8);
	cout << max_val << endl;

	system("pause");
	return 0;
}



9、



10、
















 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
30个算法程序整理,需要的同学们可以下载啦! 30个算法程序整理~川理-鹏\BP神经网络分类.pdf, 409828 , 2017-09-07 30个算法程序整理~川理-鹏\BP神经网络评价.pdf, 431981 , 2017-09-10 30个算法程序整理~川理-鹏\BP神经网络,预测.pdf, 156242 , 2017-09-10 30个算法程序整理~川理-鹏\GM(1,1)预测.pdf, 134683 , 2017-09-07 30个算法程序整理~川理-鹏\RBF神经网络.pdf, 616956 , 2017-09-10 30个算法程序整理~川理-鹏\Read Me.txt, 582 , 2017-09-12 30个算法程序整理~川理-鹏\三次指数平滑法.pdf, 93869 , 2017-09-10 30个算法程序整理~川理-鹏\人员疏散-元胞自动机.pdf, 278402 , 2017-09-10 30个算法程序整理~川理-鹏\偏最小二乘法.pdf, 624380 , 2017-09-08 30个算法程序整理~川理-鹏\关联分析+优势分析.pdf, 691257 , 2017-09-07 30个算法程序整理~川理-鹏\因子分析评价模型.p df, 302027 , 2017-09-07 30个算法程序整理~川理-鹏\城市规划-元胞自动机.pdf, 336276 , 2017-09-10 30个算法程序整理~川理-鹏\基于形态学的权重自适应图像去噪.pdf, 910283 , 2017-09-08 30个算法程序整理~川理-鹏\多单因素方差分析.pdf, 317006 , 2017-09-07 30个算法程序整理~川理-鹏\小波神经网络.pdf, 545875 , 2017-09-10 30个算法程序整理~川理-鹏\指派优化问题(0,1整数规划).pdf, 583624 , 2017-09-10 30个算法程序整理~川理-鹏\支持向量机.pdf, 770026 , 2017-09-07 30个算法程序整理~川理-鹏\最小生成树.pdf, 668256 , 2017-09-10 30个算法程序整理~川理-鹏\模拟退火最短路问题.pdf, 840666 , 2017-09-09 30个算法程序整理~川理-鹏\模糊聚类.pdf, 282470 , 2017-09-07 30个算法程序整理~川理-鹏\熵权法.pdf, 152382 , 2017-09-07 30个算法程序整理~川理-鹏\牛顿插值.pdf, 236217 , 2017-09-10 30个算法程序整理~川理-鹏\空间分布图.pdf, 212741 , 2017-09-08 30个算法程序整理~川理-鹏\简单图像处理.pdf, 414725 , 2017-09-08 30个算法程序整理~川理-鹏\粒子群算法-函数寻优.pdf, 664981 , 2017-09-09 30个算法程序整理~川理-鹏\蒙特卡罗非线性规划求解.pdf, 514421 , 2017-09-09 30个算法程序整理~川理-鹏\遗传算法.pdf, 613569 , 2017-09-09 30个算法程序整理~川理-鹏\遗传算法GA-最短路问题.pdf, 534278 , 2017-09-10 30个算法程序整理~川理-鹏\量子遗传算法.pdf, 343005 , 2017-09-10 30个算法程序整理~川理-鹏\非线性整数规划.pdf, 71362 , 2017-09-08 30个算法程序整理~川理-鹏\非线性最小二乘优化问题.pdf, 362554 , 2017-09-09 30个算法程序整理~川理-鹏, 0 , 2017-09-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值