几种常见算法
几种常见算法
悠米海 发表于3年前
几种常见算法
  • 发表于 3年前
  • 阅读 196
  • 收藏 20
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

摘要: 冒泡 选择 插入 快速 归并 最大堆 最小堆 二叉树 排序算法

#include <iostream>
using namespace std;

void paopao_sort(int arr[], int nSize)
{
	for(int i=0;i<nSize;i++)
	{
		for(int j=0;j<nSize-1-i;j++)
		{
			if(arr[j] > arr[j+1])
			{
				int nTmp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = nTmp;
			}
		}
	}
}

void select_sort(int arr[], int nSize)
{
	for(int i=0;i<nSize;i++)
	{
		for(int j=i;j<nSize;j++)
		{
			if(arr[i] > arr[j])
			{
				int nTmp = arr[i];
				arr[i] = arr[j];
				arr[j] = nTmp;
			}
		}
	}
}

void insert_sort(int arr[], int nSize)
{
	for(int i=0;i< nSize;i++)
	{
		int nTmp = arr[i];
		int j = i;
		for(;j>0 && arr[j-1] > nTmp;j--)
		{
			arr[j] = arr[j-1];
		}
		arr[j] = nTmp;
	}
}

void quick_sort(int arr[], int low,int high)
{
	if(low >= high)
		return;
	int first = low,last = high;
	int key = arr[first];
	while(first < last){
		while(first < last && arr[last] >= key){
			--last;
		}
		arr[first] = arr[last];
		while(first < last && arr[first] <= key){
			++first;
		}
		arr[last] = arr[first];
	}
	arr[first] = key;
	quick_sort(arr, low, first -1);
	quick_sort(arr, first+1, high);
}

void merge(int sour[], int tmp[], int st, int md, int ed)
{
	if(st >= ed)
		return;
	int i=st,j=md+1,k=st;
	while(i!=md+1 && j!=ed+1){
		if(sour[i] > sour[j]){
			tmp[k++] = sour[j++];
		}else{
			tmp[k++] = sour[i++];
		}
	}
	while(i!=md+1){
		tmp[k++] = sour[i++];
	}
	while(j!=ed+1){
		tmp[k++] = sour[j++];
	}
	for(i=st;i<=ed;i++){
		sour[i] = tmp[i];
	}
}

void merge_sort(int sour[],int tmp[],int st, int ed){
	if(st >= ed)
		return;
	int md = (st + ed)/2;
	merge_sort(sour,tmp,st,md);
	merge_sort(sour,tmp,md+1,ed);
	merge(sour,tmp,st,md,ed);
}

void heapAdjust(int arr[],int i,int nLen){
	int nChild, nTmp;
	for(;i*2+1 < nLen;i = nChild){
		nChild = i*2+1;
		if(nChild < nLen - 1 && arr[nChild] < arr[nChild +1]){
			nChild ++;
		}
		if(arr[nChild] > arr[i]){
			nTmp = arr[nChild];
			arr[nChild] = arr[i];
			arr[i] = nTmp;
		}else{
			break;
		}
	}
}

void heap_sort(int arr[], int nLen)
{
	for(int i=nLen/2-1;i>=0;i--){
		heapAdjust(arr,i,nLen);
	}
	int nTmp = 0;
	for(int i=nLen-1;i>0;i--)
	{
		nTmp = arr[i];
		arr[i] = arr[0];
		arr[0] = nTmp;
		heapAdjust(arr,0,i);
	}
}

void heapAdjustS(int arr[], int i, int nLen)
{
	int nChild, nTmp;
	for(;i*2+1 < nLen;i = nChild){
		nChild = i*2+1;
		if(nChild < nLen -1 && arr[nChild] > arr[nChild+1] ){
			nChild ++;
		}
		if(arr[i] > arr[nChild]){
			nTmp = arr[nChild];
			arr[nChild] = arr[i];
			arr[i] = nTmp;
		}else{
			break;
		}
	}
}

void heaps_sort(int arr[], int nLen)
{
	for(int i=nLen/2-1;i>=0;i--){
		heapAdjustS(arr,i,nLen);
	}
	int temp;
	for(int i=nLen-1;i>0;i--)
	{
		temp=arr[i];
		arr[i]=arr[0];
		arr[0]=temp;
		heapAdjustS(arr,0,i);
	}
}

struct tree{
	int		node;
	tree*	left;
	tree*	right;
};

tree* treeAdjust(tree*& root, int key)
{
	if(root == NULL){
		root = new tree;
		root->node = key;
		root->left = root->right = NULL;
		return root;
	}
	if(root->node == key){
		return root;
	}else if(root->node > key){
		return treeAdjust(root->left, key);
	}else{
		return treeAdjust(root->right, key);
	}
}

void treeDel(tree* root, int arr[], int& index)
{
	if(root == NULL)
		return;
	if(root->left != NULL){
		treeDel(root->left, arr, index);
	}
	arr[index++] = root->node;
	if(root->right != NULL){
		treeDel(root->right, arr, index);
	}
	delete root;
}

void tree_sort(int arr[], int nLen)
{
	tree* root = NULL;
	for(int i=0;i<nLen;i++)
	{
		treeAdjust(root,arr[i]);
	}
	int nIndex = 0;
	treeDel(root, arr, nIndex);
}


void arrCopy(int base[], int to[], int nLen)
{
	for(int i=0;i<nLen;i++)
		to[i] = base[i];
}

void arrShow(int arr[], int nLen)
{
	for(int i=0;i<nLen;i++)
		cout<<arr[i]<<",";
	cout<<endl;
}

int main()
{
	int base[] = {3,6,4,1,2,5,8,7,10,9};
	int nLen = sizeof(base)/sizeof(int);
	int* ptr = new int[nLen];

	cout<<"原始数组:"<<endl;
	arrShow(base, nLen);

	cout<<"冒泡:"<<endl;
	arrCopy(base, ptr, nLen);
	paopao_sort(ptr, nLen);
	arrShow(ptr, nLen);

	cout<<"选择:"<<endl;
	arrCopy(base, ptr, nLen);
	select_sort(ptr, nLen);
	arrShow(ptr, nLen);

	cout<<"插入:"<<endl;
	arrCopy(base, ptr, nLen);
	insert_sort(ptr, nLen);
	arrShow(ptr, nLen);

	cout<<"快速:"<<endl;
	arrCopy(base, ptr, nLen);
	quick_sort(ptr, 0, nLen-1);
	arrShow(ptr, nLen);

	cout<<"归并:"<<endl;
	arrCopy(base, ptr, nLen);
	int* pTmp = new int[nLen];
	merge_sort(ptr, pTmp, 0, nLen-1);
	arrShow(ptr, nLen);
	delete[] pTmp;

	cout<<"最大堆:"<<endl;
	arrCopy(base, ptr, nLen);
	heap_sort(ptr, nLen);
	arrShow(ptr, nLen);

	cout<<"最小堆:"<<endl;
	arrCopy(base, ptr, nLen);
	heaps_sort(ptr, nLen);
	arrShow(ptr, nLen);

	cout<<"二叉树:"<<endl;
	arrCopy(base, ptr, nLen);
	tree_sort(ptr, nLen);
	arrShow(ptr, nLen);

	delete[] ptr;
	return 0;
}



运行结果:



共有 人打赏支持
粉丝 13
博文 91
码字总数 36984
×
悠米海
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: