algorithm
共 21 篇文章
c - linkedlist
c - linkedlist store ordered number in linkedlist, no repeat. code: linkedlist_test.c: #include <stdio.h> #include <stdlib.h> extern struct linkedlist { int num; struct linkedlist *pnext; }; /** * add a num to ordered linkedlist, if it not exists yet, * * @param num * the number to add * @param sta…
c - word counter (binary-tree)
c - word counter (binary-tree) count ascii word in a file, written by c, using binary-tree, code: word_counter.c: #include <stdio.h> #include <string.h> #include <stdlib.h> #define WORD_MAX_LEN 50 #define LAST_LETTER 1 #define LAST_OTHER 0 /** * count ascii words * @author kuchaguangjie * @date 201…
random select
random select problem: select m number from 0 - (n-1) randomly, ------ solution 1 for each number in 0 - (n-1), in increasing order, generate a random number x, using "(x%remain) < to_select" to decide will the number be choose, until m number is choosen, time: o(n), when n is small, this is quick,…
sparse data structure - matrix
sparse data structure sparse data structure - means most element are empty(or have a very same value), so we can try to use some better data structure to reduce memory use, ------ matrix we can use 'header array' & 'link list' to represent matrix, how to: define a data structure to represent a elem…
max sub_sequence - c
max sub_sequence - c /* problem: there is a sequence of number,could be positive or negative, find out the sub sequence which has max sum, solution: loop the sequence, use maxendinghere to record sum of elements,when sum < 0, set to 0 and go on summation from next element, use maxsofar to record th…
binary search - c
binary search - c (simple) search - basic: /** @author kuchaguangjie@gmail.com */ #include <stdio.h> /** * binary search (simple) * * @param * arr pointer of array * @param * len length of array * @param * x target to search * * @return index of target in the array,return -1 if not found, */ int bi…
bit_array - simple use
bit array ,use less memory to describe a set of int number, example: /** * use bit string to store int numbers,if char[n] = 1,then n is include,else n is not exclude, */ #include <stdio.h> // set a bit to 1,which indicate that a number is include, void setbit(char data[], int pos); // get a bit,1 =…
linkedlist - java 简单实现
linked list 链表, ------ 特点: linkedlist 相对于基于数组的 list: * 在中间 插入 & 删除 时,效率较高,只影响 前结点的next指针 和 后结点的pre指针, * 根据 index get() 时 效率较低,需要一个个查找 * ------ 结构: 每个元素包含: * value 值, * pre 前一个元素, * next 后1个元素, ------ 操作: * search O(n) * insert O(1) * delete O(1) * Sentinels 用 一个 特殊值 表示不存在的值,以方便各种操作对空元素的判断, * ------…
2011-02-11 21:29 · algorithm · Java · junit · J#
queue (用 java 简单实现)
queue ------ 结构 线性存储,1个出口 & 1个入口, first-in, first-out ------ 操作 * insert (enqueue) 在入口处添加, 时间复杂度:O(1) * delete (dequeue) 在出口处删除, 时间复杂度:O(1) * ------ 实现方案: 用 Object[] 存贮数据,类采用泛型, add() & remove() 方法,对数组进行操作,并用 synchronized 控制并发, 数组 小index 是 head,大index是tail, 插入在 tail 处,删除在 head 处, 用2个变量分别保存当前 下次 插入 …
2011-02-03 01:45 · algorithm · C · Java · junit · C++ · 数据结构
Medians and Order Statistics (次序统计)
[b]Medians and Order Statistics[/b] ------ [b]概述[/b] Order Statistics: 次序统计,即 找出 n 个数中 排在 第 i 位 的那个数,记为 ith Medians: 中位数,排在中间的数, ------ [b]Medians 的取值[/b] 假设所有的数都不相同,则: n = odd 时,只有1个 中间数,i = (n+1)/2, n = even 时,有2个 中间数,i = n/2 和 i = n/2 + 1, 可以合并为: i = lower((n + 1)/2), upper((n + 1)/2), 对于 odd 2者…
2011-01-03 14:36 · algorithm · 算法 · prototype · XHTML
counting sort
[b]counting sort[/b] ------ [b]counting sort 概述[/b] 输入是 0-k 之间的整数,通过 计算比每个值小的值的个数来排序, ------ [b]性能[/b] 不是 comparation sort, 时间复杂度是 O(n) 空间复杂度是 O(n) ------ [b]排序步骤[/b] * 求出 最小、最大 值 * 初始化 计数数组 * 遍历 input 数组,计数 * 由计数数组 求出 大于等于 每个数的个数, * 遍历 输入数组,根据计数结果每个输入值 排序后的 位置,组成输出数组, * 输出 * ------ [b]例子[/b] * js …
2011-01-02 20:36 · algorithm · HTML · J# · prototype · XHTML
quick sort
[b]quicksort[/b] ------ [b]quicksort overview[/b] quicksort 在实际应用中 非常出色, 时间复杂度: 理论上最差是:O(n^2), 实际应用中:平均可达到 O(n * lg(n)),且其中的 常量 比较小, 空间复杂度: 空间占用比较小,有优势, 即使在 虚拟内存中 也运行得较好, ------ [b]quicksort 原理[/b] quicksort 采用 divide-conquer 方法, 分3步: * divide 将 A[p .. r] 分解为 A[p .. q-1] 和 A[q+1 .. r] 2个子数组,分解函数称为 …
2011-01-01 20:26 · algorithm · HTML · J# · prototype · XHTML
priority queue
[b]priority queue[/b] priority queue , 基于 heap,封装了一些操作而实现 优先级队列 的功能, ------ [b]分类:[/b] * max-queue: 对应 max-heap, 用于取出最大的值, 应用: 任务队列 (高优先级先执行) * min-queue 对应 min-heap, 用于取出最小的值, 应用: 事件触发队列 (触发时间小的先执行) ------ [b]priority queue 的 operation[/b] [b]max-queue:[/b] * INSERT(S, x) 将 x 放入 set S 中, 可记为: S ← …
2010-12-22 00:11 · algorithm · HTML · 算法 · prototype · XHTML
heap sort
[b]heapsort[/b] ------ [b]heap 数据结构[/b] heap 是以 nearly complete binary tree 存储数据的, tree height = lg(n+1), max-heap: A[i] >= A[sub(i)], 即 父节点 >= 子节点 ------ [b]heapsort 基本原理[/b] 首先将 heap 组装为 max-heap,则 root node 为最大值, 每组装1次 max-heap,就将 root node 取出放大结果数组中, 这样遍历 n-1 次后,就获得了已排序的数组, ------ [b]heapsort 性…
2010-12-18 19:09 · js · algorithm · HTML · 数据结构 · prototype · JavaScript · XHTML
merge sort
[b]merge sort[/b] 合并排序 ------ [b]merge sort 原理 [/b] 采用 divide-and-conqure,即 分而治之, 将问题分成最小的单元,然后从最小的解决,再不断合并,合并中继续解决,直到完成, ------ [b]例子:[/b] * js 代码 [code="js"] var arr_merge= [ 78, 13, 6, 177, 26, 90, 288, 45, 62 ]; /** * merge sort (合并排序),时间: o(n*lg(n)),内存: n, * * @param inputArr * @return */ func…
2010-12-01 23:37 · algorithm · HTML · XHTML
insertion sort
[b]insertion sort[/b] ------ [b]insertion sort 原理[/b] 不断将数据插入已排序的序列,每次插入时,逐个比较,直到找到比自己大的则插入, 时间:o(n^2) 内存:o(1) ------ [b]例子[/b]: * [b]javascript 代码[/b] [code="javascript"] var arr_one = [ 78, 13, 6, 177, 26, 90, 288, 45, 62 ]; /** * insertion sort (插值排序),时间: o(n^2),内存: 1, * * @param inputArr * @ret…
2010-10-28 00:21 · algorithm · HTML · J# · JavaScript · XHTML
z 字型 读取 矩阵
以 z 字型 读取 矩阵, ------ [b]思路[/b]: 以矩阵的 左斜切线 为读取单元, 步骤: * 求出 左斜切线 个数 * 遍历 左斜切线 * 求出 左斜切线的 长度 * 求出 左斜切线的 起点、止点 * 遍历 左斜切线,遍历方向 由 左斜切线的index 决定, * * ------ [b]主要用到的数学知识[/b]: * m 行 n 列 矩阵中 * 共有 m+n-1 个 左斜切线 * 每条左斜切线的长度 = (左斜切线的index + 1) - 该线超出row长度 - 该线超出col长度 * * ------ [b]示例[/b]: * [b]js[/b] [code="jav…
2010-10-23 16:50 · algorithm · HTML · Blog · JavaScript · 单元测试 · IDEA
排序算法:求 长度为 n的 数组中,最大的 m个数
排序:数组 length=m,从其中中取出最大的 n 数字,n<=m import java.util.Random; import java.util.TreeSet; /** * 排序算法:数组 length=m,从其中中取出最大的 n 数字,n<=m * 思路:用 TreeSet 存放结果,可以自动排序,先取前100个放到 TreeSet,然后遍历其余的,如果大于 TreeSet 中最小的,则删除 TreeSet 中最小的值,并将新值放到 TreeSet 中, */ public class TopMax { /** * 如果数字不在 treeset 里,则加入,并返回 true。 *…
2010-08-31 10:16 · algorithm · 算法
已排序数组 a,求其元素的绝对值 共有多少个不同的值
已排序数组 a,求其元素的绝对值 共有多少个不同的值? 思路: 2个 index,从最接近0的2个元素开始向2端遍历,每次先比较2个index所在的元素的绝对值,取小值,与最后1次的值比较,如果大于,则 count++,并将该值设为 最后一次的值,然后将小的那个 index 向前进的方向加1, 算法的复杂度: 因为只要遍历1次,因此复杂度是 o(n),n是数组的个数, 内存占用: 需要2个 index,1个 last,1个 count,因此除数组外,额外占用的内存是固定的,即 o(1) 代码: package test; public class Test { public static v…
2010-08-29 20:41 · algorithm · 算法
binary search
binary search in sorted array: package me.util.algorithm; /** * binary search util * @author eric * @date 2010-8-29 下午07:09:41 */ public class BinarySearchUitl { public static void main(String[] args) { int[] is = new int[] { -10, -9, -9, -5, -4, -3, -3, -2, -1, 0, 1, 2, 5, 6, 7, 8, 9, 11, 14 }; Sy…
2010-08-29 19:35 · algorithm