锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲不卡无码av中文字幕,亚洲裸男gv网站,精品国产亚洲一区二区三区http://m.tkk7.com/Skynet/category/42816.htmlzh-cnTue, 08 Dec 2009 08:15:29 GMTTue, 08 Dec 2009 08:15:29 GMT60璺熸垜涓璧峰 - 綆楁硶瀵艱 - 綰挎ф椂闂存帓搴?/title><link>http://m.tkk7.com/Skynet/archive/2009/12/08/305156.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Tue, 08 Dec 2009 06:21:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/12/08/305156.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/305156.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/12/08/305156.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/305156.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/305156.html</trackback:ping><description><![CDATA[<strong>涓嬮潰浠嬬粛鐨勬帓搴忛兘涓猴細闈炴瘮杈冩帓搴忔硶</strong><br /> <br /> 榪欓噷涓漢璁や負鍦ㄦ煇浜涚壒瀹氱殑鍦版柟闈炴瘮杈冩帓搴忕殑閫熷害闈炲父鏄庢樉錛?br /> 姣斿 錛?瀵瑰緟鎺掓暟鎹腑鏈夐『搴忓垎綾伙紝浣跨敤楦藉發鎬諱綋鍒嗙被錛岀劧鍚庡涓嶅悓綾誨埆鐨勫緟鎺掑皬鏁版嵁闆嗗悎閲囩敤 鎻掑叆錛屽揩鎺掔瓑鎺掑簭鏂瑰紡<br /> <br /> <br /> Counting sort 錛氳鏁版帓搴?br /> 鎻忚堪錛氳凱浠e緟鎺掑簭鏁扮粍鍑哄厓绱爔錛岀‘瀹氬皬浜庢鍏冪礌[z]涓暟錛岀劧鍚庢妸x鏀懼埌瀹冨湪鐨勬渶緇堣緭鍑烘暟緇刐z]涓娿?br /> 鐗規э細涓庡緟鎺掑兼湁鍏籌紱紼沖畾鐨勬帓搴忕畻娉曪紱寰呮帓搴忔暟鎹姹傝繃浜庝弗鏍鹼紝鏃犲疄闄呯敤澶勶紱<br /> 綆楁硶鐨勬楠ゅ涓嬶細<br />    1. 鎵懼嚭寰呮帓搴忕殑鏁扮粍涓渶澶у拰鏈灝忕殑鍏冪礌<br />    2. 緇熻鏁扮粍涓瘡涓間負i鐨勫厓绱犲嚭鐜扮殑嬈℃暟錛屽瓨鍏ユ暟緇凜鐨勭i欏?br />    3. 瀵規墍鏈夌殑璁℃暟绱姞錛堜粠C涓殑絎竴涓厓绱犲紑濮嬶紝姣忎竴欏瑰拰鍓嶄竴欏圭浉鍔狅級<br />    4. 鍙嶅悜濉厖鐩爣鏁扮粍錛氬皢姣忎釜鍏冪礌i鏀懼湪鏂版暟緇勭殑絎珻(i)欏癸紝姣忔斁涓涓厓绱犲氨灝咰(i)鍑忓幓1<br /> <br /> <br /> Radix sort錛氬熀鏁版帓搴?br /> 鎻忚堪錛氬皢鎵鏈夊緟姣旇緝鏁板?姝f暣鏁?緇熶竴涓哄悓鏍風殑鏁頒綅闀垮害,鏁頒綅杈冪煭鐨勬暟鍓嶉潰琛ラ浂. 鐒跺悗, 浠庢渶浣庝綅寮濮? 渚濇榪涜涓嬈℃帓搴?榪欐牱浠庢渶浣庝綅鎺掑簭涓鐩村埌鏈楂樹綅鎺掑簭瀹屾垚浠ュ悗, 鏁板垪灝卞彉鎴愪竴涓湁搴忓簭鍒?<br /> 鎺掑簭鏂瑰紡錛歀SD 鐢卞彸鍚戝乏鎺掞紱MSD 鐢卞乏鍚戝彸鎺?br /> 鐗規э細闈炴瘮杈冩帓搴忥紱寰呮帓鏁版嵁闇瑕佺粺涓鏍煎紡錛?br /> 鍋囪闇鎺掑簭鏁板垪鐨勫彇鍊艱寖鍥翠粠1...k,鎴戜滑浜庢槸寤轟竴涓狵+1鍏冪殑鏁扮粍 a[]錛屽茍璧嬪垵鍊間負0錛岀劧鍚庝究寮濮嬫帓搴忓伐浣滐細<br /> 綆楁硶鐨勬楠ゅ涓嬶細<br />    1. 鎸夎緭鍏ラ『搴忚鍏ユ暟鍒楋紝濡傛灉鎵璇葷殑鍏冪礌涓篿(1<=i<=k),鎴戜滑灝卞皢a[i]鐨勫煎姞涓錛岃繖鏍風洿鍒拌瀹屾墍鏈夌殑鍏冪礌銆?br />    2. 璇誨嚭鍏冪礌騫舵帓搴忥細鎴戜滑閬嶅巻鏁翠釜鏁扮粍錛屽鏋渁[i]=j(j>=0),鎴戜滑灝辮緭鍑簀嬈(琛ㄧず鍏冪礌i鍦ㄥ師鍏堟暟鍒椾腑鍑虹幇浜唈嬈?,榪欐牱杈撳嚭鐨勫簭鍒楀氨鏄凡鎺掑簭鐨勩?br />    3. 鐢變簬涓鑸帓搴忕畻娉曟秹鍙婂埌鍏冪礌涔嬮棿鐨勬瘮杈冿紝濡傛灉鍖栨垚姣旇緝鏍戝彲浠ョ煡閬擄紝榪欐牱鐨勬帓搴忕畻娉曞鏉傚害鐨勪笅闄愭槸O(N*lnN),鑰岃鏁版帓搴忔病鏈夋瘮杈冨厓绱狅紝鎵浠ユ墍闇鎺掑簭鏃墮棿灝卞皯浜嗭紝鎴戜滑鍙互鐪嬪埌璁℃暟鎺掑簭鐨勫鏉傚害涓篛(n*k)錛屽叾涓璳錛坘鐨勫畾涔夊悓涓婏級涓哄悎騫舵帓鍒楁墍闇鐨勬椂闂達紝鏄釜甯告暟銆?br />    4. 姝ょ畻娉曢傚悎鎵闇鎺掑垪鐨勫厓绱犲彇鍊艱寖鍥翠笉澶х殑鎯呭喌涓嬶紝鍚﹀垯浼氶犳垚絀洪棿鐨勬秷鑰楋紝姣斿錛屼竴鍏?00涓厓绱狅紝鍏跺彇鍊艱寖鍥翠粠1-100000錛屾樉鐒惰繖涓椂鍊欑敤鍩烘暟鎺掑簭鏄笉鍚堥傜殑銆?br /> <br /> <br /> <br /> Bucket sort錛氭《鎺掑簭<br /> 鎻忚堪錛氬伐浣滅殑鍘熺悊鏄皢闃靛垪鍒嗗埌鏈夐檺鏁伴噺鐨勬《瀛愰噷銆傛瘡涓《瀛愬啀涓埆鎺掑簭錛堟湁鍙兘鍐嶄嬌鐢ㄥ埆鐨勬帓搴忕畻娉曟垨鏄互閫掑洖鏂瑰紡緇х畫浣跨敤妗舵帓搴忚繘琛屾帓搴忥級銆?br /> 妗舵帓搴忎互涓嬪垪紼嬪簭榪涜錛?br />    1. 璁劇疆涓涓畾閲忕殑闃靛垪褰撲綔絀烘《瀛愩?br />    2. 瀵昏搴忓垪錛屽茍涓旀妸欏圭洰涓涓竴涓斁鍒板搴旂殑妗跺瓙鍘匯?br />    3. 瀵規瘡涓笉鏄┖鐨勬《瀛愯繘琛屾帓搴忋?br />    4. 浠庝笉鏄┖鐨勬《瀛愰噷鎶婇」鐩啀鏀懼洖鍘熸潵鐨勫簭鍒椾腑銆?br /> <br /> <br /> <br /> Pigeonhole sort錛氶附宸㈡帓搴?br /> 鎻忚堪錛氭槸涓縐嶆椂闂村鏉傚害涓篛(n)涓斿湪涓嶅彲閬垮厤閬嶅巻姣忎竴涓厓绱犲茍涓旀帓搴忕殑鎯呭喌涓嬫晥鐜囨渶濂界殑涓縐嶆帓搴忕畻娉? 浣嗗畠鍙湁鍦ㄥ樊鍊?鎴栬呭彲琚槧灝勫湪宸?寰堝皬鐨勮寖鍥村唴鐨勬暟鍊兼帓搴忕殑鎯呭喌涓嬪疄鐢?<br /> 綆楁硶鐨勬楠ゅ涓嬶細涓庢《鎺掑悓<br /> <br /> <br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/305156.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-12-08 14:21 <a href="http://m.tkk7.com/Skynet/archive/2009/12/08/305156.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>璺熸垜涓璧峰 - 綆楁硶瀵艱 - 蹇熸帓搴?/title><link>http://m.tkk7.com/Skynet/archive/2009/12/03/304668.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Thu, 03 Dec 2009 09:11:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/12/03/304668.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/304668.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/12/03/304668.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/304668.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/304668.html</trackback:ping><description><![CDATA[<br /> # 蹇帓 鍜?鍒嗘不 寰堝儚 閮芥槸<strong>鍒嗚屾不涔?/strong> 錛屼絾浠栦滑鍗存槸 鐩稿弽鐨?鏂瑰紡鎺掑簭 錛?br /> 鍒嗘不 鏄兂鎷嗗垎瀹屾垚鍚庯紝鍚堝茍浠ユ湁搴忕殑灝忔榪涜 <strong>鎺掑簭</strong> 錛岃屽揩鎺掓槸鐩存帴鐢卞師濮嬬殑“鎷嗗垎”鏉?strong>鎺掑簭</strong> 銆?br /> <br /> <br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008000;">#</span><span style="color: #008000;">encoding=utf-8</span><span style="color: #008000;"><br /> #</span><span style="color: #008000;">浠?nbsp;澶?nbsp;鍒?nbsp;灝?/span><span style="color: #008000;"><br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> partition(A,p,r):<br />     tmp</span><span style="color: #000000;">=</span><span style="color: #000000;">A[p]<br />     </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> True :<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> p</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;"><</span><span style="color: #000000;">r </span><span style="color: #0000ff;">and</span><span style="color: #000000;"> A[p] </span><span style="color: #000000;">></span><span style="color: #000000;"> tmp : p</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> r</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">></span><span style="color: #000000;">p </span><span style="color: #0000ff;">and</span><span style="color: #000000;"> A[r] </span><span style="color: #000000;"><=</span><span style="color: #000000;"> tmp : r</span><span style="color: #000000;">-=</span><span style="color: #000000;">1</span><span style="color: #000000;">    <br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> A[p]</span><span style="color: #000000;"><=</span><span style="color: #000000;">A[r]: A[p],A[r]</span><span style="color: #000000;">=</span><span style="color: #000000;">A[r],A[p]<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> r</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;"><=</span><span style="color: #000000;">p : </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> p<br /> <br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> quickSort(A,p,r):<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> p</span><span style="color: #000000;"><</span><span style="color: #000000;">r:<br />         q</span><span style="color: #000000;">=</span><span style="color: #000000;">partition(A,p,r)<br />         quickSort(A,p,q)<br />         quickSort(A,q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,r)<br /> <br /> A</span><span style="color: #000000;">=</span><span style="color: #000000;">[</span><span style="color: #000000;">9</span><span style="color: #000000;">,</span><span style="color: #000000;">61</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">,</span><span style="color: #000000;">14</span><span style="color: #000000;">,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">,</span><span style="color: #000000;">667</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">8</span><span style="color: #000000;">]<br /> </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> A<br /> quickSort(A,0,len(A)</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br /> </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> A<br /> <strong># 緇撴灉 </strong></span><strong>[667, 61, 14, 9, 8, 7, 7, 6, 3, -1]</strong></div> <br /> <br /> <br /> 鍥捐В錛?br /> 涓嬈¤凱浠h繃紼嬫弿榪?錛堜粠灝忓埌澶э級錛?br /> 1. 浠?A[0] 涓哄垏鍒嗙偣 鐢ㄤ復鏃跺彉閲?璁板綍 榪欓噷鏄?<strong>鍒囧垎鐐?/strong> = [5] <br /> 2. 鍒嗗埆璧?2鏋氭寚閽?[鍒囧垎璧峰乏,鍙砞<br /> 3. 鍒嗗埆鍚戜腑闂?闈犳嫝 錛?褰撳乏鎸囬拡鎸囧悜鍊煎ぇ浜?鍒囧垎鐐?鍋滄 宸?錛?鍙蟲寚閽堟寚鍚戝?灝忎簬 鍒囧垎鐐?鍋滄 鍙?銆?br /> 4. 鍒ゆ柇 鏄惁鏄?nbsp; 鍋滄鐐?涓?宸﹀?灝忎簬 鍙沖?鏄細浜ゆ崲涓ゆ寚閽堝?錛?<br /> <img src="http://m.tkk7.com/images/blogjava_net/skynet/42926/qs.jpg" alt="" border="0" /><br /> <strong>絎竴嬈¤凱浠e悗 :  <br />   浠ュ垵濮嬪垎闅?[5]  灝卞凡緇忓垏鍒嗗ソ浜?灝忎簬 5 鐨勫乏 錛屽ぇ浜庣瓑浜? 鐨勫彸</strong><br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/304668.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-12-03 17:11 <a href="http://m.tkk7.com/Skynet/archive/2009/12/03/304668.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>璺熸垜涓璧峰 - 綆楁硶瀵艱 - 鍫?/title><link>http://m.tkk7.com/Skynet/archive/2009/12/01/304352.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Tue, 01 Dec 2009 04:05:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/12/01/304352.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/304352.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/12/01/304352.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/304352.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/304352.html</trackback:ping><description><![CDATA[<strong><br /> 杞嚜銆?/strong>http://www.cnblogs.com/coderzh/archive/2008/09/22/1296195.html<strong>銆?br /> </strong>浣滆咃細<a target="_blank" >CoderZh</a>錛?a target="_blank" >CoderZh鐨勬妧鏈崥瀹?- 鍗氬鍥?/a>錛?br /> 鍑哄錛?a target="_blank" >http://coderzh.cnblogs.com/</a><br /> # 鎴戣繖 鍔犱簡浜?涓漢鐞嗚В鐨?璇存槑<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008000;">#</span><span style="color: #008000;">encoding:UTF-8</span><span style="color: #008000;"><br /> #</span><span style="color: #008000;"> [閫掑綊] - 鍗曟潯璺?nbsp;鑷笅寰涓婃帓搴?nbsp;</span><span style="color: #008000;"><br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> heap_adjust(data, s, m):<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> s </span><span style="color: #000000;">></span><span style="color: #000000;"> m:</span><span style="color: #0000ff;">return</span><span style="color: #000000;"><br />    <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 澹版槑 棰勮鐖惰妭鐐逛綅緗?/span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;"><br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> [宸瀛愯妭鐐瑰?nbsp;澶т簬 鐖惰妭鐐瑰?nbsp; :  棰勮鐖惰妭鐐逛綅緗?nbsp;涓?nbsp;宸﹀瓙鑺傜偣浣嶇疆</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> data[</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">></span><span style="color: #000000;"> data[temp]: temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">s</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> [鍙砞瀛愯妭鐐瑰?nbsp;澶т簬 棰勮鐖惰妭鐐?nbsp;: 棰勮鐖惰妭鐐逛綅緗?nbsp;涓?nbsp;鍙沖瓙鑺傜偣浣嶇疆</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> s </span><span style="color: #000000;"><=</span><span style="color: #000000;"> m </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;"> </span><span style="color: #0000ff;">and</span><span style="color: #000000;"> data[</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">s] </span><span style="color: #000000;">></span><span style="color: #000000;"> data[temp]: temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> s<br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 浜ゆ崲鍊?nbsp;婊¤凍 鍫嗙壒鎬?nbsp;姝や負 [ 鐖惰妭鐐?nbsp;灝忎簬 瀛愯妭鐐?nbsp; ]</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> temp </span><span style="color: #000000;"><></span><span style="color: #000000;"> s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">:<br />         data[s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">], data[temp] </span><span style="color: #000000;">=</span><span style="color: #000000;"> data[temp], data[s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">]<br />         heap_adjust(data, temp </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">, m)<br /> <br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> heap_sort(data):<br />     m </span><span style="color: #000000;">=</span><span style="color: #000000;"> len(data) </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"><br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 鏋勫緩 鍫嗘爲</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #008000;">#</span><span style="color: #008000;"> 嫻嬭瘯鏁版嵁 [3,2,1] 鏁扮粍鍊間負 鎵浠ラ潪搴曞眰鍙惰妭鐐?/span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> range(m , 0, </span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />         heap_adjust(data, i, len(data))<br /> <br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 浠庡爢鏍戜腑 [鍑烘爤] 鎺掑簭杈撳嚭</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #008000;">#</span><span style="color: #008000;"> 嫻嬭瘯鏁版嵁 [5, 4, 3, 2]</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    data[0], data[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> data[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">], data[0]<br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> n </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> range(len(data) </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />         heap_adjust(data, </span><span style="color: #000000;">1</span><span style="color: #000000;">, n)<br />         data[0], data[n </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> data[n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">], data[0]<br /> <br /> <br /> <br /> data</span><span style="color: #000000;">=</span><span style="color: #000000;">[</span><span style="color: #000000;">2</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">868</span><span style="color: #000000;">,</span><span style="color: #000000;">9</span><span style="color: #000000;">,</span><span style="color: #000000;">8</span><span style="color: #000000;">,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]<br /> heap_sort(data)<br /> </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> data<br /> </span><span style="color: #008000;">#</span><span style="color: #008000;"> [-1, 2, 3, 3, 6, 8, 9, 868]</span></div> <strong><br /> <br /> 杞嚜 銆?</strong>http://www.cppblog.com/guogangj/<strong> 銆?br /> 鍫嗗瓨鍌?/strong> <br /> <img src="http://m.tkk7.com/images/blogjava_net/skynet/42926/o_ds_binary_heap_array.png" alt="" border="0" /><br /> <br /> <br /> <strong>鍫?鍏ユ爤 澶嶆潅搴︿負Ο(logn)</strong><br /> <img src="http://m.tkk7.com/images/blogjava_net/skynet/42926/r_ds_binary_heap_insert.png" alt="" border="0" /><br /> <br /> <br /> <br /> <br /> <strong>鍫?鍑烘爤  Ο(logn)</strong><br /> <br /> <img src="http://m.tkk7.com/images/blogjava_net/skynet/42926/o_ds_binary_heap_dequeue.png" alt="" border="0" /><br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/304352.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-12-01 12:05 <a href="http://m.tkk7.com/Skynet/archive/2009/12/01/304352.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>璺熸垜涓璧峰 - 綆楁硶瀵艱 - 閫掑綊寮?/title><link>http://m.tkk7.com/Skynet/archive/2009/11/25/303704.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Wed, 25 Nov 2009 15:09:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/11/25/303704.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/303704.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/11/25/303704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/303704.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/303704.html</trackback:ping><description><![CDATA[<br /> 鐪嬬浜岃妭  - 閫掑綊鏍戞柟娉?錛?br /> 紿佸彂濂囨兂 鑳藉惁 浣跨敤 txt 鏋勯犲嚭 閫掑綊榪囩▼  <br /> 榪樻槸鏈?涓婃鎻愬埌鐨?閫掑綊鏂規硶 鍒嗘不鎺掑簭<br /> <br /> <br /> <br />  <br /> <br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008000;">#</span><span style="color: #008000;"> encoding: utf-8  </span><span style="color: #008000;"><br /> </span><span style="color: #000000;">arr</span><span style="color: #000000;">=</span><span style="color: #000000;">[]<br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> printTree():<br />     ac </span><span style="color: #000000;">=</span><span style="color: #000000;"> []<br />     ii </span><span style="color: #000000;">=</span><span style="color: #000000;"> 0 <br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> r </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> arr :<br />         c,ss,cc </span><span style="color: #000000;">=</span><span style="color: #000000;"> r <br />         sc </span><span style="color: #000000;">=</span><span style="color: #000000;"> [</span><span style="color: #800000;">'</span><span style="color: #800000;"> </span><span style="color: #800000;">'</span><span style="color: #000000;"> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> xrange(cc</span><span style="color: #000000;">*</span><span style="color: #000000;">(c</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">))]<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> xrange(len(sc)) :<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> i </span><span style="color: #000000;">%</span><span style="color: #000000;"> cc </span><span style="color: #000000;">==</span><span style="color: #000000;"> 0 : sc[i]</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">鈹?/span><span style="color: #800000;">"</span><span style="color: #000000;"> <br />         </span><span style="color: #008000;">#</span><span style="color: #008000;">print "ci %s ii %s = %s "%(ci,ii,ii < ci)</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ii</span><span style="color: #000000;">>=</span><span style="color: #000000;">c  : <br />             sc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">""</span><span style="color: #000000;">.join(sc)</span><span style="color: #000000;">+</span><span style="color: #800000;">"</span><span style="color: #800000;">鈹溾攢</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">ss</span><span style="color: #000000;">+</span><span style="color: #800000;">'</span><span style="color: #800000;">  </span><span style="color: #800000;">'</span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> :<br />             sc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">""</span><span style="color: #000000;">.join(sc)</span><span style="color: #000000;">+</span><span style="color: #800000;">"</span><span style="color: #800000;">鈹斺攢</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">ss<br />         ii </span><span style="color: #000000;">=</span><span style="color: #000000;"> c<br />         ac.append( sc )<br />         <br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> r </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> ac[::</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">] :<br />         </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> r<br />     <br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> MERGE(A,p,q,r):<br />     </span><span style="color: #008000;">#</span><span style="color: #008000;">print "%s:%s - %s:%s" % (p,q+1,q+1,r+1)</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> p</span><span style="color: #000000;">==</span><span style="color: #000000;">q : L </span><span style="color: #000000;">=</span><span style="color: #000000;"> [A[p],</span><span style="color: #000000;">10</span><span style="color: #000000;">**</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br />     </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> : L </span><span style="color: #000000;">=</span><span style="color: #000000;"> A[p:q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">+</span><span style="color: #000000;">[</span><span style="color: #000000;">10</span><span style="color: #000000;">**</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br /> <br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">==</span><span style="color: #000000;">r : R </span><span style="color: #000000;">=</span><span style="color: #000000;"> [A[r],</span><span style="color: #000000;">10</span><span style="color: #000000;">**</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br />     </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> : R </span><span style="color: #000000;">=</span><span style="color: #000000;"> A[q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">:r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">+</span><span style="color: #000000;">[</span><span style="color: #000000;">10</span><span style="color: #000000;">*</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br /> <br />     i </span><span style="color: #000000;">=</span><span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> 0<br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> k </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> xrange(p,r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />         </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> L[i]</span><span style="color: #000000;"><</span><span style="color: #000000;">R[j] :<br />             A[k]</span><span style="color: #000000;">=</span><span style="color: #000000;">L[i]<br />             i</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />             A[k]</span><span style="color: #000000;">=</span><span style="color: #000000;">R[j]<br />             j</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> print "%s:%s = %s \n%s:%s = %s\n\n%s" % ( p,q, L , q+1,r,R, A)</span><span style="color: #008000;"><br /> </span><span style="color: #000000;"><br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> MERGE_SORT(A,p,r,c</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> p</span><span style="color: #000000;"><</span><span style="color: #000000;">r:<br />         q </span><span style="color: #000000;">=</span><span style="color: #000000;"> (p</span><span style="color: #000000;">+</span><span style="color: #000000;">r)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;"><br />         MERGE_SORT(A,p,q,c</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         MERGE_SORT(A,q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,r,c</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         arr.append( (c,</span><span style="color: #800000;">"</span><span style="color: #800000;">%s - %s</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">%</span><span style="color: #000000;"> ( A[p:q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">],A[q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">:r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">]) , </span><span style="color: #000000;">3</span><span style="color: #000000;"> ) )<br />         </span><span style="color: #008000;">#</span><span style="color: #008000;"> Debugging(A,p,q,r, sc )</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        MERGE(A,p,q,r)<br /> <br /> A</span><span style="color: #000000;">=</span><span style="color: #000000;">[</span><span style="color: #000000;">5</span><span style="color: #000000;">,</span><span style="color: #000000;">2</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">,</span><span style="color: #000000;">4</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">2</span><span style="color: #000000;">,</span><span style="color: #000000;">6</span><span style="color: #000000;">]<br /> MERGE_SORT(A,0,len(A)</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br /> </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> A<br /> printTree() <br /> <br /> <br /> </span></div> <br /> <br /> 杈撳嚭 錛堥噸涓嬪線涓婄湅  杈撳嚭 鎺掑簭榪囩▼ 錛屾垜灝變笉澶氳浜?搴旇寰堝ソ鐞嗚В浜嗭紒錛侊級錛?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">[1, 2, 2, 3, 4, 5, 6, 7]<br /> 鈹溾攢[2, 4, 5, 7] - [1, 2, 3, 6]<br /> 鈹?nbsp; 鈹溾攢[1, 3] - [2, 6]<br /> 鈹?nbsp; 鈹?nbsp; 鈹溾攢[2] - [6]<br /> 鈹?nbsp; 鈹?nbsp; 鈹斺攢[1] - [3]<br /> 鈹?nbsp; 鈹溾攢[2, 5] - [4, 7]<br /> 鈹?nbsp; 鈹?nbsp; 鈹溾攢[7] - [4]<br /> 鈹?nbsp; 鈹?nbsp; 鈹斺攢[5] - [2]</span></div> <br /> <br /> <br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/303704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-11-25 23:09 <a href="http://m.tkk7.com/Skynet/archive/2009/11/25/303704.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>璺熸垜涓璧峰 - 綆楁硶瀵艱 - 鍑芥暟鐨勫闀?/title><link>http://m.tkk7.com/Skynet/archive/2009/11/23/303404.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Mon, 23 Nov 2009 15:33:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/11/23/303404.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/303404.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/11/23/303404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/303404.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/303404.html</trackback:ping><description><![CDATA[<br /> <img alt="" src="http://m.tkk7.com/images/blogjava_net/skynet/ywl.jpg" border="0" width="161" height="186" /><br /> <strong><br /> <br /> <br /> 3.1 娓愯繎鍙?/strong><br /> <br /> 娓愯繎鑼冨洿      f(n) = θ(g(n))  ~a=b      <br /> 娓愯繎涓婄晫      f(n) = Ο(g(n))  ~a<=b    0≤f(n)≤cg(n) <br /> 娓愯繎涓嬬晫      f(n) = Ω(g(n))  ~a>=b    0≤cg(n)≤f(n)<br /> 闈炴笎榪戜笂鐣?nbsp;  f(n) = o(g(n))    ~a<b     0≤f(n)<cg(n)   =>lim[n<=∞](f(n)/g(n))=0<br /> 闈炴笎榪戜笅鐣?nbsp;  f(n) = ω(g(n))   ~a>b     0≤cg(n)<f(n)   =>lim[n<=∞](f(n)/g(n))=0<br /> <br /> <br /> 娓愯繎鍙蜂嬌鐢紙鐩墠鎴戣兘鐞嗚В鍒扮殑錛侊級: <br /> 褰撴笎榪戠鍙峰嚭鐜板湪鏌愪釜鍏紡涓椂錛屾垜浠皢鍏惰В閲婁負涓涓笉鍦ㄤ箮鍏跺悕縐扮殑緗插悕鍑芥暟銆?br /> 渚嬶細2n^2+3n+1 = 2n^2+θ(n) 錛岃繖縐嶇敤娉曟湁鍔╀簬灞忚斀鏃犲叧绱ц鐨勭粏鑺傦紝濡備綆闃墮」銆傘?br /> <br /> 鈭慬1≤k≤n]O(i)<br /> <br /> <br /> <strong>3.2 鏍囧噯璁板彿鍜屽父閲忓嚱鏁?/strong><br /> 鍗曡皟鎬?錛?鍗曡皟閫掑 錛?鍗曡皟閫掑噺<br /> # 浼犺涓殑騫挎挱浣撴搷鍘熸潵鏄?涓婁笅鍙栨暣鍟?錛?鍛靛懙<br /> 涓嬪彇鏁達紝涓婂彇鏁?錛?x-1 < 鈹擷鈹?<=  x   <=  鈹孹鈹?nbsp; <  x+1<br /> <br /> 鍙栨ā榪愮畻  a mod n  = a-鈹攁/n鈹榥 <br /> <br /> 澶氶」寮?nbsp; p(n) = 鈭慬0≤i≤d] a.i n^i<br /> <br /> 鎸囨暟 (a^m)^n = a^(m*n)   ;  a^m*a^n = a^(m+n)<br /> <br /> # 鎸囨暟涓殑 鐗規畩絎﹀彿 e <br /> # e涓嶈瀵箈寰垎鍑犳錛岀粨鏋滈兘榪樻槸e錛侀毦鎬暟瀛︾郴瀛︾敓浼氱敤e姣斿柣鍧氬畾涓嶇Щ鐨勭埍鎯咃紒 <br /> # 鏁板涓殑鐖辨儏絎﹀彿 e 鍝堝搱錛侊紒<br /> e <span name="whlm" id="whlm">= lim[</span>n≤∞<span name="whlm" id="whlm">](1+1/n)^n  </span><br /> <br /> <br /> 瀵規暟 <br /> lgn = log_2(n)<br /> lnn=log_e(n)<br /> lg^k(n)=(lgn)^k<br /> lg lg n = lg(lgn)<br /> <br /> <br /> 闃朵箻  n!<br /> <br /> <br /> 鍑芥暟榪唬<br /> <br /> <br /> 鏂愭嘗閭e垏 <br /> F0 = 0<br /> F1 = 1<br /> ..<br /> Fi = Fi-1+Fi-2<br />  <br /> <br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/303404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-11-23 23:33 <a href="http://m.tkk7.com/Skynet/archive/2009/11/23/303404.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鎺ㄨ崘綆楁硶 - 鐨皵閫婂嚱鏁?/title><link>http://m.tkk7.com/Skynet/archive/2009/11/22/303271.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Sun, 22 Nov 2009 15:56:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/11/22/303271.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/303271.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/11/22/303271.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/303271.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/303271.html</trackback:ping><description><![CDATA[<br /> <br /> 鍏紡錛?br /> <img src="http://m.tkk7.com/images/blogjava_net/skynet/113f06f5bbf.jpg" alt="" border="0" height="171" width="426" /><br /> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">鏁版嵁 elt 娓呮礂鍚?txt)</span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> 涓鑸?nbsp;user 鍜?nbsp;item 鍒嗗煎寲 </span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> 姣斿 鐢ㄦ埛涓嬭澆錛屾敹钘忥紝璇曞惉 鏌恑tem 絳夌瓑</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">user    items    score<br /> <img src="http://m.tkk7.com/Images/dot.gif" alt="" />.<br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 緇撴灉杈撳嚭 (bdb)</span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> user    item1:score1,item2:score2,item3:score3<img src="http://m.tkk7.com/Images/dot.gif" alt="" /><img src="http://m.tkk7.com/Images/dot.gif" alt="" />.</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> python</span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);">EOF<br /> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> bsddb<br /> db </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> bsddb.hashopen(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-items.db</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> row </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> open(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-item-sc.txt</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">):<br />     row</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">row.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">\n</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)[0]<br />     dr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> row.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> db.has_key(dr[0]) : db[dr[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br />     </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> : db[dr[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db[dr[0]]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /> <br /> db.close()<br /> EOF<br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 緇撴灉杈撳嚭 (txt)</span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> user    user     score</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><img src="http://m.tkk7.com/Images/dot.gif" alt="" /><br /> <br /> python</span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);">EOF<br /> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> bsddb<br /> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> math </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"><br /> db </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> bsddb.hashopen(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-items.db</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> ps(u1,u2):<br />     um1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> db[u1].split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) :<br />         v</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />         um1[v[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">float(v[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br />     um2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />     si</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[]<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> db[u2].split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) :<br />         v</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />         um2[v[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">float(v[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> um1.has_key( v[0] ) : si.append(v[0])<br />     n </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> len(si)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> n </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);"> :</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> None<br />     <br />     sum1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum( [um1[it] </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si] )<br />     sum2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum( [um2[it] </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si] )<br />     <br />     sum1Sq</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum([ pow(um1[it],</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si])<br />     sum2Sq</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum([ pow(um2[it],</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si])<br />     <br />     pSum </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sum( [ um1[it]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">um2[it] </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si ] )<br /> <br />     num </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pSum </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);"> (sum1</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">sum2</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">n)<br />     den </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqrt( (sum1Sq</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">pow(sum1,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">n )</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">( sum2Sq</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">pow(sum2,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">n ) )<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> den</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> None<br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> num</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">den<br /> <br /> fc </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-user-sc.txt</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">w</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> xrange(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">43381</span><span style="color: rgb(0, 0, 0);">):<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> j </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> xrange(i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">43381</span><span style="color: rgb(0, 0, 0);">):<br />         sc </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ps(str(i),str(j))  <br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> sc </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> None: </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);">fc, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%s\t%s\t%s</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">(i,j,sc)      <br /> <br /> fc.close()<br /> <br /> EOF<br /> <br /> <br /> <br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 嫻嬭瘯浣跨敤</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">python</span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);">EOF<br /> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> bsddb<br /> db </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> bsddb.hashopen(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-items.db</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> db[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /> EOF<br /> <br /> </span><span style="color: rgb(0, 0, 0);">25</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 0);">30604</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> um1[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">468</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">],um1[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">471</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /> </span><span style="color: rgb(0, 0, 0);">2.0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br /> (Pdb) </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> um2[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">468</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">],um2[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">471</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /> </span><span style="color: rgb(0, 0, 0);">2.0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br /> <br /> <br /> <br /> </span></div> <br /> <br /> <br /> 濡傛灉瀵瑰ぇ瀹跺 鎺ㄨ崘鏈変竴浜涗簡瑙o紝鏁版嵁鑳藉埌 鐢ㄦ埛涓庣敤鎴峰叧緋?鍒嗗煎寲) ,鏄兘騫插緢澶氫簨鎯呬簡銆?br /> 姣斿錛?br />   1. 棣栧厛寰楀埌鏌愮敤鎴風浉榪戝害鏈楂樼殑鍑犱綅媧昏穬鐢ㄦ埛錛岀湅榪欏嚑浣嶇敤鎴峰湪鐪嬩粈涔堬紝鍚粈涔?鐒跺悗鎺ㄨ崘鍑哄幓 <br /> <br /> 鎵╁睍錛?br />   鎶婂垵濮嬪?鍙嶈繃鏉?nbsp; item  user  score 錛岀劧鍚庣粺璁″嚭 item 鍜?item 涔嬮棿鐨勫叧緋?銆?br />   褰?娑堣垂鏌愪竴浜у搧 錛岄┈涓婃帹鑽愬嚭 鍏朵粬鐨勭浉榪戠殑浜у搧 錛堟檪鏃舵帹鑽愶級<br /> <br />   <br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/303271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-11-22 23:56 <a href="http://m.tkk7.com/Skynet/archive/2009/11/22/303271.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>璺熸垜涓璧峰 - 綆楁硶瀵艱 - 鍒嗘不綆楁硶http://m.tkk7.com/Skynet/archive/2009/11/22/303269.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Sun, 22 Nov 2009 15:26:00 GMThttp://m.tkk7.com/Skynet/archive/2009/11/22/303269.htmlhttp://m.tkk7.com/Skynet/comments/303269.htmlhttp://m.tkk7.com/Skynet/archive/2009/11/22/303269.html#Feedback0http://m.tkk7.com/Skynet/comments/commentRss/303269.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/303269.html
def MERGE(A,p,q,r):
    
print "%s:%s - %s:%s" % (p,q+1,q+1,r+1)
    
if p==q : L = [A[p],10**10]
    
else : L = A[p:q+1]+[10**10]

    
if q+1==r : R = [A[r],10**10]
    
else : R = A[q+1:r+1]+[10*10]

    i 
= j = 0
    
for k in xrange(p,r+1):
        
if L[i]<R[j] :
            A[k]
=L[i]
            i
+=1
        
else:
            A[k]
=R[j]
            j
+=1
    
# print "%s:%s = %s \n%s:%s = %s\n\n%s" % ( p,q, L , q+1,r,R, A)


def Debugging(A,p,q,r,c):
    
print "%s\t%s:%s - %s:%s" % (c,p,q,q+1,r)

def MERGE_SORT(A,p,r,c=1):
    
if p<r:
        q 
= (p+r)/2
        MERGE_SORT(A,p,q,c
+1)
        MERGE_SORT(A,q
+1,r,c+1)
        
#Debugging(A,p,q,r,c)
        MERGE(A,p,q,r)

A
=[5,2,7,4,1,3,2,6]
print A
MERGE_SORT(A,0,len(A)
-1)
print A

緇撴灉杈撳嚭銆嬨?br /> python 2f.py
[5, 2, 7, 4, 1, 3, 2, 6]
[1, 2, 2, 3, 4, 5, 6, 7]


鍒嗕韓浜涚粏鑺傦細綆楁硶騫朵笉闅撅紝浣嗙‘瀹炲啓浜嗗緢涔咃紝璋冭瘯璁╂垜寰堥儊闂楓?br /> 鐩村埌鍐欎簡 def Debugging  鐩祴錛?br />
python 2f.py
3       0:0 - 1:1
3       2:2 - 3:3
2       0:1 - 2:3
3       4:4 - 5:5
3       6:6 - 7:7
2       4:5 - 6:7
1       0:3 - 4:7
鐪?姣忓眰 瀵規暟緇勭殑 鏁扮粍涓嬫爣鍙栧?錛?
鍦?python 涓綋
arr = [1,2,3,4] 鎴戝笇鏈涜兘鍙栧嚭 [2,3] 鏄?arr[1:3] 鏄渶鍚庝竴浣嶄笉璁$畻鍦ㄥ唴鐨?br /> 鏈鍏稿瀷鐨?nbsp; arr[0,1]  == [1]











]]>
鐞嗚В楂樻柉鍑芥暟錛屼互鍙婂湪鎺ㄨ崘綆楁硶涓殑搴旂敤http://m.tkk7.com/Skynet/archive/2009/11/19/302901.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Thu, 19 Nov 2009 03:14:00 GMThttp://m.tkk7.com/Skynet/archive/2009/11/19/302901.htmlhttp://m.tkk7.com/Skynet/comments/302901.htmlhttp://m.tkk7.com/Skynet/archive/2009/11/19/302901.html#Feedback1http://m.tkk7.com/Skynet/comments/commentRss/302901.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/302901.html 榪欐槸涓涓緢鏈夌敤鐨?鍏紡姣斿錛氱敤鎴鋒秷璐瑰垎鍊兼潈閲?錛?浜у搧鍏寵仈鍒嗗兼潈閲?絳夌瓑


鍏紡 

鍦?http://www.wolframalpha.com 涓〃紺?:
e = (1+1/n) ^n
a*e^(-(x-b)^2/c^2) 
a 宄板兼渶澶у?
b 宄板紉杞村亸縐婚噺
c 寮у害璺ㄥ害


  =  1*e^(-(x-1)^2/1^2)




淇敼 宄板?a = 2



榪欓噷 灝?涓嶄竴涓灞曠幇 b 宄板紉杞村亸縐婚噺 錛?c 寮у害璺ㄥ害 浜?澶у鍙互 鍘?wolframalpha 鑷繁鍘誨皾璇?br />


瀹炰緥1 涓庢椂闂存湁鍏崇殑閫掑噺 錛?br />

import math
def gaussian(x,peak=1.0,axis=1.0,span=1.0):
    
return peak*math.e**(-(x-axis)**2/(span)**2 )


璺ㄥ害 c 鍙傝?
= 1 : 鍦?.5 闄勪歡鎬ュ墽琛板噺
= 2 : 4
= 18 :30 # 榪欎釜鏁?nbsp;琛板噺緇熻 涓涓湀 涓嶉敊
= 55 :90 # 琛板噺緇熻 涓涓搴?nbsp;涓嶉敊


#綆鍗曞簲鐢?nbsp;
娑堣垂1嬈″緱宄板?鍒?nbsp;嫻忚1嬈″嘲鍊?鍒?nbsp;
緇熻鏌愮敤鎴峰搴﹀緱鍒?br /> 鏁版嵁錛氬湪鍓?0澶╂祻瑙?0嬈★紝娑堣垂1嬈?nbsp;錛屽墠11澶╂祻瑙?嬈?nbsp;
d10 
= gaussian(10,span=55.0)
d11 
= gaussian(11,span=55.0)
print d10*10*2+d10*4*1+d11*5*2
#緇撴灉 33.0407089687


鍊掔殑楂樻柉銆錛嶃瀹炰緥2  錛?br /> 鍏紡 =

 
鐩殑銆涓庢鏁版湁鍏崇殑浜у搧鍒嗗煎寲
#鐢ㄦ埛 瀵?nbsp;鏌愪駭鍝?nbsp;鍒嗗煎寲
#
 姣斿 鏌愮敤鎴?nbsp;鐢ㄨ繃鏌愪駭鍝?nbsp;n嬈★紝鎴戝笇鏈?nbsp;n 鏃犻檺澶ф槸涓涓?nbsp;娓愯繘鏌愪釜鍊?nbsp;鑰屼笉鏄拰 n 鏃犻檺閫掑鐨?/span>
#
涓嬮潰鐨?nbsp;fun 緇撴灉鏄?nbsp; 1.6 锝?nbsp;10 鍒嗗肩洿鎺ョ殑鍖哄煙, 涔熷氨鏄?nbsp;浼犺涓殑 浜у搧鎰熷叴瓚?nbsp;“10鍒嗗埗” 綆鏄撶増   
def gs(x,peak=9.0,axis=-2.0,span=11.0):
    
return "%.4f" % (-1*peak*math.e**(-(x-axis)**2/(span)**2 )+peak+1)


>>> gs(1)
'1.6451'
>>> gs(2)
'2.1148'
>>> gs(3)
'2.6800'
>>> gs(4)
'3.3161'
>>> gs(5)
'3.9970'
>>> gs(6)
'4.6969'
>>> gs(60)
'10.0000'






]]>
璺熸垜涓璧峰 - 綆楁硶瀵艱 - 鎻掑叆鎺掑簭http://m.tkk7.com/Skynet/archive/2009/11/18/302864.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Wed, 18 Nov 2009 15:29:00 GMThttp://m.tkk7.com/Skynet/archive/2009/11/18/302864.htmlhttp://m.tkk7.com/Skynet/comments/302864.htmlhttp://m.tkk7.com/Skynet/archive/2009/11/18/302864.html#Feedback0http://m.tkk7.com/Skynet/comments/commentRss/302864.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/302864.html#  鎻掑叆鎺掑簭                -         澶嶆潅搴?/span>
def insertion_sort(arr):             # 1
    for j in xrange( 1,len(arr) ):   # n-1
        key = arr[j]                 # n-1
        i=j-1                        # n-1
        while i>=and arr[i]> key : # n(n-1)/2
            arr[i+1= arr[i]        # n(n-1)/2
            i=i-1                    # n(n-1)/2
        arr[i+1= key               # n-1
        print arr                    # n-1

楠岃瘉緇撴灉 :
>>> arr=[5,2,4,6,1,3]
>>> insertion_sort(arr)
[2, 5, 4, 6, 1, 3]
[2, 4, 5, 6, 1, 3]
[2, 4, 5, 6, 1, 3]
[1, 2, 4, 5, 6, 3]
[1, 2, 3, 4, 5, 6]





楠岃瘉澶嶆潅搴︼細
z = 5(n-1)+1+3n(n-1)/2
鎴戜滑嫻嬭瘯鏁版嵁 涓?nbsp; n=6 
褰撴暟鎹瀬绔儏鍐靛氨鏄渶瑕佸叏閮ㄩ噸鏂版帓鍒?
灝辨槸 [6,5,4,3,2,1] 瑕佹帓鍑?[1,2,3,4,5,6] 榪欐牱
>> z = 71

涓縐嶆瘮杈冪鐨?楠岃瘉鏂規硶 渚涘ぇ瀹舵媿鐮?:
def insertion_sort(arr):         
    ii
=0
    ii
+=1
    
for j in xrange( 1,len(arr) ):
        ii
+=1
        
        key 
= arr[j]            
        ii
+=1
        
        i
=j-1                
        ii
+=1
        
        
while i>=and arr[i]> key :    
            ii
+=1
            
            arr[i
+1= arr[i]    
            ii
+=1
            
            i
=i-1            
            ii
+=1
            
        arr[i
+1= key            
        ii
+=1
        
        
print arr            
        ii
+=1

    
print "----",str(ii)

>>> arr=[6,5,4,3,2,1]
>>> insertion_sort(arr)
[5, 6, 4, 3, 2, 1]
[4, 5, 6, 3, 2, 1]
[3, 4, 5, 6, 2, 1]
[2, 3, 4, 5, 6, 1]
[1, 2, 3, 4, 5, 6]
---- 71  #澶嶆潅搴﹂獙璇佷負 71


緗楀棪涓?n(n-1)/2
鏋佺鎯呭喌涓?nbsp;
i=1 ; j 闇瑕佹尓鍔?1嬈?br /> i=2 ; j 鎸姩 1+2嬈?br /> i=3 ; j 鎸姩 1+2+3嬈?br /> ....
i=n ; j 鎸姩 1+2....+n

鎴戜滑鍙堟壘鍒?(1+n)+(2+n-1)+(3+n-2).... = (1+n)n/2
鎴戜滑榪?鐨?i 鏄粠 2 嬈″紑濮嬬殑 涔熷氨鏄  (n-1)n/2 浜?
def tn(ii):
    ti
=0
    
for i in xrange(ii) :
        
for j in xrange(i) :
            ti
+=1
    
print ti


print tn(2)  #1 = 1
print tn(3)  #3 = 1+2
print tn(4)  #6 = 1+2+3
..








]]>
綆楁硶 澶嶆潅搴﹀垎鏋愬浘http://m.tkk7.com/Skynet/archive/2009/11/17/302742.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Tue, 17 Nov 2009 15:12:00 GMThttp://m.tkk7.com/Skynet/archive/2009/11/17/302742.htmlhttp://m.tkk7.com/Skynet/comments/302742.htmlhttp://m.tkk7.com/Skynet/archive/2009/11/17/302742.html#Feedback0http://m.tkk7.com/Skynet/comments/commentRss/302742.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/302742.html 錛堝鏋滃ぇ瀹舵湁鍏磋叮灝辮窡鎴戜竴璧?- 銆婄畻娉曞璁恒?/strong>錛屼篃鏈涘ぇ瀹剁洃鐫f垜鑳芥瘡澶╂嬁鍑轟竴灝忔椂鍜屽ぇ瀹跺垎浜畻娉?綆楁硶浠g爜鎴戜細灝介噺浣跨敤 py 鍜?瑙e喅涓浜涘垎鏋愭棩蹇楃殑搴旂敤涓婇潬 錛堝叾瀹烇紝涓婇潰璐瑰姴鐨?浜屽弶hash 鏄負浜嗗垎鏋愭棩蹇椾腑 - 甯屾湜鑳藉疄鐜?澶氫釜澶ф枃浠朵笉闇瑕佸悎騫跺氨鑳芥牴鎹煇鍒楁帓搴忚緭鍑? 鐩墠鐨勮В鍐沖姙娉?find .. -exec cat {} \; | perl |sort 鐨勭鏂規硶 錛?

1. 綆楁硶鍦ㄨ綆椾腑鐨勪綔鐢?錛堢瑪璁幫級 :
   綆楁硶(algorithm):灝辨槸瀹氫箟鑹ソ鐨勮綆楄繃紼嬶紝瀹冨彇涓涓垨涓緇勪綔涓鴻緭鍑猴紝騫朵駭鐢熶竴涓垨涓緇勮嚜浣滀負杈撳嚭銆?br />
涓浜涘嚱鏁拌繍琛岀駭鍒?nbsp; # http://www.wolframalpha.com/ 鍑芥暟閮藉彲鍦ㄧ綉绔欓噷榪愯
榪欓噷 n=涓浜挎潯鏁版嵁 :
log_2(n)     30
n^0.5        31622
n            10^9
n*log_2(n)   2.9^10
n^2          10^18
n^3          10^27
2^n          鏃犵┓
n!           10^362880


闇瑕佺煡閬撶殑澶嶆潅搴?- 鍦ㄦ煇涓涓復鐣岀偣鍚?鍚堝茍浼氬埆鎻掑叆瑕佸揩鐨?br />   鎻掑叆鎺掑簭  澶嶆潅搴?n^2    http://www.wolframalpha.com/input/?i=n^2
  鍚堝茍鎺掑簭  澶嶆潅搴?n*log_2(n)  http://www.wolframalpha.com/input/?i=n*log_2%28n%29+

緗戜笂鐨勬煡鎵懼埌鐨勪竴浜涘悕縐幫細鍙傝?http://www.51testing.com/?uid-130868-action-viewspace-itemid-66729
1.1紼沖畾鎺掑簭  闈炵ǔ瀹氭帓搴?-
  紼沖畾鎺掑簭鏄墍鏈夌浉絳夌殑鏁扮粡榪囨煇縐嶆帓搴忔柟娉曞悗錛屼粛鑳戒繚鎸佸畠浠湪鎺掑簭涔嬪墠鐨勭浉瀵規搴忥紝銆傚弽涔嬶紝灝辨槸闈炵ǔ瀹氱殑鎺掑簭銆?br /> 1.2鍐呮帓搴?nbsp; 澶栨帓搴?/strong>
  鍦ㄦ帓搴忚繃紼嬩腑錛屾墍鏈夐渶瑕佹帓搴忕殑鏁伴兘鍦ㄥ唴瀛橈紝騫跺湪鍐呭瓨涓皟鏁村畠浠殑瀛樺偍欏哄簭錛岀О涓哄唴鎺掑簭錛?鍦ㄦ帓搴忚繃紼嬩腑錛屽彧鏈夐儴鍒嗘暟琚皟鍏ュ唴瀛橈紝騫跺熷姪鍐呭瓨璋冩暣鏁板湪澶栧瓨涓殑瀛樻斁欏哄簭鎺掑簭鏂規硶縐頒負澶栨帓搴忋?br /> 1.3綆楁硶鐨勬椂闂村鏉傚害  絀洪棿澶嶆潅搴?/strong>
  鎵璋撶畻娉曠殑鏃墮棿澶嶆潅搴︼紝鏄寚鎵ц綆楁硶鎵闇瑕佺殑璁$畻宸ヤ綔閲忋?涓涓畻娉曠殑絀洪棿澶嶆潅搴︼紝涓鑸槸鎸囨墽琛岃繖涓畻娉曟墍闇瑕佺殑鍐呭瓨絀洪棿銆?br />

鍑犵甯歌鐨勭畻娉曞鏉傚害錛?br /> 2.1鍐掓場鎺掑簭 錛圔ubble Sort錛?nbsp;   O(n^2)
2.2閫夋嫨鎺掑簭 錛圫election Sort錛?O(n^2 )
2.3鎻掑叆鎺掑簭 錛圛nsertion Sort錛?O(n^2)
2.4鍫嗘帓搴?nbsp;   O( nlog(n) )
2.5褰掑茍鎺掑簭  O( nlog_2(n)  )
2.6蹇熸帓搴?nbsp; 鏈濂?O( nlog_2(n) ) 鏈鍧?O(n^2)




wiki 鍙傝?錛歨ttp://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95
絀╁畾鐨?/strong>
  • 鍐掓場鎺掑簭錛坆ubble sort錛?鈥?O(n2)
  • 鎻掑叆鎺掑簭 錛坕nsertion sort錛夆?O(n2)
  • 璦堟暩鎺掑簭 (counting sort) 鈥?O(n+k); 闇瑕?O(n+k) 欏嶅絀洪枔
  • 鍚堜降鎺掑簭 錛坢erge sort錛夆?O(n log n); 闇瑕?O(n) 欏嶅絀洪枔
  • 鍘熷湴鍚堜降鎺掑簭 鈥?O(n2)
  • 浜屽弶鎺掑簭妯?/a>鎺掑簭 錛圔inary tree sort錛?鈥?O(n log n)鏈熸湜鏅傞枔; O(n2)鏈澹炴檪闁? 闇瑕?O(n) 欏嶅絀洪枔
  • 櫬垮發鎺掑簭 (Pigeonhole sort) 鈥?O(n+k); 闇瑕?O(k) 欏嶅絀洪枔
  • 鍩烘暩鎺掑簭 錛坮adix sort錛夆?O(n·k); 闇瑕?O(n) 欏嶅絀洪枔
  • [涓嶇┅瀹?/span>
    • 閬告搰鎺掑簭 錛坰election sort錛夆?O(n2)
    • 甯岀埦鎺掑簭 錛坰hell sort錛夆?O(n log n) 濡傛灉浣跨敤鏈浣崇殑鐝懼湪鐗堟湰
    • 鍫嗘帓搴?/a> 錛坔eapsort錛夆?O(n log n)
    • 蹇熸帓搴?/a> 錛坬uicksort錛夆?O(n log n) 鏈熸湜鏅傞枔, O(n2) 鏈澹炴儏娉? 灝嶆柤澶х殑銆佷簜鏁鎬覆鍒椾竴鑸浉淇℃槸鏈蹇殑宸茬煡鎺掑簭
    • [涓嶅鐢ㄧ殑鎺掑簭婕旂畻娉?/span>
      • Bogo鎺掑簭 鈥?O(n × n!) 鏈熸湜鏅傞枔錛岀劇紿殑鏈澹炴儏娉併?/li>


      • ]]>綆楁硶鍏紡 - 鍥懼艦鍖?http://m.tkk7.com/Skynet/archive/2009/11/05/301255.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Thu, 05 Nov 2009 05:09:00 GMThttp://m.tkk7.com/Skynet/archive/2009/11/05/301255.htmlhttp://m.tkk7.com/Skynet/comments/301255.htmlhttp://m.tkk7.com/Skynet/archive/2009/11/05/301255.html#Feedback1http://m.tkk7.com/Skynet/comments/commentRss/301255.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/301255.html
         鍦ㄥ畾鍒?鏁板鍏紡鐨勬椂鍊?鍙兘 甯屾湜鏈変釜鐩磋鐨?灞曠幇
        鎴戜滑鍓嶄竴孌墊椂闂寸敤鍒扮殑  { 鎺ㄨ崘鍒嗗?90 澶?閫掑噺鍏紡錛屽鏋滆繖涓滆タ鏃╁彂鐜?鍏紡灝變笉浼氶敊浜嗭紒}
        鐩墠鎴戜滑閫掑噺鐨勫叕寮?/div>
        y=-x/90+1
        y = -x/90+1
         
         鍦ㄦ瘮濡傛垜浠紭鍖栦笅  90澶╂敼鎴?
        (y*10)^2=-x+90
        100 y^2 = 90-x
        2009-11-05





        ]]>apriori-瀹炵幇http://m.tkk7.com/Skynet/archive/2009/09/01/293479.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Tue, 01 Sep 2009 09:43:00 GMThttp://m.tkk7.com/Skynet/archive/2009/09/01/293479.htmlhttp://m.tkk7.com/Skynet/comments/293479.htmlhttp://m.tkk7.com/Skynet/archive/2009/09/01/293479.html#Feedback0http://m.tkk7.com/Skynet/comments/commentRss/293479.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/293479.html瀛欒秴
        璁茶В
        榪欏氨 鎶婁粬鐨?鎬濇兂 鐢諱釜鍥?




        ]]>apriori 綆楁硶錛坧y錛?/title><link>http://m.tkk7.com/Skynet/archive/2009/08/31/293309.html</link><dc:creator>鍒樺嚡姣?/dc:creator><author>鍒樺嚡姣?/author><pubDate>Mon, 31 Aug 2009 06:25:00 GMT</pubDate><guid>http://m.tkk7.com/Skynet/archive/2009/08/31/293309.html</guid><wfw:comment>http://m.tkk7.com/Skynet/comments/293309.html</wfw:comment><comments>http://m.tkk7.com/Skynet/archive/2009/08/31/293309.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/Skynet/comments/commentRss/293309.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/Skynet/services/trackbacks/293309.html</trackback:ping><description><![CDATA[<br /> <br /> 浠g爜:<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> -*- coding: UTF8 -*-</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sys<br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鏈灝?nbsp;鏀寔搴?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">sup_min </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> int(sys.argv[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br /> <br /> ss </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 浜ゆ槗 鏁版嵁 搴?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">D</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[<br />  </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A,B,C,D</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />  </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">B,C,E</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />  </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A,B,C,E</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />  </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">B,D,E</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />  </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A,B,C,D</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /> ]<br /> <br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">浜ゆ槗鏁版嵁搴撳睍鐜?/span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> <br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> arr </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> D : </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> arr<br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"><br /> <br /> <br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">rows</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">int(sys.argv[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])</span><br /> <span style="color: rgb(128, 0, 0);">D=[]<br /> for tid in open('BuyMusic.20090722.mob.prodIds').readlines()[:rows] :<br />     D.append(tid.split("\n")[0].split("\t")[1])<br /> <br /> <br /> print "璇誨彇 鏂囦歡緇撴潫 BuyMusic.20090722.mob.prodIds !"<br /> </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">鍏ㄥ眬 棰戠箒欏?nbsp;鏀墮泦</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">sup_data_map </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {}<br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">鍏ㄥ眬  鏈澶ч綣侀」 鏀墮泦</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">is_zsup</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 閬嶅巻榪囩▼ 涓存椂 灞閮?nbsp; 棰戠箒欏?nbsp;鏀墮泦</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">mapL </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {}<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 絎竴嬈?nbsp;棰戠箒欏?nbsp;鏀墮泦</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> find_frequent_1_itemset(I):<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> I</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">null</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">or</span><span style="color: rgb(0, 0, 0);"> I</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">  <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapL.has_key(I): mapL[I]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"> <br />     </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">: mapL[I]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br /> <br /> map(find_frequent_1_itemset,[ I  </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> TID </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> D </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> I  </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> TID.split(ss) ])<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鍒烽夋帀 灝忎簬 鏈灝忔敮鎸佸害 鐨?nbsp;棰戠箒欏?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> remove_not_sup_min(map,supmin</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sup_min):<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k  </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> [k </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> map.items() </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> v</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">supmin] :<br />         </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> map[k]<br /> remove_not_sup_min(mapL)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">絎竴嬈?nbsp;絳涢?nbsp;棰戠箒欏?nbsp;緇撴潫!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> mapL<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 瑁呰澆 鍏ㄥ眬 棰戠箒欏?nbsp;鏈澶ч綣侀」</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> mapL.items() : <br />     sup_data_map[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />     is_zsup[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鍒ゅ畾 鏄惁 'BD' 灞炰簬  'BCD' 涓?nbsp;</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">isInTid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">lambda</span><span style="color: rgb(0, 0, 0);"> I,TID : len(I.split(ss)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> len([i </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> I </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> TID.split(ss)])<br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 緇勫悎  [A,B] + [A,C] = [A,B.C]</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> comb(arr1,arr2):<br />     tmap</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> arr1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">arr2 : tmap[v]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"> <br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> tmap.keys()<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> apriori 榪唬鏍稿績</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> runL(mapL,dep):<br />     mapL2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {}<br />     C</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />     keys </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mapL.keys()<br />     iik</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"><br />     jjk</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鏍規嵁 涓婃  棰戠箒欏?nbsp;錛岀敓鎴愭湰嬈?nbsp;'鍙兘棰戠箒欏? 闆嗗悎 </span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> ii </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> range(len(keys)) : <br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> jj </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> range(ii</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,len(keys)) :<br />             keystr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">comb([ch </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> ch </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> keys[ii].split(ss)],[ch </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> ch </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> keys[jj].split(ss)])<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> len(keystr) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> dep : </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br />             keystr.sort()<br />             tk</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ss.join(keystr)<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> tk </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> C : C[tk]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(keys[ii],keys[jj])<br /> <br />     </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">  '鍙兘棰戠箒欏? 瀵規瘮 浜ゆ槗鏁版嵁搴?nbsp; 璁℃暟</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> tk,z </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> C.items():<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> TID </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> D:<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isInTid(tk,TID) :<br />                 </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapL2.has_key(tk): mapL2[tk]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />                 </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">: mapL2[tk]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鍒烽夋帀 灝忎簬 鏈灝忔敮鎸佸害 鐨?nbsp;棰戠箒欏?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    remove_not_sup_min(mapL2)<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">  is_zsup.items() :<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k1,v1 </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> mapL2.items() :<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isInTid(k,k1) :<br />                 </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> is_zsup[k]<br />                 </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鍏ㄥ眬 棰戠箒欏?nbsp;,鏈澶ч綣侀」  鏀墮泦 </span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> mapL2.items() : <br />         sup_data_map[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />         is_zsup[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />     </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">絎?/span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">str(dep)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">嬈?nbsp;絳涢?nbsp;棰戠箒欏?nbsp;緇撴潫!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> <br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> mapL2<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鐪熸 榪愯 </span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">ii</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> mapL :<br />     ii</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ii</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />     mapL </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> runL(mapL,ii)<br />     </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> mapL<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 鍏ㄥ眬  棰戠箒欏?nbsp;涓?nbsp;鍘婚櫎 鏈澶ч綣侀」</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> is_zsup.items() :<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> sup_data_map.has_key(k) : </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> sup_data_map[k]<br /> <br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">棰戠箒欏?/span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> sup_data_map<br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> <br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">鏈澶ч綣侀」</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> is_zsup<br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> <br /> <br /> </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">鍙俊搴?nbsp;灞曠幇</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">  sup_data_map.items() :<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k1,v1 </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> is_zsup.items() :<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isInTid(k,k1) :<br />             </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> k,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">-></span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,k1,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\t%.1f</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">((float(is_zsup[k1])</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">float(sup_data_map[k]))</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /> </span></div> <br /> <br /> <br /> <br /> 緇撴灉:<br /> -bash-3.00$ python ap.py 2<br /> 浜ゆ槗鏁版嵁搴撳睍鐜?br /> A,B,C,D<br /> B,C,E<br /> A,B,C,E<br /> B,D,E<br /> A,B,C,D<br /> <br /> 絎竴嬈?絳涢?棰戠箒欏?緇撴潫!<br /> {'A': 3, 'C': 4, 'B': 5, 'E': 3, 'D': 3}<br /> 絎?嬈?絳涢?棰戠箒欏?緇撴潫!<br /> {'C,D': 2, 'C,E': 2, 'A,D': 2, 'A,B': 3, 'A,C': 3, 'B,E': 3, 'B,D': 3, 'B,C': 4}<br /> 絎?嬈?絳涢?棰戠箒欏?緇撴潫!<br /> {'A,B,D': 2, 'A,B,C': 3, 'B,C,D': 2, 'B,C,E': 2, 'A,C,D': 2}<br /> 絎?嬈?絳涢?棰戠箒欏?緇撴潫!<br /> {'A,B,C,D': 2}<br /> 絎?嬈?絳涢?棰戠箒欏?緇撴潫!<br /> {}<br /> 棰戠箒欏?br /> {'A': 3, 'C': 4, 'B': 5, 'E': 3, 'D': 3, 'C,D': 2, 'C,E': 2, 'A,D': 2, 'A,B': 3, 'A,C': 3, 'A,B,D': 2, 'B,C,D': 2, 'A,C,D': 2, 'B,E': 3, 'B,D': 3, 'B,C': 4, 'A,B,C': 3}<br /> <br /> 鏈澶ч綣侀」<br /> {'B,C,E': 2, 'A,B,C,D': 2}<br /> <br /> 鍙俊搴?灞曠幇<br /> A -> A,B,C,D     66.7%<br /> C -> B,C,E     50.0%<br /> C -> A,B,C,D     50.0%<br /> B -> B,C,E     40.0%<br /> B -> A,B,C,D     40.0%<br /> E -> B,C,E     66.7%<br /> D -> A,B,C,D     66.7%<br /> C,D -> A,B,C,D     100.0%<br /> C,E -> B,C,E     100.0%<br /> A,D -> A,B,C,D     100.0%<br /> A,B -> A,B,C,D     66.7%<br /> A,C -> A,B,C,D     66.7%<br /> A,B,D -> A,B,C,D     100.0%<br /> B,C,D -> A,B,C,D     100.0%<br /> A,C,D -> A,B,C,D     100.0%<br /> B,E -> B,C,E     66.7%<br /> B,D -> A,B,C,D     66.7%<br /> B,C -> B,C,E     50.0%<br /> B,C -> A,B,C,D     50.0%<br /> A,B,C -> A,B,C,D     66.7%<br /> <br /> <img src ="http://m.tkk7.com/Skynet/aggbug/293309.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/Skynet/" target="_blank">鍒樺嚡姣?/a> 2009-08-31 14:25 <a href="http://m.tkk7.com/Skynet/archive/2009/08/31/293309.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>k-means 錛坧ython錛夌畻娉?http://m.tkk7.com/Skynet/archive/2009/08/07/290242.html鍒樺嚡姣?/dc:creator>鍒樺嚡姣?/author>Fri, 07 Aug 2009 08:20:00 GMThttp://m.tkk7.com/Skynet/archive/2009/08/07/290242.htmlhttp://m.tkk7.com/Skynet/comments/290242.htmlhttp://m.tkk7.com/Skynet/archive/2009/08/07/290242.html#Feedback0http://m.tkk7.com/Skynet/comments/commentRss/290242.htmlhttp://m.tkk7.com/Skynet/services/trackbacks/290242.html 杞?http://www.daniweb.com/forums/thread31449.html
        浠涔堥兘涓嶈浜嗭紝鐩存帴鐪嬩唬鐮佸惂銆?br /> 娉ㄨВ 搴旇鍐欑殑姣旇緝璇︾粏


        # liukaiyi 
        # 娉?nbsp;k-means 錛岀淮搴︾被鍨?nbsp;- 鏁板煎艦寮?nbsp;( 199 鎴?nbsp;23.13 

        import sys, math, random

        # -- 綾誨寲 '鏁版嵁' 
        #
         鍦?nbsp;n-緇村害絀洪棿
        class Point:
            
        def __init__(self, coords, reference=None):
                self.coords 
        = coords
                self.n 
        = len(coords)
                self.reference 
        = reference
            
        def __repr__(self):
                
        return str(self.coords)

        # -- 綾誨寲 '鑱氶泦鐐?nbsp;/ 鑱氱被騫沖潎璺濈 鐐?nbsp;' 
        #
         -- 鍦?nbsp;n-緇村害絀洪棿
        #
         -- k-means 鏍稿績綾?/span>
        #
         -- 姣忔 鑱氶泦鍚勭偣 鍥寸粫濂?nbsp;榪涜鑱氶泦 
        #
         -- 騫舵彁渚涙柟娉?nbsp;姹?鑱氶泦鍚庣殑璁$畻涓績鐐癸紝鍚屾椂璁板叆 姝ゆ 涓績鐐?鑱氶泦鍚勭偣騫沖潎璺濈)錛屼負涓嬩竴嬈¤仛闆嗘彁渚涗腑蹇冪偣.
        class Cluster:
            
        def __init__(self, points):
                
        if len(points) == 0: raise Exception("ILLEGAL: EMPTY CLUSTER")
                self.points 
        = points
                self.n 
        = points[0].n
            
        for p in points:
                    
        if p.n != self.n: raise Exception("ILLEGAL: MULTISPACE CLUSTER")
                
        # 姹?nbsp;鑱氶泦鍚勭偣鍚?nbsp;騫沖潎鐐?/span>
            self.centroid = self.calculateCentroid()
            
        def __repr__(self):
                
        return str(self.points)
            
            
        # 鏇存柊 涓績鐐癸紝騫惰繑鍥?nbsp;鍘熶腑蹇冪偣 涓?nbsp;鐜頒腑蹇冪偣(鑱氶泦鍚勭偣騫沖潎璺濈)璺濈  
            def update(self, points):
                old_centroid 
        = self.centroid
                self.points 
        = points
                self.centroid 
        = self.calculateCentroid()
                
        return getDistance(old_centroid, self.centroid)
            
            
        # 璁$畻騫沖潎鐐?nbsp;錛堣仛闆?鏀墮泦鍚勭偣錛堢鏈被鐨勪腑蹇冪偣錛夋渶榪戞暟鎹?鍚庣敓鎴愭柊鐨?nbsp;涓績鐐?nbsp;錛?/span>
            def calculateCentroid(self):
                centroid_coords 
        = []
                
        #  緇村害 榪唬
            for i in range(self.n):
                    centroid_coords.append(
        0.0)
                    
        # 鏀墮泦鍚勭偣 榪唬 
                for p in self.points:
                        centroid_coords[i] 
        = centroid_coords[i]+p.coords[i]
                    centroid_coords[i] 
        = centroid_coords[i]/len(self.points)
                
        return Point(centroid_coords)

        # -- 榪斿洖鏍規嵁 k-means 鑱氶泦褰㈡垚鐨?nbsp;鏁版嵁闆?nbsp;
        def kmeans(points, k, cutoff):
            
        # Randomly sample k Points from the points list, build Clusters around them
            initial = random.sample(points, k)
            clusters 
        = []
            
        for p in initial: clusters.append(Cluster([p]))
            
        # 榪唬 k-means 鐩村埌 姣忔榪唬 鍚勬敹闆嗙偣 鍒殑 鏈澶?nbsp;涓嶈秴榪?nbsp;0.5 
            while True:
                
        #  k 涓敹闆?nbsp;鏁扮粍
                lists = []
                
        for c in clusters: lists.append([])
            
        # 榪唬 姣忎釜 鏁版嵁鐐?nbsp;錛屽茍璁$畻涓庢瘡涓腑蹇冪偣璺濈
            # 騫舵妸鏁版嵁鐐規坊鍔犲叆鐩稿簲鏈鐭殑涓績鐐規敹闆嗘暟緇勪腑
            # 鍦ㄨ凱浠d腑 smallest_distance 涓烘瘡涓偣涓庡悇涓績鐐規渶鐭窛紱?nbsp;鍙傛暟錛岃娉ㄦ剰鐪?/span>
                for p in points:
                    smallest_distance 
        = getDistance(p, clusters[0].centroid)
                    index 
        = 0
                    
        for i in range(len(clusters[1:])):
                        distance 
        = getDistance(p, clusters[i+1].centroid)
                        
        if distance < smallest_distance:
                            smallest_distance 
        = distance
                            index 
        = i+1
                    
        # 娣誨姞鍒?nbsp;紱繪渶鐭腑蹇冭窛紱葷殑 鏁扮粍涓?/span>
                lists[index].append(p)
            
                
        # 鑱氶泦瀹岋紝璁$畻鏂?nbsp;涓績鐐?/span>
            # 騫?nbsp;cluster.centroid 灞炴ц鍏ヤ笅 鏂頒腑蹇冪偣錛堜笅涓嬈?nbsp;鑱氶泦鐨勪腑蹇冪偣 錛?/span>
            # 騫?nbsp;璁$畻涓庝笂涓嬈?nbsp;涓績鐐?nbsp;璺濈 錛屽鏋?nbsp;宸煎湪 cutoff 0.5 浠ヤ笅 ,璺沖嚭榪唬 錛堢粨鏉燂紝榪斿洖鏈鍚庝竴嬈?nbsp;鑱氶泦闆嗗悎錛?/span>
            biggest_shift = 0.0
                
        for i in range(len(clusters)):
                    shift 
        = clusters[i].update(lists[i])
                    biggest_shift 
        = max(biggest_shift, shift)
                
        if biggest_shift < cutoff: break
            
        return clusters


        # -- 寰楀埌嬈у嚑閲屽痙璺濈涓ょ偣涔嬮棿 
        def getDistance(a, b):
            
        # Forbid measurements between Points in different spaces
            if a.n != b.n: raise Exception("ILLEGAL: NON-COMPARABLE POINTS")
            
        # Euclidean distance between a and b is sqrt(sum((a[i]-b[i])^2) for all i)
            ret = 0.0
            
        for i in range(a.n):
                ret 
        = ret+pow((a.coords[i]-b.coords[i]), 2)
            
        return math.sqrt(ret)

        # -- 鍦?nbsp;n-緇村害 絀洪棿涓垱寤?nbsp;闅忔満鐐?/span>
        #
         -- 闅忔満鐢熸垚 嫻嬭瘯鏁版嵁
        def makeRandomPoint(n, lower, upper):
            coords 
        = []
            
        for i in range(n): coords.append(random.uniform(lower, upper))
            
        return Point(coords)

        # main 
        def main(args):
            
        # 鍙傛暟璇存槑
            # num_points,    n,    k,      cutoff,         lower,        upper 
            # 闅忔満鏁版嵁鏁伴噺 , 緇村害, 鑱氶泦鏁? 璺沖嚭榪唬鏈灝忚窛紱?nbsp;,   緇村害鏁版渶澶у?緇村害鏁版渶灝忓?/span>
            num_points, n, k, cutoff, lower, upper = 10230.5-200200

            
        # 鍦?nbsp;n-緇村害絀洪棿閲?nbsp;, 鍒涘緩 num_points 闅忔満鐐?/span>
            # 嫻嬭瘯鏁版嵁鐢熸垚 
            points = []
            
        for i in range(num_points): points.append(makeRandomPoint(n, lower, upper))

            
        # 浣跨敤 k-means 綆楁硶錛屾潵 鑱氶泦鏁版嵁鐐?nbsp;(綆楁硶鍏ュ彛鐐?
            clusters = kmeans(points, k, cutoff)

            
        print "\nPOINTS:"
            
        for p in points: print "P:", p
            
        print "\nCLUSTERS:"
            
        for c in clusters: print "C:", c
        if __name__ == "__main__": main(sys.argv)



        ]]>
        主站蜘蛛池模板: 国产免费拔擦拔擦8x| 亚洲av无码专区首页| 久久久久亚洲精品无码网址| 日本人的色道免费网站| 国产日韩AV免费无码一区二区三区| 亚洲AV男人的天堂在线观看| 亚洲国产国产综合一区首页| 狠狠亚洲狠狠欧洲2019| 大陆一级毛片免费视频观看| 91高清免费国产自产| 久久国产乱子伦精品免费看| 国产黄色免费观看| 免费精品国自产拍在线播放| 亚洲国产精品99久久久久久| 亚洲一卡2卡4卡5卡6卡在线99| 西西人体44rt高清亚洲 | 亚洲资源在线观看| 区久久AAA片69亚洲| 亚洲人成无码www久久久| 免费a级毛片在线观看| 欧洲美熟女乱又伦免费视频| 精品久久久久久久久免费影院| 小草在线看片免费人成视久网| 人妻在线日韩免费视频| 91免费福利视频| 国产精品网站在线观看免费传媒| 丝袜足液精子免费视频| 一级片在线免费看| 一本岛v免费不卡一二三区| 午夜在线免费视频| 免费人人潮人人爽一区二区| 国产成人综合亚洲一区| 香港一级毛片免费看| 免费视频成人国产精品网站| xxxxxx日本处大片免费看| 一级做a爰片性色毛片免费网站| 无码精品人妻一区二区三区免费 | 国产男女猛烈无遮挡免费视频网站| 岛国大片免费在线观看| 国产成人涩涩涩视频在线观看免费 | 四虎成人免费大片在线|