Tiap

649 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
649
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Tiap

  1. 1. kobe [email_address] Thinking in Algorithm Application -- for app developer
  2. 2. Thinking in Algorithm Application feedback from TIAP first time on 2009.6.3 非常有用 by 马程钟 很好,对工作很有帮助 by 孟光 不错,加深了对算法的理解 by 朱岩 相当不错 by 宫晶纬 再讲一遍 by 孙郁芳 ...
  3. 3. Thinking in Algorithm Application JAVA: public static int binarySearch(byte[] a, byte key) C++: template <class ForwardIterator, class T> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T> pair<ForwardIterator,ForwardIterator> equal_range ( ForwardIterator first, ForwardIterator last, const T& value );
  4. 4. Thinking in Algorithm Application Prime Number: near 600: 599 601 near 1200: 1201 near 3600: 3593 3607 near 86400: 86399
  5. 5. Thinking in Algorithm Application fast recursion framework: void func(array,m,n) { if(...) return; for(;;) { do_someting(); m=new_m1;n=new_n1; func(array,new_m2,new_n2); }//end for } test_vec.begin()
  6. 6. Thinking in Algorithm Application void TopN_most_beautiful_edtion(array,size,top) { std::make_heap(array,array+top); pos=array; for(;pos!=array+size;pos++) { if(array[pos]<array[0]) { array[top]=array[pos]; std::pop_heap(array,array+top+1); } }//end for }
  7. 7. Thinking in Algorithm Application array range: 10000 0000 top range: 100 average time cost: std::nth_element => 1.13s TopN_most_beautiful_edtion => 0.19s std::sort => 17.21s
  8. 8. Thinking in Algorithm Application self sort: void SelfSort(array) { int pos=array.size()-1; int expect_pos=0; while(pos>=0) { expect_pos=tellme_my_pos(array[pos]); if(expect_pos<pos) swap(array[pos],array[expect_pos]); else pos--; }//end while }
  9. 9. Thinking in Algorithm Application the best way to pickup random record in mysql: SELECT * FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;
  10. 10. Thinking in Algorithm Application simple KMP by Chenhua: int StrStr(string a, string b) { int m=0,n=0; for(;m<a.size()-b.size();) { while(n<b.size() && a[m]==b[n]) {m++;n++} if(n==b.size()) return m; if(n==0) {m++;continue;} if(n>=SFP) {m=m-n+SFP;} n=0; }//end for return -1; }
  11. 11. Thinking in Algorithm Application what can do in just 1 s, for some &quot;snail&quot; operations we thought: empty loop => 20 0000 0000 times md5 (256 char) => 60 0000 times mutex pair (lock,unlock) => 8000 0000 times sort (100 0000 array in mem) => 8 times binary search (100 0000 array in mem) => 250 0000 times thread pair (create,delete) => 10 0000 times all data above base on 4CPU 3GHz & 4GMem
  12. 12. Thinking in Algorithm Application some &quot;missing&quot; useful topic: 1, parser 2, matrix(pagerank) 3, parallel programming future study: 1, advanced graph algorithm 2, compiler implement 3, distributed algorithm valuable code base: 1 , C++ standard template library 2 , C++ boost library 3 , Java Development Kit
  13. 13. Thinking in Algorithm Application contributors: 播客排行榜 => fulin@staff 最大 N 元素 => jingwei@staff 原地排序 => yanbo@staff simple KMP => chenhua(yahoo) mysql random => chunsheng1@staff 博文多关键字过滤 => ouyangde@staff SNS 关键用户 => zhiyong@staff 优先队列 => jingwei@staff 80:20 => jingwei@staff zhuyan@staff
  14. 14. Thinking in Algorithm Application thanks to: [email_address] [email_address] [email_address] [email_address] [email_address] [email_address] [email_address] [email_address] [email_address] [email_address] feedback: [email_address]
  15. 15. <ul><ul><li>Let's enjoy algorithm application </li></ul></ul>

×