Algorithm
文章平均质量分 67
fduan
这个作者很懒,什么都没留下…
展开
-
EM algorithm for GMM in MATLAB
function [label, model, llh] = emgm(X, init)% EM algorithm for Gaussian mixture model%% initializationfprintf('EM for Gaussian mixture: running ... ');R = initialization(X,init);tol = 1e-6;maxi转载 2012-10-14 22:10:18 · 2449 阅读 · 1 评论 -
数据结构题典018:三个有序单链表求交(ANSI C)
题目:设有三个带头结点的元素按非递减有序排列的单链表A, B, C,对A进行如下操作,使A中仅含有三个表中的交,且没有值相同的结点,并释放无用结点。限定时间复杂度为O( m + n + p ),其中m, n, p分别为三个表的长度。/* * Intersection of three ordered linked lists. * * fduan, Dec. 29, 2011.原创 2011-12-29 23:24:36 · 1022 阅读 · 0 评论 -
数据结构题典017:从无序数据建立有序顺序表(ANSI C)
题目:从无序的输入数据中建立一个递增有序的顺序表。int order_insert_array( int x[], int e, int n, int m ){ int len = m, pos, i; if( m == 0 ) x[len++] = e; else { i = 0; while( i < m && x[i] < e ) ++i; pos = (原创 2011-12-29 22:59:00 · 1910 阅读 · 0 评论 -
数据结构题典010:有序单链表的交集(ANSI C)
题目:设有两个非递减有序的单链表,编写算法求二者的交集(以链表形式存放),要求交集中元素保持递增有序。分析:此题关键是要跳过相邻的重复元素。/* * Intersection of two non-descending linked lists. * * fduan, Dec. 28, 2011. */void intersection( link_list lst_a, l原创 2011-12-28 22:08:45 · 1939 阅读 · 0 评论 -
数据结构题典014:单链表的子序列检测(ANSI C)
题目:设有两个整数序列 A = a_1, a_2, ..., a_m和 B = b_1, b_2, ..., b_n已存入两个单链表,设计算法判断序列B是否为序列A的子序列。int is_subseq_llist( link_list lst_a, link_list lst_b ){ node_ptr pa = lst_a->next, pb = lst_b->next; while(原创 2011-12-29 00:13:23 · 1360 阅读 · 1 评论 -
数据结构题典016:按递增次序输出单链表所有元素(ANSI C)
题目:按递增次序输出单链表所有元素,并释放结点的存储空间。要求不使用数组做辅助空间。#include void min_elem( link_list * lst, node_ptr * min_prev, node_ptr * min_ptr ){ node_ptr p = (*lst)->next, prev = NULL; elem_type min_v = INT_MAX;原创 2011-12-29 02:40:09 · 3016 阅读 · 0 评论 -
数据结构题典013:链表合并之二(ANSI C)
题目:设有两个元素递增的单链表(带头结点),编写算法将二者合并为按元素递减排列的链表L,要求利用原表的结点空间存放L。/* * fduan, Dec. 28, 2011. */void calc_union_v2( link_list * lst_a, link_list * lst_b ){ node_ptr pa = *lst_a, pb = *lst_b, p = NULL;原创 2011-12-28 23:50:35 · 820 阅读 · 0 评论 -
数据结构题典012:链表求交集之二(ANSI C)
问题:已知两个按元素递增排列的链表,求二者交集,要求将结果放入第一个链表中。/* * Intersection of two ordered linked lists. * * fduan, Dec. 28, 2011. */void intersect_v2( link_list * lst_a, link_list lst_b ){ node_ptr pa = *lst_a原创 2011-12-28 23:24:09 · 1001 阅读 · 0 评论 -
数据结构题典011:有序单链表的并集(ANSI C)
题目:与009类似,但这次是求并集。分析:与有序顺序表归并思路类似,只是注意要处理两个顺序表当前指针指向的元素相同的情形(此时只为并集中增加一项)。/* * Union set of two linked lists which have non-descending order. * * fduan, Dec. 28, 2011. */void calc_union( lin原创 2011-12-28 22:24:52 · 1822 阅读 · 0 评论 -
数据结构题典009:递归实现单链表逆序数出(ANSI C)
设所考虑单链表含头结点,写出逆序输出表中元素的递归算法。void inv_trav_recur( link_list p ){ if( p != NULL ) { inv_trav_recur( p->next ); printf( "%d ", p->data ); }}void inverse_traverse_llist( link_list lst ){ i原创 2011-12-28 08:56:57 · 1020 阅读 · 0 评论 -
数据结构题典008:顺序表的合并(ANSI C)
题意:设有顺序表La和Lb,二者中元素均为非递减有序,空间足够大。设计算法将Lb中的元素合并到La中,使新的La元素仍非递减有序。分析:此题与将两个有序顺序表合并到第三个顺序表中的思路类似,只是为了减少移动次数,比较的次序从两线性表的尾部开始,这样每个元素最多只移动一次。/* * merging of two ordered sequences * * fduan, Dec. 27,原创 2011-12-28 01:25:43 · 892 阅读 · 0 评论 -
数据结构题典002:删除单链表中最大元素所在结点(ANSI C)
分析:此题关键在于找到最大元素所在的前驱结点。status_code remove_max_elem_llist( link_list * lst, elem_type * e ){ status_code res = Success; node_ptr h = *lst, p = h, pre_max = p; int max_v = -1000; while( p->next原创 2011-12-25 01:24:13 · 3280 阅读 · 0 评论 -
用单链表实现多项式运算(ANSI C)
1、多项式结构定义polynomial.h#ifndef _polynomial_h#define _polynomial_hstruct poly_term{ int expn; int coef;};typedef struct poly_term poly_term;typedef poly_term * poly_term_ptr;struct poly_n原创 2011-12-23 02:48:01 · 755 阅读 · 0 评论 -
数据结构题典006:有序表中冗余元素的删除(ANSI C)
1、顺序表,假设元素已按升序排序/* * remove redundant elements from ordered sequence */int remove_redundant_elem( int c[], int n ){ int i = 0, j = 1; while( j < n ) { if( c[i] != c[j] ) c[++i]原创 2011-12-25 19:24:57 · 786 阅读 · 0 评论 -
数据结构题典005:单链表的复制(ANSI C)
数据结构题典005:单链表的复制(ANSI C)void clone_node( node_ptr src, node_ptr * dst ){ if( src != NULL ) { *dst = ( node_ptr )malloc( sizeof( node ) ); (*dst)->data = src->data; } else *dst = NULL;}原创 2011-12-25 10:48:23 · 778 阅读 · 0 评论 -
简单选择排序(C & Java 实现)
C语言版 #include #define SWAP( a, i, j ) { t = a[i]; a[i] = a[j]; a[j] = t; }void disp_array( int a[], int n ){ int i; for( i = 0; i < n; ++i ) printf( "%d ", a[i] );原创 2012-08-30 06:14:26 · 686 阅读 · 0 评论 -
数据结构题典022:栈的应用——行编辑程序(C语言版)
编写程序实现从终端接收用户输入的数据,并存入用户数据区。输入#表示上一字符无效,输入@表示当前输入行整行无效。/* * line editor by using stack. * * fduan, Dec. 31, 2011. */void line_editor(){ using std::stack; stack ss; std::string li原创 2011-12-31 04:20:23 · 3656 阅读 · 1 评论 -
最小堆的实现(C 语言版)
最小堆本质上是一棵局部有序的完全二叉树,适于需要查找序列中前k小的元素的场合,如构造Huffman树。核心算法为 向上调整(fix up)和向下调整(fix down)算法。void fix_up_min_heap( int arr[], int n, int len, int i ){ int j = ( i - 1 ) / 2; // parent index int tmp =原创 2012-08-23 20:09:25 · 4955 阅读 · 0 评论 -
冒泡排序算法(C & Java 实现)
C语言版#include #define SWAP( a, b ) { t = a; a = b; b = t; }#define LESS( a, b ) a < bvoid disp_array( int a[], int n );void bubble_sort( int a[], int n ){ int i, j, t; int flag = 1; /原创 2012-08-28 00:58:18 · 831 阅读 · 0 评论 -
快速排序算法(C & Java 实现)
C语言版 #include void disp_array( int a[], int n ){ int i; for( i = 0; i < n; ++i ) printf( "%d ", a[i] ); printf( "\n" );}int partition( int a[], int low, int high ){ in原创 2012-08-30 07:28:27 · 805 阅读 · 0 评论 -
堆排序算法(C & Java 实现)
C语言版#include #define SWAP( a, i, j ) { t = a[i]; a[i] = a[j]; a[j] = t; }#define LESS( a, b ) ( a < b ) void disp_array( int a[], int n ){ int i; for( i = 0; i < n; ++i ) printf( "%d ",原创 2012-08-30 11:05:30 · 630 阅读 · 0 评论 -
面试题解:二维数组中的查找(C 语言版)
题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。输入:输入可能包含多个测试样例,对于每个测试案例,输入的第一行为两个整数m和n(1输入的第二行包括一个整数t(1接下来的m行,每行有n个数,代表题目所给出的m行n列的矩阵(矩阵如题目描述所示,每一原创 2012-08-19 21:55:31 · 1789 阅读 · 0 评论 -
数据结构题典021:栈的应用——括号匹配的检验(C++)
题目:假设表达式中允许出现圆括号和方括号,其嵌套顺序随意,设计算法检验给定表达式中的括号是否匹配。/* * Brackets matching algorithm by utilizing stack. * * fduan, Dec. 31, 2011. */bool is_pair( char c1, char c2 ){ return ( c1 == '(' && c2原创 2011-12-31 03:16:18 · 2037 阅读 · 0 评论 -
数据结构题典020:栈的应用——数制转换(ANSI C)
题目:将十进制数 src 转换为 k 进制数。原理:N = ( N div k ) x k + N mod k,其中div为整除,mod为取余。void num_sys_convert( int src, int k ){ link_stack ls; init_stack_link( &ls ); while( src != 0 ) { push_link( &ls,原创 2011-12-30 15:02:58 · 677 阅读 · 0 评论 -
数据结构题典007:顺序表中元素块的位置交换(ANSI C)
假设有顺序表 L.elem[] = { a_1, a_2, ..., a_m, | b_1, b_2, ..., b_n },设计算法将L的两部分元素互换,使得 L.elem[] = { b_1, b_2, ..., b_n, |a_1, a_2, ..., a_m }思路一:循环移位,数组整体循环右移n个元素void circ_right_shift( int c[], int len,原创 2011-12-26 00:20:36 · 2251 阅读 · 0 评论 -
数据结构题典003:线性表的就地逆置/翻转(ANSI C)
1、顺序表a) 通过数组下标访问元素/* * 通过数组下标访问 */void reverse_sqlist( int c[], int n ){ int i = 0, j = n - 1; int t; for( ; i < j; ++i, --j ) { t = c[i]; c[i] = c[j]; c[j] = t; }}b) 通过指针访问元素/原创 2011-12-25 02:29:54 · 1084 阅读 · 0 评论 -
Solution of ZOJ 1205 Martian Addition
In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit原创 2011-05-12 15:05:00 · 690 阅读 · 0 评论 -
Solution of ZOJ 1949 Error Correcting
A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property: 1 0 1 00 0 0 01 1 1 10 1 0 1The sums of the rows原创 2011-05-12 12:02:00 · 787 阅读 · 0 评论 -
Solution of ZOJ 1188 DNA Sorting
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is原创 2011-05-12 09:59:00 · 1803 阅读 · 0 评论 -
Solution of ZOJ 2095 Divisor Summation (Online Version)
Give a natural number n (1原创 2011-05-12 01:12:00 · 1330 阅读 · 0 评论 -
Solution of ZOJ 2850 Beautiful Meadow
Tom has a meadow in his garden. He divides it into N * M squares. Initially all the squares were covered with grass. He mowed down the grass on some of the squares and thinks the meadow is beautiful if and only if Not all squares are covered with grass原创 2011-05-11 22:32:00 · 1104 阅读 · 0 评论 -
Solution of ZOJ 2857 Image Transformation
Convert color images to a gray-level images.原创 2011-05-11 20:01:00 · 778 阅读 · 0 评论 -
算法百题007:数字逆序求和(Adding Reversed Numbers)
Reversed number is a number written in arabic numerals but the order of digits is reversed. Calculate the reversed sum of a set of reversed numbers.原创 2011-05-11 16:35:00 · 2083 阅读 · 0 评论 -
算法百题006:单词逆序输出(Word Reversal)
For each list of words, output a line with each word reversed without changing the order of the words.原创 2011-05-11 10:34:00 · 3812 阅读 · 0 评论 -
用C++实现单词分割
题目:假定有如下语句行“I am happy today”,要求以空格为分隔符实现单词的分割。原创 2011-05-11 10:16:00 · 5993 阅读 · 1 评论 -
A Simple Implementation of Binary Search Tree in C++
A Simple Implementation of Binary Search Tree in C++原创 2011-05-08 15:35:00 · 809 阅读 · 0 评论 -
Algorithm Visualization of USFCA
Cited from http://www.cs.usfca.edu/~galles/visualization/Algorithms.html原创 2011-05-08 13:14:00 · 1319 阅读 · 0 评论 -
算法百题005:数字根(Digital Roots)
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process原创 2011-05-10 12:14:00 · 2889 阅读 · 1 评论 -
算法百题004:数据压缩——游程编码
题目:给定一个包含ASCII码的字符串,利用下列规则进行编码。1)将子串中连续k个相同字符c编为kc;2)若子串长度为1,则1忽略。原创 2011-05-08 19:30:00 · 1667 阅读 · 0 评论 -
Solution of ZOJ 1797 Least Common Multiplier
Calculating the least common multiple (LCM) of a set of positive integers.原创 2011-05-11 14:51:00 · 806 阅读 · 0 评论