程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 【C/C++學院】0907-象棋五子棋代碼分析/尋找算法以及排序算法

【C/C++學院】0907-象棋五子棋代碼分析/尋找算法以及排序算法

編輯:C++入門知識

【C/C++學院】0907-象棋五子棋代碼分析/尋找算法以及排序算法


象棋五子棋代碼分析

編譯代碼報錯:

錯誤	1	error MSB8031: Building an MFC project for a non-Unicode character set is deprecated. You must change the project property to Unicode or download an additional library. See http://go.microsoft.com/fwlink/p/?LinkId=286820 for more information.	C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets	369	5	chess

 

安裝vc_mbcsmfc.exe。

Pdb格式不兼容報錯:

\

\

\
 

\

\

 

尋找算法以及排序算法

 

插值查找.cpp

 

#define  _CRT_SECURE_NO_WARNINGS

#include  
#include 
int Bin_Search(int *a, int key, int n)
{
	int low, high, mid;
	low = 0;
	high = n - 1;
	while (low <= high)
	{
		mid = low + (high - low) * (key - a[low]) / (a[high] - a[low]); //此處於二分查找不同,套用插值公式  
		if (a[mid] > key)         //如果key比插值小,把高位調整為插值下標的下一位            
			high = mid - 1;
		else if (a[mid] < key)
			low = mid + 1;
		else
			return mid;
	}
	return -1;
}
int mainA()
{
	int a[] = { 1, 5, 17, 25, 33, 38, 46, 55, 69, 75, 99 };
	int key;
	int len = sizeof(a) / sizeof(*a);
	printf("請輸入要查找的值:\n");
	scanf("%d", &key);
	int pos = Bin_Search(a, key, len);
	if (pos != -1)
		printf("在數組的第%d個位置找到:%d\n", pos + 1, key);
	else
		printf("未在數組中找到元素:%d\n", key);


	system("pause");
	return 0;
}

 

斐波那契查找.cpp

#define  _CRT_SECURE_NO_WARNINGS
#define  MAXSIZE 13
#include 
#include 
//斐波那契查找算法的明顯優點在於它只涉及加法和減法運算,而不用除法。
//因為除法比加減法要占去更多的機時,因此,斐波那契查找的平均性能要比折半查找好。
void fibonacci(int *f)
{
	f[0] = 1;
	f[1] = 1;
	for (int i = 2; i < MAXSIZE; ++i)
		f[i] = f[i - 2] + f[i - 1];
}

int fibonacci_search(int *a, int key, int n)
{
	int low = 0, high = n - 1;
	int mid = 0;
	int k = 0;
	int F[MAXSIZE];
	fibonacci(F);
	while (n > F[k] - 1) //計算出n在斐波那契中的數列  
		++k;
	for (int i = n; i < F[k] - 1; ++i) //把數組補全  
	{
		a[i] = a[high];
	}
	while (low <= high)
	{
		mid = low + F[k - 1] - 1;  //根據斐波那契數列進行黃金分割  
		if (a[mid] > key)
		{
			high = mid - 1;
			k = k - 1;
		}
		else if (a[mid] < key)
		{
			low = mid + 1;
			k = k - 2;
		}
		else{
			if (mid <= high) //如果為真則找到相應的位置  
				return mid;
			else
				return -1;

		}
	}
	return -1;
}

int main14()
{

	int a[MAXSIZE] = { 5, 15, 19, 20, 25, 31, 38, 41, 45, 49, 52, 55, 57 };
	int k;
	printf("請輸入要查找的數字:\n");
	scanf("%d", &k);
	int pos = fibonacci_search(a, k, 13);
	if (pos != -1)
		printf("在數組的第%d個位置找到元素:%d\n", pos + 1, k);
	else
		printf("未在數組中找到元素:%d\n", k);


	system("pause");
	return 0;
}

插入.cpp

#include 
using namespace std;

void main3()
{

	int a[10];
	for (int i = 0; i < 10; i++)
		a[i] = rand() % 100;
	for (int i = 0; i < 10; i++)     //總索引
		for (int j = 0; j < i; j++)   //前面排好的部分
		{
		int temp = a[i];
		if (a[i] < a[j])
		{
			for (int k = i; k >= j; k--)
			{
				a[k] = a[k - 1];
			}
			a[j] = temp;
		}
		}
	for (int i = 0; i < 10; i++)
		cout << a[i] << " ";

	system("pause");
}

堆排序.cpp

#include 
#include 
#include 
#include 

void PrintArr(int *pnArr, int nLen)
{
	for (int i = 0; i < nLen; i++)
	{
		printf("%d ", pnArr[i]);
	}
	printf("\n");
}

//返回i父節點下標
int Parent(int i)
{
	return (i - 1) / 2;
}

//返回i左孩子下標
int LeftChild(int i)
{
	return i * 2 + 1;
}

//返回i右孩子下標
int RightChild(int i)
{
	return i * 2 + 2;
}

void Swap(int *a, int *b)
{
	int nTmp = *a;
	*a = *b;
	*b = nTmp;
}
void MaxHeapify(int *pnArr, int nLen, int i)
{
	int LChild = LeftChild(i);
	int RChild = RightChild(i);
	int nMaxPos;
	if (LChild < nLen && pnArr[LChild] > pnArr[i])
	{
		nMaxPos = LChild;
	}
	else
	{
		nMaxPos = i;
	}
	if (RChild < nLen && pnArr[RChild] > pnArr[nMaxPos])
	{
		nMaxPos = RChild;
	}

	if (nMaxPos != i)
	{
		Swap(&pnArr[nMaxPos], &pnArr[i]);
		MaxHeapify(pnArr, nLen, nMaxPos);
	}

}
void BuildMaxHeap(int *pnArr, int nLen)
{
	for (int i = Parent(nLen - 1); i >= 0; i--)
	{
		MaxHeapify(pnArr, nLen, i);
	}
}

void HeapSort(int *pnArr, int nLen)
{
	BuildMaxHeap(pnArr, nLen);
	for (int i = nLen - 1; i > 0; i--)
	{
		Swap(&pnArr[i], &pnArr[0]);
		nLen--;
		MaxHeapify(pnArr, nLen, 0);
	}
}
int main7()
{
	int nArr[10] = { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 };

	PrintArr(nArr, 10);
	HeapSort(nArr, 10);

	PrintArr(nArr, 10);
	system("pause");
	return 0;
}

二路插入.cpp

#include
using namespace std;
#define MAX 20
void PrintArray(int a[], int len){
	for (int i = 0; i < len; i++)
		cout << a[i] << " ";
	cout << endl;
}
void BinInsertSort(int a[], int len){
	int *d = (int *)malloc(len*sizeof(len));
	for (int i = 0; i < len; i++)
		d[i] = 0;
	int first = 0, final = 0;
	d[0] = a[0];
	for (int i = 1; i < len; i++){
		if (a[i] <= d[first]){
			first = (first - 1 + len) % len;
			d[first] = a[i];
		}
		else if (a[i] >= d[final]){
			final = final + 1;
			d[final] = a[i];
		}
		else{
			int j = final++;
			while (a[i] < d[j]){
				d[(j + 1) % len] = d[j];
				j = (j - 1 + len) % len;
			}
			d[j + 1] = a[i];
		}
	}
	cout << "輔助數組中排序結果為:";
	PrintArray(d, len);
}
int main10(){
	int a[MAX], len;
	cout << "請輸入待排序的元素個數:";
	cin >> len;
	cout << "請輸入待排序的元素:";
	for (int i = 0; i < len; i++)
		cin >> a[i];
	BinInsertSort(a, len);
	system("pause");
	return 0;
}

非遞歸快速排序.cpp

#include
#include
#include
#include
#include
using namespace std;

/**把數組分為兩部分,軸pivot左邊的部分都小於軸右邊的部分**/
template 
int partition(vector &vec, int low, int high){
	Comparable pivot = vec[low];  //任選元素作為軸,這裡選首元素
	while (low < high){
		while (low < high && vec[high] >= pivot)
			high--;
		vec[low] = vec[high];
		while (low < high && vec[low] <= pivot)
			low++;
		vec[high] = vec[low];
	}
	//此時low==high
	vec[low] = pivot;
	return low;
}

/**使用遞歸快速排序**/
template
void quicksort1(vector &vec, int low, int high){
	if (low < high){
		int mid = partition(vec, low, high);
		quicksort1(vec, low, mid - 1);
		quicksort1(vec, mid + 1, high);
	}
}

/**使用棧的非遞歸快速排序**/
template
void quicksort2(vector &vec, int low, int high){
	stack st;
	if (low < high){
		int mid = partition(vec, low, high);
		if (low < mid - 1){
			st.push(low);
			st.push(mid - 1);
		}
		if (mid + 1 < high){
			st.push(mid + 1);
			st.push(high);
		}
		//其實就是用棧保存每一個待排序子串的首尾元素下標,下一次while循環時取出這個范圍,對這段子序列進行partition操作
		while (!st.empty()){
			int q = st.top();
			st.pop();
			int p = st.top();
			st.pop();
			mid = partition(vec, p, q);
			if (p < mid - 1){
				st.push(p);
				st.push(mid - 1);
			}
			if (mid + 1 < q){
				st.push(mid + 1);
				st.push(q);
			}
		}
	}
}

int main11(){
	int len = 1000000;
	vector vec;
	for (int i = 0; i < len; i++)
		vec.push_back(rand());
	clock_t t1 = clock();
	quicksort1(vec, 0, len - 1);
	clock_t t2 = clock();
	cout << "recurcive  " << 1.0*(t2 - t1) / CLOCKS_PER_SEC << endl;

	//重新打亂順序
	random_shuffle(vec.begin(), vec.end());

	t1 = clock();
	quicksort2(vec, 0, len - 1);
	t2 = clock();
	cout << "none recurcive  " << 1.0*(t2 - t1) / CLOCKS_PER_SEC << endl;

	return 0;
}

歸並.cpp

#include 

using namespace std;

const int N = 10;
int anthor[N];
void MergeSort(int *array, int begin, int end)
{
	if (end - begin > 1)
	{		//
		MergeSort(array, begin, (begin + end) / 2);
		MergeSort(array, (begin + end) / 2 + 1, end);

		int i = begin;
		int j = (begin + end) / 2 + 1;
		int k = begin;

		while (i <= (begin + end) / 2 && j <= end)//合並時,把一個串全部並入另一個串放在一個新串,剩下的直接放在尾部
		{
			if (array[i] > array[j])        //小的值進入,並將索引後移
				anthor[k++] = array[j++];
			if (array[i] < array[j])
				anthor[k++] = array[i++];

		}
		while (i <= (begin + end) / 2)
		{
			anthor[k++] = array[i++];
		}
		while (j <= end)
		{
			anthor[k++] = array[j++];
		}

		for (k = begin; k <= end; k++)    //排序好重新拷貝回數組
			array[k] = anthor[k];

	}
	else      //相鄰則直接交換
	{
		if (array[end] < array[begin])
		{
			int temp = array[end];
			array[end] = array[begin];
			array[begin] = temp;
		}
	}
}


int main6()
{
	int array[N];
	for (int i = 0; i < 10; i++)
	{
		array[i] = rand() % 100;
		cout << array[i] << " ";
	}

	MergeSort(array, 0, N - 1);
	cout << endl;
	for (int i = 0; i < 10; i++)
	{

		cout << array[i] << " ";
	}
	system("pause");
	return 0;
}

紅黑樹.cpp

// 紅黑樹.cpp : 定義控制台應用程序的入口點。
#include 
#include 
#include 

typedef int key_t;
typedef int data_t;

typedef enum color_t
{
	RED = 0,
	BLACK = 1
}color_t;

typedef struct rb_node_t
{
	struct rb_node_t *left, *right, *parent;
	key_t key;
	data_t data;
	color_t color;
}rb_node_t;

/* forward declaration */
rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root);
rb_node_t* rb_search(key_t key, rb_node_t* root);
rb_node_t* rb_erase(key_t key, rb_node_t* root);

int main()
{
	int i, count = 90;
	key_t key;
	rb_node_t* root = NULL, *node = NULL;

	//srand(time(NULL));
	for (i = 1; i < count; ++i)
	{
		key = rand() % count;
		if ((root = rb_insert(key, i, root)))
		{
			printf("[i = %d] insert key %d success!\n", i, key);
		}
		else
		{
			printf("[i = %d] insert key %d error!\n", i, key);
			exit(-1);
		}

		if ((node = rb_search(key, root)))
		{
			printf("[i = %d] search key %d success!\n", i, key);
		}
		else
		{
			printf("[i = %d] search key %d error!\n", i, key);
			exit(-1);
		}
		if (!(i % 10))
		{
			if ((root = rb_erase(key, root)))
			{
				printf("[i = %d] erase key %d success\n", i, key);
			}
			else
			{
				printf("[i = %d] erase key %d error\n", i, key);
			}
		}
	}
	return 0;
}

static rb_node_t* rb_new_node(key_t key, data_t data)
{
	rb_node_t *node = (rb_node_t*)malloc(sizeof(struct rb_node_t));

	if (!node)
	{
		printf("malloc error!\n");
		exit(-1);
	}
	node->key = key, node->data = data;

	return node;
}

/*-----------------------------------------------------------
|   node           right
|   / \    ==>     / \
|   a  right     node  y
|       / \           / \
|       b  y         a   b
-----------------------------------------------------------*/
static rb_node_t* rb_rotate_left(rb_node_t* node, rb_node_t* root)
{
	rb_node_t* right = node->right;

	if ((node->right = right->left))
	{
		right->left->parent = node;
	}
	right->left = node;

	if ((right->parent = node->parent))
	{
		if (node == node->parent->right)
		{
			node->parent->right = right;
		}
		else
		{
			node->parent->left = right;
		}
	}
	else
	{
		root = right;
	}
	node->parent = right;

	return root;
}

/*-----------------------------------------------------------
|       node           left
|       / \            / \
|    left  y   ==>    a   node
|   / \               / \
|  a   b             b   y
-----------------------------------------------------------*/
static rb_node_t* rb_rotate_right(rb_node_t* node, rb_node_t* root)
{
	rb_node_t* left = node->left;

	if ((node->left = left->right))
	{
		left->right->parent = node;
	}
	left->right = node;

	if ((left->parent = node->parent))
	{
		if (node == node->parent->right)
		{
			node->parent->right = left;
		}
		else
		{
			node->parent->left = left;
		}
	}
	else
	{
		root = left;
	}
	node->parent = left;

	return root;
}

static rb_node_t* rb_insert_rebalance(rb_node_t *node, rb_node_t *root)
{
	rb_node_t *parent, *gparent, *uncle, *tmp;

	while ((parent = node->parent) && parent->color == RED)
	{
		gparent = parent->parent;

		if (parent == gparent->left)
		{
			uncle = gparent->right;
			if (uncle && uncle->color == RED)
			{
				uncle->color = BLACK;
				parent->color = BLACK;
				gparent->color = RED;
				node = gparent;
			}
			else
			{
				if (parent->right == node)
				{
					root = rb_rotate_left(parent, root);
					tmp = parent;
					parent = node;
					node = tmp;
				}

				parent->color = BLACK;
				gparent->color = RED;
				root = rb_rotate_right(gparent, root);
			}
		}
		else
		{
			uncle = gparent->left;
			if (uncle && uncle->color == RED)
			{
				uncle->color = BLACK;
				parent->color = BLACK;
				gparent->color = RED;
				node = gparent;
			}
			else
			{
				if (parent->left == node)
				{
					root = rb_rotate_right(parent, root);
					tmp = parent;
					parent = node;
					node = tmp;
				}

				parent->color = BLACK;
				gparent->color = RED;
				root = rb_rotate_left(gparent, root);
			}
		}
	}

	root->color = BLACK;

	return root;
}

static rb_node_t* rb_erase_rebalance(rb_node_t *node, rb_node_t *parent, rb_node_t *root)
{
	rb_node_t *other, *o_left, *o_right;

	while ((!node || node->color == BLACK) && node != root)
	{
		if (parent->left == node)
		{
			other = parent->right;
			if (other->color == RED)
			{
				other->color = BLACK;
				parent->color = RED;
				root = rb_rotate_left(parent, root);
				other = parent->right;
			}
			if ((!other->left || other->left->color == BLACK) &&
				(!other->right || other->right->color == BLACK))
			{
				other->color = RED;
				node = parent;
				parent = node->parent;
			}
			else
			{
				if (!other->right || other->right->color == BLACK)
				{
					if ((o_left = other->left))
					{
						o_left->color = BLACK;
					}
					other->color = RED;
					root = rb_rotate_right(other, root);
					other = parent->right;
				}
				other->color = parent->color;
				parent->color = BLACK;
				if (other->right)
				{
					other->right->color = BLACK;
				}
				root = rb_rotate_left(parent, root);
				node = root;
				break;
			}
		}
		else
		{
			other = parent->left;
			if (other->color == RED)
			{
				other->color = BLACK;
				parent->color = RED;
				root = rb_rotate_right(parent, root);
				other = parent->left;
			}
			if ((!other->left || other->left->color == BLACK) &&
				(!other->right || other->right->color == BLACK))
			{
				other->color = RED;
				node = parent;
				parent = node->parent;
			}
			else
			{
				if (!other->left || other->left->color == BLACK)
				{
					if ((o_right = other->right))
					{
						o_right->color = BLACK;
					}
					other->color = RED;
					root = rb_rotate_left(other, root);
					other = parent->left;
				}
				other->color = parent->color;
				parent->color = BLACK;
				if (other->left)
				{
					other->left->color = BLACK;
				}
				root = rb_rotate_right(parent, root);
				node = root;
				break;
			}
		}
	}

	if (node)
	{
		node->color = BLACK;
	}

	return root;
}

static rb_node_t* rb_search_auxiliary(key_t key, rb_node_t* root, rb_node_t** save)
{
	rb_node_t *node = root, *parent = NULL;
	int ret;

	while (node)
	{
		parent = node;
		ret = node->key - key;
		if (0 < ret)
		{
			node = node->left;
		}
		else if (0 > ret)
		{
			node = node->right;
		}
		else
		{
			return node;
		}
	}

	if (save)
	{
		*save = parent;
	}

	return NULL;
}

rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root)
{
	rb_node_t *parent = NULL, *node;

	parent = NULL;
	if ((node = rb_search_auxiliary(key, root, &parent)))
	{
		return root;
	}

	node = rb_new_node(key, data);
	node->parent = parent;
	node->left = node->right = NULL;
	node->color = RED;

	if (parent)
	{
		if (parent->key > key)
		{
			parent->left = node;
		}
		else
		{
			parent->right = node;
		}
	}
	else
	{
		root = node;
	}

	return rb_insert_rebalance(node, root);
}

rb_node_t* rb_search(key_t key, rb_node_t* root)
{
	return rb_search_auxiliary(key, root, NULL);
}

rb_node_t* rb_erase(key_t key, rb_node_t *root)
{
	rb_node_t *child, *parent, *old, *left, *node;
	color_t color;

	if (!(node = rb_search_auxiliary(key, root, NULL)))
	{
		printf("key %d is not exist!\n");
		return root;
	}

	old = node;

	if (node->left && node->right)
	{
		node = node->right;
		while ((left = node->left) != NULL)
		{
			node = left;
		}
		child = node->right;
		parent = node->parent;
		color = node->color;

		if (child)
		{
			child->parent = parent;
		}
		if (parent)
		{
			if (parent->left == node)
			{
				parent->left = child;
			}
			else
			{
				parent->right = child;
			}
		}
		else
		{
			root = child;
		}

		if (node->parent == old)
		{
			parent = node;
		}

		node->parent = old->parent;
		node->color = old->color;
		node->right = old->right;
		node->left = old->left;

		if (old->parent)
		{
			if (old->parent->left == old)
			{
				old->parent->left = node;
			}
			else
			{
				old->parent->right = node;
			}
		}
		else
		{
			root = node;
		}

		old->left->parent = node;
		if (old->right)
		{
			old->right->parent = node;
		}
	}
	else
	{
		if (!node->left)
		{
			child = node->right;
		}
		else if (!node->right)
		{
			child = node->left;
		}
		parent = node->parent;
		color = node->color;

		if (child)
		{
			child->parent = parent;
		}
		if (parent)
		{
			if (parent->left == node)
			{
				parent->left = child;
			}
			else
			{
				parent->right = child;
			}
		}
		else
		{
			root = child;
		}
	}

	free(old);

	if (color == BLACK)
	{
		root = rb_erase_rebalance(child, parent, root);
	}
	system("pause");
	return root;
}

基數.cpp

#include 
#include 
#include 
#include 

static void PrintArr(int *pnArr, int nLen)
{
	for (int i = 0; i < nLen; i++)
	{
		printf("%d ", pnArr[i]);
	}
	printf("\n");
}

void CountSort(int *pnArr, int nArrR[], int nLen, int k)
{
	int *pnArrTmp = (int *)malloc(sizeof(int) * k);

	for (int i = 0; i < k; i++)
	{
		pnArrTmp[i] = 0;
	}

	for (int i = 0; i < nLen; i++)
	{
		pnArrTmp[pnArr[i]] = pnArrTmp[pnArr[i]] + 1;
	}

	PrintArr(pnArrTmp, k);

	for (int i = 1; i < k; i++)
	{
		pnArrTmp[i] = pnArrTmp[i] + pnArrTmp[i - 1];
	}

	PrintArr(pnArrTmp, k);

	
	for (int i = nLen - 1; i >= 0; i--)
	{
		nArrR[pnArrTmp[pnArr[i]] - 1] = pnArr[i];
		pnArrTmp[pnArr[i]] = pnArrTmp[pnArr[i]] - 1;
	}
}

int main9()
{
	int nArr[11] = { 0, 2, 1, 3, 2, 6, 9, 7, 4, 8, 6 };
	int nArrR[11]; //存放排序後的結果
	PrintArr(nArr, 11);
	CountSort(nArr, nArrR, 11, 10);

	PrintArr(nArrR, 11);
	system("pause");
	return 0;
}

快速.cpp

#include  
using namespace std;
int a[200001], n;
void swap(int &a, int &b){
	int tmp = a;
	a = b;
	b = tmp;
}

int partition(int p, int r){
	int rnd = rand() % (r - p + 1) + p;
	swap(a[rnd], a[r]);
	int pvt = r, i = p - 1;
	for (int j = i + 1; j < r; j++)
		if (a[j] < a[pvt])
			swap(a[j], a[++i]);
	swap(a[++i], a[pvt]);
	return i;
}

void qsort(int p, int r){
	if (p < r){
		int q = partition(p, r);
		qsort(p, q - 1);
		qsort(q + 1, r);
	}
}
int main5()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	qsort(0, n - 1);
	for (int i = 0; i < n; i++)
		cout << a[i];
	system("pause");
	return 0;
}

冒泡.cpp

#include 
#include
using namespace std;

int main1()
{
	int a[10];
	for (int i = 0; i < 10; i++)
		a[i] = rand() % 100;
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10 - i; j++)
		{
		if (a[j] < a[j + 1])
		{
			int temp = a[j];
			a[j] = a[j + 1];
			a[j + 1] = temp;
		}
		}
	for (int i = 0; i < 10; i++)
		cout << a[i] << " ";
	system("pause");

	return 0;
}

木桶.cpp

#include
#include
#define SIZE 100
void bucket_sort(unsigned *, int);//桶排序函數的原型
void print(unsigned *, int);//打印函數的原型
int main8()
{
	unsigned array[SIZE];
	int i = 0;

	//為數組元素隨機賦值
	for (i = 0; i < SIZE; ++i)
		array[i] = rand();

	printf("排序前\n");
	print(array, SIZE);

	//排序
	bucket_sort(array, SIZE);

	printf("排序後\n");
	print(array, SIZE);
	system("pause");
	return 0;
}
void bucket_sort(unsigned * arr, int len)
{
	unsigned *buckets[10];//指針數組
	unsigned n = 1;//用於取整數各位上的值
	int index;//數組下標計數索引
	int indexs[10];//各個桶下標計數索引
	int i, j;

	//分配動態內存作為桶
	for (i = 0; i < 10; ++i)
		buckets[i] = (unsigned *)malloc(sizeof(unsigned)*len);

	while (1)
	{
		//計數索引清零
		index = 0;
		for (i = 0; i < 10; ++i)
			indexs[i] = 0;

		//數組至桶
		for (i = 0; i < len; ++i)
			buckets[arr[i] / n % 10][indexs[arr[i] / n % 10]++] = arr[i];

		//桶至數組
		for (i = 0; i < 10; ++i)
			for (j = 0; j < indexs[i]; ++j)
				arr[index++] = buckets[i][j];

		//為取元素的下一位做准備
		n *= 10;

		//判斷是否該結束
		for (i = 0; arr[i] < n&&i < len; ++i);
		if (i == len) break;
	}

	//釋放動態內存
	for (i = 0; i < 10; ++i)
		free(buckets[i]);
}
void print(unsigned * arr, int len)
{
	int i = 0;
	for (i = 0; i < len; ++i)
	{
		printf("%8d", arr[i]);

		//5個元素一行
		if ((i + 1) % 5 == 0)
			printf("\n");
	}
}

希爾.cpp

#include 
#include

using namespace std;
const int N = 10;
void shell_sort(const int len, int *array)
{
	int j, i, key;
	int gap = 0;
	if (len <= 0 || array == NULL)
		return;
	while (gap <= len)
	{
		gap = gap * 3 + 1;
	}
	while (gap > 0)
	{
		for (i = gap; i < len; i++)
		{
			j = i - gap;
			key = array[i];
			while ((j >= 0) && (array[j] > key))
			{
				array[j + gap] = array[j];
				j = j - gap;
			}
			array[j + gap] = key;
		}
		//display_array(len,array,gap);
		gap = (gap - 1) / 3;
	}
}

// 3  5  18   29     35   24   12  0
// 3                           12   
//     5                            0
// 3   0  18  29    35    24  12    5
//3                       24
//    0                       12
//        18                        5
//3   0   5  29     35    24  12    18
//3                  35
//   0                   24
//       5                   12
//           29                     18
//3  0   5   18   35   24   12     29   
//3          18             12
//   0            35               29
//        5            24
//3  0    5  12   29   24      18   35
//3       5       29            18
//   0        12        24          35
//3   0   5   12     18    24        29 35
//0    3  5 12    18  24   29  35

int main4()
{
	int array[N];
	for (int i = 0; i < 10; i++)
	{
		array[i] = rand() % 100;
		cout << array[i] << " ";
	}
	shell_sort(N - 1, array);
	cout << endl;
	for (int i = 0; i < 10; i++)
	{

		cout << array[i] << " ";
	}
	system("pause");
	return 0;
}

選擇.cpp

#include 
using namespace std;

void main2()
{
	int a[10];
	for (int i = 0; i < 10; i++)
		a[i] = rand() % 100;

	for (int i = 0; i < 10; i++)
	{

		for (int j = i; j < 10 - i; j++)
			if (a[j] < a[i])
			{
			int temp = a[j];
			a[j] = a[i];
			a[i] = temp;
			}
	}
	
	for (int i = 0; i < 10; i++)
		cout << a[i] << " ";
	system("pause");
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved