代码改变世界

第二章 算法基础

2017-09-07 21:28  szn好色仙人  阅读(220)  评论(0编辑  收藏  举报
1.循环不变式,需要被证明的3条性质:
A:初始化:循环的第一次迭代之前,它为真
B:保持:如果循环的某次迭代之前为真,下次迭代前仍为真
C:终止:在循环终止时,循环不变式为我们提供一个性质,该性质有助于证明算法是正确的


2.
A:一般衡量一个算法优劣,往往集中于只求最坏情况运行时间
B:对于算法的运行时间的函数,我们一般只关心最重要的项,这样可以得到简单的抽象


3.
A:分治法思想:将原问题分解为几个规模较小的但是类似于原问题的子问题,递归的求解这些子问题,然后再合并这些子问题的解


4.插入、合并、冒泡、stl::sort、qsort、误打误撞写出来的插入-冒泡法
#include "stdafx.h"
#include <Windows.h>
#include <random>
#include <algorithm>
#include <ctime>


using std::default_random_engine;
using std::uniform_int_distribution;
using std::sort;


//合并算法
void Merge(int* pValue, int nPos0, int nPos1, int nPos2, int* pNewValue[2])
{
	int aCount[2] = {nPos1 - nPos0 + 1, nPos2 - nPos1};
	memcpy(pNewValue[0], pValue + nPos0, sizeof(int) * aCount[0]);
	memcpy(pNewValue[1], pValue + nPos1 + 1, sizeof(int) * aCount[1]);

	int aIndex[2] = {0, 0};
	int nTemIndex = 0;

	for (int i = nPos0; i <= nPos2; ++i)
	{
		if (aIndex[0] >= aCount[0])
		{
			nTemIndex = 1;
		}
		else if (aIndex[1] >= aCount[1])
		{
			nTemIndex = 0;
		}
		else
		{
			if (pNewValue[0][aIndex[0]] > pNewValue[1][aIndex[1]])
			{
				nTemIndex = 1;
			}
			else
			{
				nTemIndex = 0;
			}
		}
	
		pValue[i] = pNewValue[nTemIndex][aIndex[nTemIndex]];
		++aIndex[nTemIndex];
	}
}
void MergeSort(int* pValue, const int CIndex0, const int CIndex1, int* pNewValue[2])
{
	if (CIndex0 < CIndex1)
	{
		int nCount = (CIndex1 + CIndex0) / 2;
		MergeSort(pValue, CIndex0, nCount, pNewValue);
		MergeSort(pValue, nCount + 1, CIndex1, pNewValue);

		Merge(pValue, CIndex0, nCount, CIndex1, pNewValue);
	}
}
void MergeSort(int* pValue, const int CLen, int* pNewValue[2])
{
	int nLen = CLen / 2;

	if (nLen)
	{
		MergeSort(pValue, nLen, pNewValue);
		MergeSort(pValue + nLen, CLen - nLen, pNewValue);

		Merge(pValue, 0, nLen - 1, CLen - 1, pNewValue);
	}
}


//插入算法
void InsertSort(int* pValue, const int CLen)
{
	int nValue = 0;
	int nIndex = 0;
	for (int i = 1; i < CLen; ++i)
	{
		nValue = pValue[i];
		nIndex = i - 1;
		while(nIndex >= 0 && pValue[nIndex] > nValue)
		{
			pValue[nIndex + 1] = pValue[nIndex];
			--nIndex;
		}
		pValue[nIndex + 1] = nValue;
	}
}


//冒泡法
void BubbleSort(int* pValue, const int CLen)
{
	int nTem = 0;
	for (int i = 0; i < CLen; ++i)
	{
		for (int j = i + 1; j < CLen; ++j)
		{
			if (pValue[i] > pValue[j])
			{
				nTem = pValue[j];
				pValue[j] = pValue[i];
				pValue[i] = nTem;
			}
		}
	}
}


//误打误撞写的算法, 命名为 插入-冒泡 法
void MyInsertAndBubbleSort(int* pValue, const int CLen)
{
	int nTem = 0;
	int nIndex = 0;
	for (int i = 0; i < CLen; ++i)
	{
		nIndex = i;

		while(nIndex > 0 && pValue[nIndex] < pValue[nIndex - 1])
		{
			nTem = pValue[nIndex];
			pValue[nIndex] = pValue[nIndex - 1];
			pValue[nIndex - 1] = nTem;

			--nIndex;
		}
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	const int CSize = 1024 * 10;
	int* pValue[3] = {new int[CSize], new int[CSize], new int[CSize]};
	int* pNewValue[2] = {new int[CSize], new int[CSize]};

	default_random_engine deEngine;
	uniform_int_distribution<int> disInt;

	for (int i = 0; i < CSize; ++i)
	{
		int nValue = disInt(deEngine);
		pValue[0][i] = nValue;
		pValue[1][i] = nValue;
		pValue[2][i] = nValue;
	}

	clock_t nBegin = clock();
	nBegin = clock();
	sort(pValue[0], pValue[0] + CSize);
	clock_t nStlSort = clock() - nBegin;
	/*
	CSize = 1024 * 10;				nStlSort = 11
	CSize = 1024 * 100;				nStlSort = 143
	CSize = 1024 * 1024;			nStlSort = 1627
	CSize = 1024 * 1024 * 10;		nStlSort = 19240
	*/

	nBegin = clock();
	qsort(pValue[1], CSize, sizeof(int), QSortCmp);
	clock_t nQSort = clock() - nBegin;
	memcpy(pValue[1], pValue[2], CSize * sizeof(int));
	/*
	CSize = 1024 * 10;				nQSort = 4
	CSize = 1024 * 100;				nQSort = 55
	CSize = 1024 * 1024;			nQSort = 661
	CSize = 1024 * 1024 * 10;		nQSort = 7691
	*/

	nBegin = clock();
	MergeSort(pValue[1], CSize, pNewValue);
	clock_t nMergeSort0 = clock() - nBegin;
	memcpy(pValue[1], pValue[2], CSize * sizeof(int));
	/*
	CSize = 1024 * 10;				nMergeSort0 = 2
	CSize = 1024 * 100;				nMergeSort0 = 26
	CSize = 1024 * 1024;			nMergeSort0 = 299
	CSize = 1024 * 1024 * 10;		nMergeSort0 = 3303
	*/

	nBegin = clock();
	MergeSort(pValue[1], 0, CSize - 1, pNewValue);
	clock_t nMergeSort1 = clock() - nBegin;
	memcpy(pValue[1], pValue[2], CSize * sizeof(int));
	/*
	CSize = 1024 * 10;				nMergeSort1 = 2
	CSize = 1024 * 100;				nMergeSort1 = 25
	CSize = 1024 * 1024;			nMergeSort1 = 291
	CSize = 1024 * 1024 * 10;		nMergeSort1 = 3294
	*/

	nBegin = clock();
	InsertSort(pValue[1], CSize);
	clock_t nInsertSort = clock() - nBegin;
	memcpy(pValue[1], pValue[2], CSize * sizeof(int));
	/*
	CSize = 1024 * 10;				nInsertSort = 63
	CSize = 1024 * 100;				nInsertSort = 6098
	CSize = 1024 * 1024;			等了几分钟也没结束,就不等了
	*/

	nBegin = clock();
	MyInsertAndBubbleSort(pValue[1], CSize);
	clock_t nMyInsertAndBubbleSort = clock() - nBegin;
	memcpy(pValue[1], pValue[2], CSize * sizeof(int));
	/*
	CSize = 1024 * 10;				nMyInsertAndBubbleSort = 94
	CSize = 1024 * 100;				nMyInsertAndBubbleSort = 9398
	CSize = 1024 * 1024;			不等了
	*/

	nBegin = clock();
	BubbleSort(pValue[1], CSize);
	clock_t nBubbleSort = clock() - nBegin;
	memcpy(pValue[1], pValue[2], CSize * sizeof(int));
	/*
	CSize = 1024 * 10;				nBubbleSort = 229
	CSize = 1024 * 100;				nBubbleSort = 26677
	CSize = 1024 * 1024;			不等了
	*/

	return 0;
}