![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法导论
文章平均质量分 59
tangyifei1991
这个作者很懒,什么都没留下…
展开
-
二分查找
二分查找,元素存在则返回元素,不存在则返回-1 代码: int BinarySearch( int A[],int n,int x ) { int left=0; int right =n-1; while(left { int middle =(left+right)/2; if((A[middle])==x )return A[middle]; if (A[middl原创 2014-01-18 13:35:30 · 287 阅读 · 0 评论 -
基数排序
int maxdigit(int arr[],int digit) { int *temp =new int[digit]; int m=0; for(int i=0;i temp[i]=arr[i]; for(int i=0;i { int k=1; while((temp[i]/10)>0) { k++; temp[i]/=10; } if(m } de原创 2014-01-18 14:19:00 · 343 阅读 · 0 评论 -
快速排序-随机选择,寻找最小的第K个数
#include "stdafx.h" #include #include #include using namespace std; int myrand(int p,int r) { int size= r-p; return p+rand()%size; } void swap(int &a,int &b) { int temp=b; b=a; a=tem原创 2014-01-18 14:37:20 · 536 阅读 · 0 评论 -
堆排序
#include "stdafx.h" #include #include using namespace std; void swap(int &a, int& b) { int temp; temp=b; b=a; a=temp; } void MaxHeapfy(int arr[],int size,int root) { int child =2*root+1;原创 2014-01-18 14:53:45 · 354 阅读 · 0 评论 -
二叉查找树
头文件: class BinaryTreeNode { public: BinaryTreeNode():key(0),left(NULL),right(NULL){}; BinaryTreeNode(int value, BinaryTreeNode *L,BinaryTreeNode *R):left(L),right(R),key(value){}; friend class B原创 2014-01-18 16:32:05 · 377 阅读 · 0 评论 -
旋转树
class AvlNode { private: int key; AvlNode *left; AvlNode *right; int Height; public: AvlNode(int num,AvlNode *L,AvlNode *R,int H):key(num),left(L),right(R),Height(H){} int height(AvlNode *t)原创 2014-01-18 16:40:10 · 417 阅读 · 0 评论 -
矩阵链乘法
#include "stdafx.h" #include using namespace std; void matrixchainorder(int *p,int(*m)[10],int(*s)[10],int length) { int n =length-1; for(int i=1;i m[i][i]=0; for(int l=2;l原创 2014-01-18 16:49:03 · 385 阅读 · 0 评论 -
广度优先搜索
#include "stdafx.h" #include #include #include using namespace std; #define N 6 #define INFINITE 0x7fffffff #define WHITE 1 #define GRAY 2 #define BLACK 3 struct Vertex { Vertex *next; in转载 2014-01-18 20:15:39 · 353 阅读 · 0 评论 -
深度优先搜索
#include "stdafx.h" #include using namespace std; #define N 6 #define INFINITE 0x7fffffff #define WHITE 1 #define GRAY 2 #define BLACK 3 struct Vertex { Vertex *next; int id; Vertex():next原创 2014-01-18 20:03:12 · 308 阅读 · 0 评论 -
Kruskal算法
#include "stdafx.h" #include #include using namespace std; #define n 9 #define max 100 typedef struct edge *link; struct edge{ int u,v; int value; } e[max]; int p[max]; //用于记录最小生成树的边 in转载 2014-01-18 20:40:49 · 344 阅读 · 0 评论 -
shell排序
#include "stdafx.h" #include #include using namespace std; void swap(int &a,int &b) { int tmp=b; b=a; a=tmp; } void shellInsert(vector &array,int d,int num) { for(int i=d;i { int j=i原创 2014-07-06 11:12:25 · 331 阅读 · 0 评论 -
计数排序
void countsort(int A[],int B[],int n,int k ) { // n是数组长度,k是整数 int *C=new int[k+1]; for(int i=0;i C[i]=0; for(int j=0;j C[A[j]]=C[A[j]]+1; for(int i=1;i C[i]=C[i]+C[i-1]; for(int j=n-1;j>原创 2014-01-18 14:15:14 · 343 阅读 · 0 评论 -
快速排序
void swap(int &a,int &b) { int temp=b; b=a; a=temp; } int Partition(int arr[],int p, int r) { int x=arr[r]; int i=p-1; for(int j=p;j { if(arr[j] { i=i+1; swap(arr[i],arr[j]); } }原创 2014-01-18 14:10:02 · 283 阅读 · 0 评论 -
归并排序
void Merge(int arr[],int p ,int q,int r) { int m=q-p+1; int n=r-q; int *L=new int[m]; int *R= new int[n]; for(int i=0;i L[i]=arr[p+i]; for(int j=0;j R[j]=arr[q+j+1]; int i=0; int j=0;原创 2014-01-18 14:04:40 · 398 阅读 · 0 评论 -
桶排序
typedef struct node *link; struct node { double value; link next; }; void BucketSort(double *arr,int length) { int counter=0; node *key=new node[10]; link p,q; for(int i=原创 2014-01-18 14:28:07 · 374 阅读 · 0 评论 -
约瑟夫环
9个人选出一个领导,将剩下所有人排成一个圆周,沿这个圆周每次数5个人就排除出队者,有人出列后,剩下的人靠拢,仍然保持一个完整的圆周。找出最后剩下的那个人是谁?(水平有限,写的不规范) #include #include using namespace std; typedef struct node* link; struct node { int item;原创 2014-01-18 15:18:31 · 408 阅读 · 0 评论 -
散列
#include "stdafx.h" #include #include using namespace std; int h1(int k,int m) { return k%m; } int h2(int k,int m,float A) { float fnum=(float)k; float re=((fnum*A)-(int)(fnum*A))*m; retur原创 2014-01-18 15:23:36 · 331 阅读 · 0 评论 -
二叉树的祖先结点,删除,插入,查找,前驱结点,后继结点等
头文件 #include using namespace std; class BinaryTreeNode { public: friend class BinaryTree; BinaryTreeNode():key(-1),left(NULL),right(NULL){} BinaryTreeNode(int num):key(num),left(NULL),right(原创 2014-01-18 16:18:48 · 712 阅读 · 0 评论 -
动态规划-钢条切割问题
int BottomUpCutRod(int p[],int n) { int *r=new int[n+1]; r[0]=0; for (int j=1;j { int q=-1; for(int i=0;i { q=max(q,p[i]+r[j-i-1]); } r[j]=q; } return r[n]; }原创 2014-01-18 16:45:46 · 377 阅读 · 0 评论 -
最长公共子序列
程序有错误,仅供本人阅览 #include "stdafx.h" #include #include using namespace std; void LCSLength(string x,string y, int(*c)[100],int(*b)[100]) { int m=x.length(); int n=y.length(); for(int i=1;i c[原创 2014-01-18 16:53:31 · 440 阅读 · 0 评论 -
贪心算法-活动选择问题
#include "stdafx.h" #include using namespace std; int GreedySelect(int *s,int *f,int length,int *a) { a[1]=1; int k=1; int j=2; for(int m=2;m { if(s[m]>=f[k]) { a[j++]=m; k=m;原创 2014-01-18 17:02:00 · 359 阅读 · 0 评论 -
赫夫曼编码
#include "stdafx.h" #include #include #include using namespace std; class HaffmanNode { public: HaffmanNode(int nKeyValue,HaffmanNode*pleft=NULL,HaffmanNode *pright=NULL) { m_nkeyvalue =nK转载 2014-01-18 17:06:17 · 450 阅读 · 0 评论 -
0-1背包问题
背包问题,代码有误,仅供本人参考 #include "stdafx.h" #include using namespace std; int max(int a,int b) { return a>b?a:b; } int KnapSnack(int capacity,int n,int weight[],int value[]) { int i; int totalvalu原创 2014-01-18 17:12:53 · 362 阅读 · 0 评论 -
冒泡排序
void swap(int& a,int& b) { int temp=b; b=a; a=temp; } void BubbleSort(int arr[],int n) { for(int i=0;i for(int j=n-1;j>i;j--) if(arr[j] swap(arr[j],arr[j-1]); } int main() { int a原创 2014-01-18 14:01:20 · 319 阅读 · 0 评论