
算法入门
文章平均质量分 53
火箭丸子
艾比斯星球战神
展开
-
算法入门--堆排序2(建立最小堆,从大到小)
#include #include int left(int i)//返回左孩子位置 { return 2*i;}int right(int i)//返回右孩子位置 { return 2*i+1;}void min_heapify(int *a,int heap_size,int i)//保持堆性质,使以i为根的子树成为最小堆 ,heap_size当前堆中元素数量原创 2011-11-24 17:18:24 · 4582 阅读 · 0 评论 -
c++ 单链表基本操作
#include #include #include #include #include /*c++实现简单的单链表操作*/using namespace std;typedef struct student{ int data; struct student *next;}node;//建立单链表 node *creat(){ n原创 2012-03-13 21:01:44 · 29557 阅读 · 1 评论 -
算法入门-选择排序
#include #include /*选择排序 从后面依次挑出最小的元素顺序放到前面的数组不稳定 */int main(int argc, char *argv[]){ int a[100]; int i,j=1,n; int temp; int key,keynum; printf("输入数组的长度n"); scanf("%d",&n); printf原创 2011-11-22 11:25:08 · 395 阅读 · 0 评论 -
(1411)实现字符串与整数的相互转换(不使用itoa和atoi)
#include #include #include using namespace std;char *inttostring(int num,char *str)//整数转成字符串 { int i=0; char temp[10]; while(num!=0) { str[i]=num%10+'0';原创 2012-03-15 16:41:45 · 804 阅读 · 0 评论 -
算法入门-分治法(合并排序中不使用哨兵)
#include #include /*分治法排序 O(n*lgn)*/#define MAXN 100int a[MAXN];void merge(int p,int q,int r)//原始合并排序算法,适用于任意长度的两个数据段 { int i,j,k; int n1=q-p+1; int n2=r-q; int L[n1+1];//L原创 2011-11-22 15:58:21 · 1364 阅读 · 0 评论 -
KMP字符串匹配算法
#include #include #include #include int next[11];void getNext(char *p){ memset(next,0,sizeof(next)); int i=-1,j=0; next[0]=-1; int len=strlen(p); while(j<len) {原创 2012-05-30 09:19:03 · 488 阅读 · 0 评论 -
最长上升子序列
一个数的序列bi,当b1题目为求出最长上升子序列的长度。#include #include #include #define MAXLEN 1000int b[MAXLEN+10];int aMax[MAXLEN+10];int main(int argc, char *argv[]){ int N; int i,j; int temp; scanf("原创 2012-05-24 15:07:53 · 727 阅读 · 1 评论 -
最长递增子序列编程之美232算法
#include #include int a[100];//存储原始序列int LIS[100];//存储以a[i]为最大元素的最长递增子序列的长度int MaxV[100];//存储长度为i的递增子序列最大元素的最小值int N;//存储原始序列的长度 int Min(int b[]){ int i; int temp=b[0]; for(i=0;i<N;i原创 2012-06-06 11:21:21 · 683 阅读 · 0 评论 -
最长公共子序列(动态规划求解)
输入:abcfbc abfcab programming contest abcd mnp输出 4 2 0#include #include #include #define MAX_LEN 1000char sz1[MAX_LEN];char sz2[MAX_LEN];in原创 2012-05-24 15:21:10 · 581 阅读 · 0 评论 -
算法入门-快速排序-基本快速排序方法
#include #include using namespace std;int a[10]={49,38, 65, 97, 76, 13, 27 };void exchange(int p,int q){ int temp; temp = a[p]; a[p] = a[q]; a[q] = temp;}void quicksort(int原创 2012-09-17 14:15:00 · 507 阅读 · 0 评论 -
循环链表实现约瑟夫环
#include #include #include #define ERROR 0using namespace std;typedef struct LNode{ int data; struct LNode *link;}LNode,*LinkList;void Josephus(int n,int k,int m){ //n原创 2012-03-13 22:23:31 · 872 阅读 · 0 评论 -
c++ 实现双链表基本操作
#include #include #include #include /*c++实现双链表的基本操作*/using namespace std;typedef struct student{ int data; struct student *pre; struct student *next;}dnode;//创立链表 dn原创 2012-03-13 21:02:57 · 5203 阅读 · 0 评论 -
算法入门--基数排序(对长度为3的字符串排序)
#include #include /*利用基数排序对长度为3的字符串进行排序,稳定排序*/void sort_at_i(char **a,int i,int length){/*i为当前要排序的第i位单词,即通过此函数对a[][i]进行排序,length为单词的个数 本例采用冒泡排序对第i位单词进行排序*/ char *temp; int j,k; for(j=0;原创 2011-11-29 15:26:53 · 2198 阅读 · 1 评论 -
算法入门--分治法排序(调用合并排序,使用哨兵进行判断)
#include #include /*分治法排序 */#define MAXN 100int a[MAXN];void merge(int p,int q,int r)//原始合并排序算法,适用于任意长度的两个数据段 { int i,j,k; int n1=q-p+1; int n2=r-q; int L[n1+2];//L[1]...L[原创 2011-11-22 15:24:49 · 944 阅读 · 1 评论 -
堆数据结构
(二叉)对数据结构是一种数组对象,可以被视为一种完全二叉树。树中每个节点与数组中存放该节点值得那个元素对应。表示堆的数组A是一个具有两个属性的对象:length[A]是数组中的元素个数,heap-size[A]是存放在A中的堆的元素的个数。也就是,虽然A[1..length[A]]中都可以包含有效值,但是在heap-size[A]之后的元素都不属于相应的堆。树的根为A[1],给定某个节点的原创 2011-11-24 14:43:53 · 610 阅读 · 0 评论 -
算法的时间复杂度和空间复杂度
常用的算法的时间复杂度和空间复杂度排序法最差时间分析平均时间复杂度稳定度空间复杂度冒泡排序O(n2)O(n2)稳定O(1)快速排序O(n2)O(n*log2n)不稳定O(log2n)~O(n)选择排序O(n2)O(n2)稳定O(1)二叉树排序O转载 2011-11-25 10:19:04 · 474 阅读 · 0 评论 -
算法入门--快速排序2(随机产生分割主元素)
#include #include /*随机快速排序算法,每次随机产生一个p...r之间的数作为主元交换对象,用于分割数组。 */void exchange(int *a,int i,int j)//交换数组两个数的位置 { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp;} int partition(i原创 2011-11-28 20:13:48 · 540 阅读 · 0 评论 -
算法入门--快速排序1
#include #include /*快速排序算法,最坏情况的运行时间为O(n*n),平均运行时间为O(n*lgn) */void exchage(int *a,int i,int j)//交换数组两个数的位置 { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp;}int partition(int *a原创 2011-11-28 10:18:52 · 770 阅读 · 1 评论 -
算法入门--最大堆实现优先队列
#include #include /*由于不会动态获得当前堆的元素数量heap_size,所以暂时用传参的方法,但是当多次运行后发现为了防止错误应该把heap_size设置为全局变量,主函数初始化后,让它被各个函数操作过程中动态改变也是可以的。 */int heap_maximum(char *a)//实现MAXIMUM(S),返回S中具有最大关键字的元素 { return原创 2011-11-25 16:04:35 · 1428 阅读 · 0 评论 -
算法入门--堆排序(最大堆,从小到大排序)
#include #include /*由于不会动态获得当前堆的元素数量heap_size,所以暂时用传参的方法,但是当多次运行后发现为了防止错误应该把heap_size设置为全局变量,主函数初始化后,让它被各个函数操作过程中动态改变也是可以的。 */int left(int i)//返回左孩子位置 { return 2*i;}int right(int i)//返回右孩原创 2011-11-24 17:06:17 · 4448 阅读 · 0 评论 -
算法入门--插入排序
/**插入排序 最好情况,输入数组已经排序,最差情况,输入数组逆序 O(n*n) */#include #include int main(int argc, char *argv[]){ int a[100]; int i,j,n; int key; printf("输入数组的长度n"); scanf("%d",&n); printf("依次输入要排原创 2011-11-22 10:19:35 · 353 阅读 · 0 评论 -
算法入门--计数排序
#include #include /*计数排序,经常作为基数排序的子过程,稳定的,时间复杂度O(n)对每一个输入元素x,确定出小于x的元素个数,直接放到相应的位置。 */void counting_sort(int *a,int *b,int k,int length){// a[]为原始数据数组,b[]为最终元素放置数组 ,k为最大的数的大小// length为数量,c[]为临原创 2011-11-29 11:00:30 · 454 阅读 · 0 评论 -
利用正则表达式检查素数
本帖最后由 遇见sharon 于 2013-8-12 17:09 编辑一般来说,我们会使用正规表达式来做字符串匹配,今天在网上浏览的时候,看到了有人用正则表达式来检查一个数字是否为素数(质数),让我非常感兴趣,这个正则表达式如入所示: 检查素数与否的正则表达式要使用这个正规则表达式,你需要把自然数转成多个1的字符串,如:2 要写成 “11”, 3 要写成 “转载 2013-08-13 09:04:28 · 989 阅读 · 0 评论