C++算法学习记录

1、使用C++ 基于STL库进行箱排序

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

void BucketSort(float arr[], int n) {
    
    
	vector<float>* bucket = new vector<float>[n];

	for (int i = 0; i < n; i++) {
    
    
		int key = arr[i] * n;
		bucket[key].push_back(arr[i]);
	}

	for (int i = 0; i < n; i++) {
    
    
		sort((bucket[i].begin()), (bucket[i].end()));
	}

	int index = 0;
	for (int i = 0; i < n; i++) {
    
    
		for (int j = 0; j < bucket[i].size(); j++) {
    
    
			arr[index++] = bucket[i][j];
		}
	}
};


int main() {
    
    
	float arr[10] = {
    
     0.33,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1 };
	BucketSort(arr, 10);
	for (int i = 0; i < 10; i++) {
    
    
		cout << arr[i] << "\t";
	}
	return 0;
}

2、C++泛型编程实现广义表

#include <iostream>
#include <string>
using namespace std;

typedef enum {
    
     atom, list }NodeTag;
template <typename DataType>
class GLIST {
    
    
private:
	typedef struct GLNode{
    
    
		NodeTag tag;
		union {
    
    
			DataType data;
			GLNode* slink;
		};
		GLNode* link;
	}*Glist;

public:
	Glist _GL;
	Glist p;

public:
	//建立广义表的存储结构
	Glist CreateGList(Glist GL) {
    
    
		char ch;
		ch = getchar();
		if (ch != ' ') {
    
    
			GL = (GLNode *)malloc(sizeof(GLNode));
			if (ch == '(') {
    
    
				GL->tag = list;
				GL->slink = CreateGList(GL->slink);
			}
			else {
    
    
				GL->tag = atom;
				GL->data = ch;
			}
		}
		else GL = NULL;
		ch = getchar();
		if (GL != NULL) {
    
    
			if (ch == ',')
				GL->link = CreateGList(GL->link);
			else
				GL->link = NULL;
			return GL;
		}		
	}

	void PrintGlist(Glist GL = this.GL) {
    
    
		if (GL != NULL) {
    
    
			if (GL->tag == list) {
    
    
				cout << "(";
				if (GL -> slink == NULL)cout << " ";
				else PrintGlist(GL->slink);
			}
			else {
    
    
				cout << GL->data;
			}
			if (GL->tag == list)cout << ")";
			if (GL->link != NULL) {
    
    
				cout << ",";
				PrintGlist(GL->link);
			}
		}
	}

	void FindGlistX(GList GL,DataType x,int * mark) {
    
    
		if (GL != NULL) {
    
    
			if (GL->tag == 0 && GL->data == x) {
    
    
				p = GL;
				*mark = 1;
			}
			else {
    
    
				if (GL->tag == 1)FindGlistX(GL->slink, x, mark);
				FindGlistX(gl->link, x, mark);
			}
		}
	}

	Glist tail() {
    
    
		Glist p;
		if (GL != NULL && GL->tag != 0) {
    
    
			p = GL->slink;
			p->link = NULL;
			return p;
		}
		else return NULL;
	}

	void depth(Glist GL, int * maxdh) {
    
    
		int h;
		if (GL->tag == 0)*maxdh = 0;
		else {
    
    
			if (GL->tag == 1 && GL->slink == NULL) {
    
    
				*maxdh = 1;
			}
			else {
    
    
				GL = GL->slink;
				*maxdh = 0;
				do {
    
    
					depth(GL, &h);
					if (h > *maxdh) {
    
    
						maxdh = h;
					}
				} while (GL != NULL);
				(*maxdh)++;
			}
		}
	}
};

int main()
{
    
    
	GLIST<char>* gl = new GLIST<char>();
	gl->CreateGList(gl->_GL);
	gl->PrintGlist(gl->_GL);
	return 0;
}

3、使用C++实现散列函数算法

#include <iostream>
#include <cmath>
using namespace std;
class Hex_Function
{
    
    
	private:
		long a;
		long b;
	public:
		Hex_Function(int value1, int value2) {
    
    
			a = value1;
			b = value2;
		}

		long Create_Hyperbola_function(long value)
		{
    
    
			return sqrt(((pow(b, 2)*pow(value, 2)) / pow(a, 2)) - pow(b, 2));
		}

		long Get_Ticking_function(long value)
		{
    
    
			return (value*a) + (b / value);
		}
};

int main()
{
    
    
	Hex_Function* one = new Hex_Function(1,65535);
	cout <<"散列值:"<< one->Get_Ticking_function(one->Create_Hyperbola_function(43));
	return 0;
}

3、使用C++实现二叉排序树

#include <iostream>
using namespace std;

int arr[10] = {
    
     11,20,39,48,9,7,5,49,1,44 };

struct BinTree {
    
    
	int key;
	BinTree* leftNode;
	BinTree* rightNode;
};


BinTree* BSTree = NULL;
BinTree* InsertBST(BinTree* T,BinTree* S) {
    
    
	BinTree* f = NULL, *p = T;
	while (p) {
    
    
		f = p;
		if (S->key < p->key)p = p->leftNode;
		else p = p->rightNode;
	}
	if (T == NULL)T = S;
	else if (S->key < f->key)
		f->leftNode = S;
	else f->rightNode = S;
	return T;
}
void CreateBST() {
    
    
	for (int i = 0; i < 10; i++) {
    
    
		BinTree* S = (BinTree*)malloc(sizeof(BinTree));
		S->key = arr[i];
		S->leftNode = S->rightNode = NULL;
		BSTree = InsertBST(BSTree, S);
	}
}

BinTree* SearchBST(BinTree* T,int data) {
    
    
	if (T == NULL || T->key == data) {
    
     return T; }
	else if (data < T->key) {
    
     SearchBST(T->leftNode, data); }
	else SearchBST(T->rightNode, data);
}
void Mid_Print(BinTree* Head) {
    
    
	if (Head->leftNode != NULL)Mid_Print(Head->leftNode);
	if (Head!=NULL)printf("%d\t", Head->key);
	if (Head->rightNode != NULL)Mid_Print(Head->rightNode);
}
int main()
{
    
    
	BinTree** one = &BSTree;
	CreateBST();
	Mid_Print(*one);
	BinTree* two = SearchBST(*one, 1);
	printf("%d", two->key);
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_27180763/article/details/123681879