锘??xml version="1.0" encoding="utf-8" standalone="yes"?>黑人大战亚洲人精品一区,久久伊人亚洲AV无码网站,亚洲婷婷第一狠人综合精品http://www.tkk7.com/javacap/category/22798.html鏉備竷鏉傚叓銆傘傘備竴瀹朵箣璦zh-cnThu, 01 Dec 2011 14:23:47 GMTThu, 01 Dec 2011 14:23:47 GMT60姹傝繛緇鏁存暟浣垮緱鍏跺拰涓虹粰瀹氱殑涓涓鏁存暟鐨勬瀯閫犳цВ娉?http://www.tkk7.com/javacap/archive/2011/12/01/365333.htmlDoubleHDoubleHThu, 01 Dec 2011 14:09:00 GMThttp://www.tkk7.com/javacap/archive/2011/12/01/365333.htmlhttp://www.tkk7.com/javacap/comments/365333.htmlhttp://www.tkk7.com/javacap/archive/2011/12/01/365333.html#Feedback0http://www.tkk7.com/javacap/comments/commentRss/365333.htmlhttp://www.tkk7.com/javacap/services/trackbacks/365333.html濡傞錛氭眰榪炵畫姝f暣鏁頒嬌寰楀叾鍜屼負緇欏畾鐨勪竴涓鏁存暟
涓嬮潰緇欏嚭鎴戠殑瑙f硶錛屽嚑涔庡彲浠ヤ竴姝ュ埌浣嶆眰鍑烘潵
瀹炵幇浠g爜濡備笅錛?br />
/**
*Author: Koth (
http://weibo.com/yovn)
*Date:  2011-12-01
*/
#include 
<stdlib.h>
#include 
<stdio.h>
#include 
<stdint.h>

int solve(int Y,int& X){
    
int m=0;
    
int t=Y;
    
if(Y<=0){
        X
=Y;
        
return 1;
    }
    
while((t&1)==0){
        m
+=1;
        t
=t>>1;
    }
    
if(m==32){
        X
=Y;
        
return 1;
    }
    
int lastK=32;
    
for(;lastK>m+1;lastK--){
        
if(Y &(1<<(lastK-1))){
            
            
break;
        }
            
    }

    
//its a number st. exp(2,K)
    if(lastK==(m+1)){
        X
=Y;
        
return 1;
    }
    
int k=1<<(m+1);
    
int b=(Y>>m)-(1<<(lastK-m-1));

    X
=(1<<(lastK-m-2))+(b+1-k)/2;

    
if(X<=0){
        k
=k-1-((0-X)<<1);
        X
=0-X+1;
    }
    
    
return k;

}

int main(int argc,char* argv[]){
    
if(argc<=1){
        fprintf(stdout,
"Usage:%s number\n",argv[0]);
        
return 0;
    }
    
int Y=atoi(argv[1]);
    
int X=0;
    
int k=solve(Y,X);
    fprintf(stdout,
"%d=",Y);
    
for(int i=0;i<k;i++){
        fprintf(stdout,
"%d",X+i);
        
if(i<(k-1)){
            fprintf(stdout,
"+");
        }
    }
    fprintf(stdout,
"\n");
    
return 0;
}


DoubleH 2011-12-01 22:09 鍙戣〃璇勮
]]>
鍚戜笂鍙栨暣鐨勪竴涓簲鐢?/title><link>http://www.tkk7.com/javacap/archive/2009/05/04/268781.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Mon, 04 May 2009 03:45:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2009/05/04/268781.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/268781.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2009/05/04/268781.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/268781.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/268781.html</trackback:ping><description><![CDATA[闂錛?br /> 鏈変釜閾捐〃錛圠ist)錛屾湁N涓厓绱狅紝褰揘寰堝ぇ鐨勬椂鍊欙紝鎴戜滑閫氬父鎯沖垎鎵瑰鐞嗚閾捐〃銆傚亣濡傛瘡嬈″鐞哅鏉?0<M<=N),閭d箞闇瑕佸鐞嗗嚑嬈℃墠鑳藉鐞嗗畬鎵鏈夋暟鎹憿錛?br /> <br /> 闂寰堢畝鍗曪紝鎴戜滑闇瑕?lt;N/M>嬈★紝榪欓噷鎴戜滑鐢?lt;>琛ㄧず鍚戜笂鍙栨暣,[]琛ㄧず鍚戜笅鍙栨暣錛岄偅涔堟庝箞鏉ヨ〃紺鴻繖涓煎憿錛?br /> 鎴戜滑鍙互璇佹槑錛?br /> <N/M>=[(N-1)/M]+1    (0<M<=N,M,N∈Z)<br /> <br /> 涓嶅け涓鑸э紝鎴戜滑璁綨=Mk+r(0<=r<M),<br /> 1錛夊綋r>0鏃訛紝<br /> <br /> 宸﹁竟錛?lt;N/M>=<(Mk+r)/M>=<k+r/M>=k+<r/M>=k+1<br /> 鍙寵竟錛歔(N-1)/M]+1=[(Mk+r-1)/M]+1=[k+(r-1)/M]+1=k+1+[(r-1)/M]=k+1<br /> 2錛夊綋r=0<br /> 宸﹁竟錛?lt;N/M>=k<br /> 鍙寵竟錛歔(N-1)/M]+1=[(Mk-1)/M]+1=[(M(k-1)+M-1)/M]+1=[k-1+(M-1)/M]+1=k+[(M-1)/M]=k<br /> <br /> 鍛介寰楄瘉銆?br /> <br /> 鏈変簡榪欎釜鍏紡錛屾垜浠湪Java浠g爜閲屽彲浠ヨ繖鏍瘋綆?<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: #0000ff;">int</span><span style="color: #000000;"> nn</span><span style="color: #000000;">=</span><span style="color: #000000;">(N</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;">M </span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;"><br /> <img src="http://www.tkk7.com/Images/dot.gif" alt="" />.<br /> <br /> </span></div> <br /> 鍥犱負'/'鏄線涓嬪彇鏁寸殑銆?br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <img src ="http://www.tkk7.com/javacap/aggbug/268781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2009-05-04 11:45 <a href="http://www.tkk7.com/javacap/archive/2009/05/04/268781.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>銆婄畻娉曟璁恒嬬涓绔犱範棰?5璇佹槑錛圵ilson瀹氱悊)http://www.tkk7.com/javacap/archive/2009/04/10/264936.htmlDoubleHDoubleHFri, 10 Apr 2009 14:38:00 GMThttp://www.tkk7.com/javacap/archive/2009/04/10/264936.htmlhttp://www.tkk7.com/javacap/comments/264936.htmlhttp://www.tkk7.com/javacap/archive/2009/04/10/264936.html#Feedback1http://www.tkk7.com/javacap/comments/commentRss/264936.htmlhttp://www.tkk7.com/javacap/services/trackbacks/264936.html 絎竴绔犵殑涔犻闅懼害閫備腑錛岃繖閲屾娊鍑虹35棰樻潵錛岃繖棰樻槸璇佹槑Wilson瀹氱悊銆?br /> Wilson瀹氱悊錛?br />    N鏄竴涓礌鏁板綋涓斾粎褰? (N-1)! ≡ -1(mod N)

璇佹槑錛?br />   棣栧厛鎴戜滑璇佹槑鑻鏄礌鏁幫紝閭d箞絳夊紡鎴愮珛錛屽浜嶯=2,榪欐槸寰堟槑鏄劇殑銆備互涓嬭瘉鏄嶯>2 鐨勬儏褰€?br />   1錛夎嫢N鏄礌鏁幫紝閭d箞鍏充簬N鍚屼綑鐨勪箻娉曠兢G={1,2,3....N-1}
    緹姣忎釜鍏冪礌閮芥湁閫嗗厓錛屾槧灝?f:a -> a^-1 錛宖(a)=a^-1 鏄竴涓竴涓鏄犲皠銆傜幇鍦紝浠誨彇涓涓厓绱狅紝瀹冪殑閫嗗厓瑕佷箞鏄叾瀹冧竴涓厓绱狅紝鎴栬呮槸瀹冩湰韜?鎴戜滑鍋囪鍏朵腑鍏冪礌x鐨勯嗗厓鏄畠鏈韓錛岄偅涔坸*x ≡1(mod N) =>(x+1)*(x-1)=K*N,鑰孨鏄礌鏁幫紝鎵浠ヨ涔坸=N-1,瑕佷箞x=1銆備篃灝辨槸璇達紝闄や簡榪欎袱涓厓绱狅紝鍏跺畠鐨勫厓绱犵殑閫嗗厓閮芥槸鏄犲皠鍒板埆鐨勫厓绱犵殑銆傝孨>2,鏄鏁幫紝鎵浠ュ厓绱犲叡鏈塏-1涓紝涔熷氨鏄伓鏁頒釜鍏冪礌銆傝繖鏍鳳紝闄や簡1鍜孨-1澶栧墿浣欑殑N-3涓厓绱犲垰濂界粨鎴愪袱涓や竴瀵?x,y)(閫嗗厓鏄敮涓鐨勶級浣垮緱f(x)=y=x^-1,涔熷氨鏄痻y≡1(mod N).
鐜板湪鎶奊鐨勫厓绱犲叏閮ㄤ箻璧鋒潵錛岃浜掍負閫嗗厓鐨勫厓绱犵粍鎴愪竴瀵歸偅涔?N-1)!=1*(N-1)*(x1*y1)*(x2*y2)...(xk*yk)≡1*(N-1)*1*1...1 (mod N)≡-1(mod N).
    榪欐牱錛屾垜浠瘉鏄庝簡涓涓柟鍚戜簡錛屼笅闈㈡垜浠瘉鏄庡彟涓涓柟鍚?br />
  2錛夎嫢(N-1)! ≡ -1(mod N)鎴愮珛錛屽垯N鏄礌鏁幫紝鑻ヤ笉鐒訛紝浠?N鏄拰鏁般傚垯(N-1)!鑲畾鏁撮櫎N,鍥犱負N鐨勬瘡涓洜瀛恜婊¤凍p>1,p<N銆?br />    鐜板湪浠?N-1)!=K1*N.鍙?N-1)! ≡ -1(mod N) => K1*N ≡ -1(mod N),鐢卞悓浣欐ц川鐭ワ紝瀛樺湪K2浣垮緱K1*N+1=K2*N,涓よ竟鍚屾椂闄や互N寰桲1+1/N=K2.鏄劇劧錛岃繖鏄笉鍙兘鐨勶紝鎵浠ヨ嫢(N-1)! ≡ -1(mod N)鎴愮珛錛屽垯N鏄礌鏁?br />
璇佹槑瀹屾瘯錛?br />
榪欓噷鐢ㄧ兢鐨勬蹇佃兘澶熺畝鍖栦竴瀹氱殑鎻忚堪錛屽叾瀹炲彲浠ュ畬鍏ㄤ笉鐢ㄧ兢鐨勬蹇電殑錛屽彧涓嶈繃榪欐牱涓鏉ワ紝鎻忚堪鏇撮暱鐐癸紝綣佺悙鐐癸紒







DoubleH 2009-04-10 22:38 鍙戣〃璇勮
]]>
鏈夋剰鎬濈殑鈥滅數鐏場闂鈥?/title><link>http://www.tkk7.com/javacap/archive/2008/03/05/183986.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Wed, 05 Mar 2008 07:00:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2008/03/05/183986.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/183986.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2008/03/05/183986.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/183986.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/183986.html</trackback:ping><description><![CDATA[<pre>榪囬亾閲屼緷嬈℃寕鐫鏍囧彿鏄?錛?錛?, ......錛?00鐨勭數鐏場錛屽紑濮嬪畠浠?br /> 閮芥槸鐏潃鐨勩傚綋絎竴涓漢璧拌繃鏃訛紝浠栧皢鏍囧彿涓?nbsp;1 鐨勫嶆暟鐨勭數鐏場鐨勫紑鍏?br /> 綰挎媺浜嗕竴涓嬶紱褰撶浜屼釜浜鴻蛋榪囨椂錛屼粬灝嗘爣鍙蜂負 2 鐨勫嶆暟鐨勭數鐏場鐨勫紑鍏?br /> 綰挎媺浜嗕竴涓嬶紱褰撶涓変釜浜鴻蛋榪囨椂錛屼粬灝嗘爣鍙蜂負 3 鐨勫嶆暟鐨勭數鐏場鐨勫紑鍏?br /> 綰挎媺浜嗕竴涓嬶紱......  濡傛榪涜涓嬪幓錛屽綋絎竴鐧句釜浜鴻蛋榪囨椂錛屼粬灝嗘爣鍙蜂負<br /> 100 鐨勫嶆暟鐨勭數鐏場鐨勫紑鍏崇嚎鎷変簡涓涓嬨?br /> 闂細褰撶涓鐧句釜浜鴻蛋榪囧悗錛岃繃閬撻噷浜潃鐨勭數鐏場鏍囧彿鏄灝戯紵<br /> <br /> <br /> 鎴戠殑鎬濊礬錛?br /> 璁炬爣鍙蜂負K鐨勭伅娉¤鎷変簡L(K)嬈★紝閭d箞褰揕(K)涓哄鏁扮殑鏃跺欙紝鐏場鏄寒鐨勩?br /> 閭d箞閭d簺鏍囧彿琚媺浜嗗鏁版鍛紵<br /> K=1鏃訛紝寰堟樉鐒舵槸鍙媺浜?嬈★紝鏈鍚庢槸浜殑銆?br /> 鍏舵K>=2鏃訛紝鎹鎰忥紝K鍙風伅絎?嬈★紝鍜岀K嬈¤偗瀹氭槸鎷変笅浜嗭紝鍏朵綑鐨勫彧浼氳絎琄鐨勫洜瀛愭鎷夛紝<br /> 鎹洜寮忓垎瑙e畾鐞嗭紝鏁癒鍒嗚В涓?br /> K=p1^(n1)*p2^(n2)*.....pi^(ni)<br /> 鍏朵腑p1,p2,...pi涓虹礌鏁般?br /> 閭d箞錛孠鏈夐偅浜涘洜瀛愬憿錛?br /> 鍏跺疄鍙互鑰冭檻浠諱竴涓洜瀛愶紝浠栧彲鑳芥槸浠巒1涓猵1涓夎嫢騫蹭釜錛?涓埌n1涓?錛屼粠n2涓猵2涓夎嫢騫蹭釜銆傘傘傘傘傦紙褰撳叏鏄?涓殑鏃跺欙紝榪欎釜鐗規畩鐨勫洜瀛愭槸1錛?br /> 榪欐牱錛屾牴鎹箻娉曞師鐞嗭紝鎬誨叡鏈塋(K)=(n1+1)*(n2+1)*(n3+1).....<br /> 姣斿錛?2=2^2*3<br /> 涓縐嶆湁3*2=6涓洜瀛愶紝浠栦滑鏄?,2,3,4,6,12.<br /> <br /> 鐜板湪鑰冭檻瑕佷嬌L(K)涓哄鏁幫紝閭d箞n1,n2,n3涓嶈兘鏈変竴涓槸濂囨暟錛屾垨鍒欙紝鏈変竴涓猲i+1涓哄伓鏁幫紝鑰屽伓鏁頒笌浠諱綍鏁扮浉涔樹粛涓哄伓鏁般?br /> 浠庤岋紝n1,n2,n3閮戒負鍋舵暟,閮借兘琚?闄ゃ?br /> 鍥犱負n1,n2,n3閮戒負鍋舵暟錛屾樉鐒惰鏁板繀欏繪槸涓鉤鏂規暟錛屽彲鍐欐垚K=(X)^2.<br /> 浠庤岋紝1錛?錛?錛?6錛?5錛?6錛?9錛?4錛?1錛?00鏈鍚庢槸浜殑銆?br /> <br /> </pre> <img src ="http://www.tkk7.com/javacap/aggbug/183986.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2008-03-05 15:00 <a href="http://www.tkk7.com/javacap/archive/2008/03/05/183986.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浼稿睍鏍戜笌鍗婁幾灞曟爲Java瀹炵幇http://www.tkk7.com/javacap/archive/2007/12/19/168627.htmlDoubleHDoubleHTue, 18 Dec 2007 16:39:00 GMThttp://www.tkk7.com/javacap/archive/2007/12/19/168627.htmlhttp://www.tkk7.com/javacap/comments/168627.htmlhttp://www.tkk7.com/javacap/archive/2007/12/19/168627.html#Feedback0http://www.tkk7.com/javacap/comments/commentRss/168627.htmlhttp://www.tkk7.com/javacap/services/trackbacks/168627.html闃呰鍏ㄦ枃

DoubleH 2007-12-19 00:39 鍙戣〃璇勮
]]>
涓縐嶆柊鐨凙VL騫寵 鏍戝垹闄ゆ搷浣滅殑瀹炵幇http://www.tkk7.com/javacap/archive/2007/12/18/168557.htmlDoubleHDoubleHTue, 18 Dec 2007 10:30:00 GMThttp://www.tkk7.com/javacap/archive/2007/12/18/168557.htmlhttp://www.tkk7.com/javacap/comments/168557.htmlhttp://www.tkk7.com/javacap/archive/2007/12/18/168557.html#Feedback0http://www.tkk7.com/javacap/comments/commentRss/168557.htmlhttp://www.tkk7.com/javacap/services/trackbacks/168557.html闃呰鍏ㄦ枃

DoubleH 2007-12-18 18:30 鍙戣〃璇勮
]]>
浣跨敤璧㈣呮爲鐨勫閮ㄦ帓搴?/title><link>http://www.tkk7.com/javacap/archive/2007/12/16/168034.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Sun, 16 Dec 2007 00:08:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2007/12/16/168034.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/168034.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2007/12/16/168034.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/168034.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/168034.html</trackback:ping><description><![CDATA[澶栭儴鎺掑簭瀹炵幇浣跨敤鍫嗘潵鐢熸垚鑻ュ共涓『涓詫紝鐒跺悗浣跨敤澶氳礬褰掑茍綆楁硶鏉ョ敓鎴愭渶緇堟帓搴忓ソ鐨勫唴瀹廣?br /> <br /> 鏈潵鎵撶畻浣跨敤璐ヨ呮爲錛岀粨鏋滃彂鐜拌觸鑰呮爲褰撳弬涓庣殑绔炶禌鑰呯浉瀵硅緝澶氱殑鎯呭喌涓嬶紝娌℃湁鍏ㄩ儴瀹屾垚灝辮絀鴻妭鐐瑰厖婊′簡銆?br /> 榪欎釜鎸夌収瀹冪殑涓ユ牸綆楁硶錛屽叾瀹炰篃鏄彲浠ョ煡閬撲笉鑳戒繚璇佹繪槸鑳芥帓瀹岀殑銆傚ぉ蹇寒浜嗭紝鎵嶅交搴曟斁寮冧嬌鐢ㄨ觸鑰呮爲銆傛敼鎴愬疄鐜拌耽鑰呮爲錛岀粨鏋滃彂鐜拌耽鑰呮爲鑳戒繚璇佷笉鍑洪敊錛屽疄鐜扮珶涔熼『鎵嬨?br /> <br /> 鏈鍚庢暣浜嗕釜嫻嬭瘯鏂囦歡500M鐨勯殢鏈哄唴瀹癸紝2~3鍒嗛挓鎺掑簭瀹屾垚浜嗭紙搴旇榪樺彲浠ヤ紭鍖栵級錛屾劅瑙夎繕琛屻?br /> <br /> 浠g爜杈冨錛屾渶瑕佽冭檻鍒頒互鍚庡彲鑳借閲嶇敤銆?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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms.extsort;<br /> <br /> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.IOException;<br /> <br /> </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> algorithms.MinHeap;<br /> <br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  *<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ExternalSorter {<br />     <br />         <br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> heapSize,RecordStore source,RunAcceptor mediator ,ResultAcceptor ra)</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> IOException<br />     {<br />         MinHeap</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Record</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> heap</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MinHeap</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Record</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">(Record.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">,heapSize);<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">heapSize;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />         {<br />             Record r</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">source.readNextRecord();<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(r.isNull())</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />             heap.insert(r);<br />         }<br />         <br />         Record readR</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">source.readNextRecord();<br />         </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">readR.isNull()</span><span style="color: rgb(0, 0, 0);">||!</span><span style="color: rgb(0, 0, 0);">heap.isEmpty())<br />         {<br />         <br />             Record curR</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">null</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);">begin output one run</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">            mediator.startNewRun();<br />             </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">heap.isEmpty())<br />             {<br />                 curR</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">heap.removeMin();<br />             <br />                 mediator.acceptRecord(curR);<br />                 <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, 0);">!</span><span style="color: rgb(0, 0, 0);">readR.isNull()) {<br />                     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (readR.compareTo(curR) </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">) {<br />                         heap.addToTail(readR);<br />                     } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />                         heap.insert(readR);<br />                 }<br />                 readR</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">source.readNextRecord();<br />                 <br />             }<br />             </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">done one run</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">            mediator.closeRun();<br />             <br />             </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">prepare for next run</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">            heap.reverse();<br />             </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">heap.isFull()</span><span style="color: rgb(0, 0, 0);">&&!</span><span style="color: rgb(0, 0, 0);">readR.isNull())<br />             {<br />                 <br />                 heap.insert(readR);<br />                 readR</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">source.readNextRecord();<br />                 <br />             }<br />             <br />             <br />         }<br />         RecordStore[] stores</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">mediator.getProductedStores();<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">        LoserTree  lt=new LoserTree(stores);</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        WinnerTree  lt</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> WinnerTree(stores);<br />         <br />         Record least</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">lt.nextLeastRecord();<br />         ra.start();<br />         </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">least.isNull())<br />         {<br />             ra.acceptRecord(least);<br />             least</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">lt.nextLeastRecord();<br />         }<br />         ra.end();<br />         <br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stores.length;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />         {<br />             stores[i].destroy();<br />         }<br />     }<br />     <br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> IOException<br />     {<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">        RecordStore store=new MemRecordStore(60004,true);<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">        RunAcceptor mediator=new MemRunAcceptor();<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">        ResultAcceptor ra=new MemResultAcceptor();</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        ExternalSorter sorter</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ExternalSorter();<br />             <br />         RecordStore store</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileRecordStore(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test_sort.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />         RunAcceptor mediator</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileRunAcceptor(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test_sort</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />         ResultAcceptor ra</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> FileRecordStore(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test_sorted.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />         <br />         <br />         sorter.sort(</span><span style="color: rgb(0, 0, 0);">70000</span><span style="color: rgb(0, 0, 0);">, store, mediator, ra);<br />     }<br />     <br /> }</span></div> <br /> 鍏朵綑鐨勯兘鎵撳寘鍦ㄦ錛?br /> <br /> <a title="婧愮爜" href="http://www.tkk7.com/Files/javacap/external_sorter(071216)_src.rar">婧愮爜</a><br /> <br /> <img src ="http://www.tkk7.com/javacap/aggbug/168034.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2007-12-16 08:08 <a href="http://www.tkk7.com/javacap/archive/2007/12/16/168034.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍥懼強鍏剁畻娉曞涔狅紙Java瀹炵幇) 涓夛細鏈灝忔敮鎾戞爲錛圥rim,Kruskal綆楁硶錛?/title><link>http://www.tkk7.com/javacap/archive/2007/12/14/167884.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Fri, 14 Dec 2007 15:48:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2007/12/14/167884.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/167884.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2007/12/14/167884.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/167884.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/167884.html</trackback:ping><description><![CDATA[鍏?鏈灝忔敮鎾戞爲MST<br /> 緇欏畾涓涓畝鍗曟湁鍚戝浘錛岃姹傛潈鍊兼渶灝忕殑鐢熸垚鏍戯紝鍗蟲眰鏈灝忔敮鎾戞爲闂銆?br /> 鎵璋撶敓鎴愭爲錛屽氨鏄鏍戠殑欏剁偣闆嗗悎絳変簬緇欏畾鍥劇殑欏剁偣闆嗗悎錛屼笖杈歸泦鍚堝寘鍚簬鍥劇殑杈歸泦鍚堛?br /> 涔熷氨璇存壘鍑烘瀯鎴愭爲鐨勶紝緇忚繃鎵鏈夐《鐐圭殑錛屼笖鏉冨兼渶灝忕殑杈歸泦銆?br /> 鏍戠殑瀹氫箟鏄姹傛棤鍥炶礬鐨勶紝鐒跺悗鏄姹傝繛閫氱殑銆?br /> <br /> 鏈変袱涓瘮杈冪粡鍏哥殑綆楁硶鏄細<br /> 1錛塒rim綆楁硶錛?璇ョ畻娉曠殑鎬濇兂璺烡ijstra綆楁硶闈炲父鐩鎬技錛孌ijstra綆楁硶涓瘡嬈℃眰鍑轟笅涓涓《鐐規椂渚濇嵁鐨勬槸紱誨垵濮嬮《鐐規渶榪戠殑錛岃孭rim綆楁硶鍒欐壘紱籚1鏁翠釜闆嗗悎鏈榪戠殑錛屼篃灝辨槸浠嶸1涓煇涓妭鐐瑰嚭鍙戝埌璇ラ《鐐圭殑杈圭殑鏉冨兼渶灝忋?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: #000000;">@Override<br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Edge[] prim() {<br />         MinHeap</span><span style="color: #000000;"><</span><span style="color: #000000;">Edge</span><span style="color: #000000;">></span><span style="color: #000000;"> heap</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> MinHeap</span><span style="color: #000000;"><</span><span style="color: #000000;">Edge</span><span style="color: #000000;">></span><span style="color: #000000;">(Edge.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">,numEdges);<br />         Edge[] edges</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Edge[numVertexes</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;">we start from 0</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">record already how many edges;</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> startV</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />         Arrays.fill(visitTags, </span><span style="color: #0000ff;">false</span><span style="color: #000000;">);<br />         Edge e;<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(e</span><span style="color: #000000;">=</span><span style="color: #000000;">firstEdge(startV);isEdge(e);e</span><span style="color: #000000;">=</span><span style="color: #000000;">nextEdge(e))<br />         {<br />             heap.insert(e);<br />         }<br />         visitTags[startV]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />         <br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(num</span><span style="color: #000000;"><</span><span style="color: #000000;">numVertexes</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&&!</span><span style="color: #000000;">heap.isEmpty())</span><span style="color: #008000;">//</span><span style="color: #008000;">tree's edge number was n-1</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        {<br />             <br />             e</span><span style="color: #000000;">=</span><span style="color: #000000;">heap.removeMin();<br />         <br />             startV</span><span style="color: #000000;">=</span><span style="color: #000000;">toVertex(e);<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(visitTags[startV])<br />             {<br />                 </span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;<br />             }<br />             visitTags[startV]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />             edges[num</span><span style="color: #000000;">++</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">e;<br />             </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(e</span><span style="color: #000000;">=</span><span style="color: #000000;">firstEdge(startV);isEdge(e);e</span><span style="color: #000000;">=</span><span style="color: #000000;">nextEdge(e))<br />             {<br />                 </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">visitTags[toVertex(e)])</span><span style="color: #008000;">//</span><span style="color: #008000;">can't add already visit vertex, this may cause cycle</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">                heap.insert(e);<br />             }<br />             <br />                 <br />             <br />         }<br />         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num</span><span style="color: #000000;"><</span><span style="color: #000000;">numVertexes</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         {<br />             </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> IllegalArgumentException(</span><span style="color: #000000;">"</span><span style="color: #000000;">not connected graph</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> edges;<br />     }<br /> <br /> </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />      * A Graph example<br />      * we mark the vertexes  with 0,1,2,<img src="http://www.tkk7.com/Images/dot.gif" alt="" />.14 from left to right , up to down<br />      * S-8-B-4-A-2-C-7-D<br />      * |   |   |   |   |<br />      * 3   3   1   2   5<br />      * |   |   |   |   |<br />      * E-2-F-6-G-7-H-2-I<br />      * |   |   |   |   |<br />      * 6   1   1   1   2<br />      * |   |   |   |   |<br />      * J-5-K-1-L-3-M-3-T<br />      * <br />      </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> testPrimMST() {<br />     <br /> <br />         <br />         DefaultGraph g</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> DefaultGraph(</span><span style="color: #000000;">15</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">its a undirected graph</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        g.setEdge(</span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">);<br />         g.setEdge(</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;">2</span><span style="color: #000000;">);<br />         g.setEdge(</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;">2</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">3</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">);<br />         <br />         g.setEdge(</span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #000000;">5</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">5</span><span style="color: #000000;">, </span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">1</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;">);<br />         g.setEdge(</span><span style="color: #000000;">6</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 />         g.setEdge(</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;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">3</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">8</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;">);<br />         g.setEdge(</span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">9</span><span style="color: #000000;">, </span><span style="color: #000000;">5</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">9</span><span style="color: #000000;">, </span><span style="color: #000000;">4</span><span style="color: #000000;">, </span><span style="color: #000000;">5</span><span style="color: #000000;">);<br />         <br />         <br />         g.setEdge(</span><span style="color: #000000;">5</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">6</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;">);<br />         g.setEdge(</span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">9</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">);<br />         g.setEdge(</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;">2</span><span style="color: #000000;">);<br />         <br />         <br />         g.setEdge(</span><span style="color: #000000;">10</span><span style="color: #000000;">, </span><span style="color: #000000;">5</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">5</span><span style="color: #000000;">, </span><span style="color: #000000;">10</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">11</span><span style="color: #000000;">, </span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">6</span><span style="color: #000000;">, </span><span style="color: #000000;">11</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">12</span><span style="color: #000000;">, </span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">7</span><span style="color: #000000;">, </span><span style="color: #000000;">12</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">13</span><span style="color: #000000;">, </span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">8</span><span style="color: #000000;">, </span><span style="color: #000000;">13</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">14</span><span style="color: #000000;">, </span><span style="color: #000000;">9</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">9</span><span style="color: #000000;">, </span><span style="color: #000000;">14</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">);<br />         <br />         g.setEdge(</span><span style="color: #000000;">10</span><span style="color: #000000;">, </span><span style="color: #000000;">11</span><span style="color: #000000;">, </span><span style="color: #000000;">5</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">11</span><span style="color: #000000;">, </span><span style="color: #000000;">10</span><span style="color: #000000;">, </span><span style="color: #000000;">5</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">11</span><span style="color: #000000;">, </span><span style="color: #000000;">12</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">12</span><span style="color: #000000;">, </span><span style="color: #000000;">11</span><span style="color: #000000;">, </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">12</span><span style="color: #000000;">, </span><span style="color: #000000;">13</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">13</span><span style="color: #000000;">, </span><span style="color: #000000;">12</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">13</span><span style="color: #000000;">, </span><span style="color: #000000;">14</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">);<br />         g.setEdge(</span><span style="color: #000000;">14</span><span style="color: #000000;">, </span><span style="color: #000000;">13</span><span style="color: #000000;">, </span><span style="color: #000000;">3</span><span style="color: #000000;">);<br />         <br />         g.assignLabels(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> String[]{</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;">"</span><span style="color: #000000;">B</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">A</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">C</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">D</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">E</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">F</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">G</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">H</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">I</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">J</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">K</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">L</span><span style="color: #000000;">"</span><span style="color: #000000;">,</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;">"</span><span style="color: #000000;">T</span><span style="color: #000000;">"</span><span style="color: #000000;">});<br />         Edge[] edges</span><span style="color: #000000;">=</span><span style="color: #000000;">g.prim();<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> total</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;"><</span><span style="color: #000000;">edges.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />         {<br />             System.out.println(edges[i].toString(g));<br />             total</span><span style="color: #000000;">+=</span><span style="color: #000000;">edges[i].getWeight();<br />         }<br />         System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">MST total cost:</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">total);<br /> }</span></div> <br /> 2)Kruskal綆楁硶錛?br /> 璇ョ畻娉曞紑濮嬫妸錛屾瘡涓妭鐐圭湅鎴愪竴涓嫭绔嬬殑涓や袱涓嶅悓鐨勭瓑浠風被錛屾瘡嬈″幓鏉冨兼渶灝忕殑杈癸紝濡傛灉鍏寵仈榪欐潯杈圭殑涓や釜欏剁偣鍦ㄥ悓涓涓瓑浠風被閲岄偅涔堣繖鏉¤竟涓嶈兘鏀懼叆MST錛堟渶灝忕敓鎴愭爲錛変腑錛屽惁鍒欐斁鍏ST涓紝騫舵妸榪欎袱涓瓑浠風被鍚堝茍鎴愪竴涓瓑浠風被銆?br /> 緇х畫浠庡墿浣欒竟闆嗛噷閫夋渶灝忕殑杈癸紝鐩村埌鏈鍚庡墿浣欎竴涓瓑浠風被浜嗐?br /> 璇ョ畻娉曟秹鍙婄瓑浠風被鐨勫悎騫?鏌ユ壘,涓鑸敤鐖舵寚閽堟爲鏉ュ疄鐜般備笅闈㈠厛緇欏嚭鐖舵寚閽堟爲瀹炵幇鐨勫茍鏌ョ畻娉曘?br /> 甯﹁礬寰勫帇緙╃殑綆楁硶錛屽叾鏌ユ壘鏃墮棿浠d環鍙互鐪嬪仛鏄父鏁扮殑銆?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: #0000ff;">package</span><span style="color: #000000;"> algorithms;<br /> <br /> </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />  * </span><span style="color: #808080;">@author</span><span style="color: #008000;"> yovn<br />  *<br />  </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> ParentTree {<br />     <br />     <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> PTreeNode<br />     {<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> parentIndex</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> numChildren</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">only make sense in root</span><span style="color: #008000;"><br /> </span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> setParent(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i) {<br />             <br />             parentIndex</span><span style="color: #000000;">=</span><span style="color: #000000;">i;<br />             <br />         }<br />     }<br />     PTreeNode[] nodes;<br />     </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> numPartions;<br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ParentTree(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> numNodes)<br />     {<br />         nodes</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> PTreeNode[numNodes];<br />         numPartions</span><span style="color: #000000;">=</span><span style="color: #000000;">numNodes;<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;"><</span><span style="color: #000000;">numNodes;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />         {<br />             nodes[i]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> PTreeNode();<br />             nodes[i].parentIndex</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">means root</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">            <br />         }<br />         <br />     }<br />     <br />     </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />      * use path compress<br />      * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> i<br />      * </span><span style="color: #808080;">@return</span><span style="color: #008000;"><br />      </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i)<br />     {<br />         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(nodes[i].parentIndex</span><span style="color: #000000;">==-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         {<br />             </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> i;<br />         }<br />         </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />         {<br />             nodes[i].setParent(find(nodes[i].parentIndex));</span><span style="color: #008000;">//</span><span style="color: #008000;">compress the path</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">            </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> nodes[i].parentIndex;<br />         }<br />     }<br />     <br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> numPartions()<br />     {<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> numPartions;<br />     }<br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">boolean</span><span style="color: #000000;"> union(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> j)<br />     {<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> pi</span><span style="color: #000000;">=</span><span style="color: #000000;">find(i);<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> pj</span><span style="color: #000000;">=</span><span style="color: #000000;">find(j);<br />         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pi</span><span style="color: #000000;">!=</span><span style="color: #000000;">pj)<br />         {<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(nodes[pi].numChildren</span><span style="color: #000000;">></span><span style="color: #000000;">nodes[pj].numChildren)<br />             {<br />                 nodes[pj].setParent(pi);<br />                 nodes[pj].numChildren</span><span style="color: #000000;">+=</span><span style="color: #000000;">nodes[pi].numChildren;<br />                 nodes[pi].numChildren</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />                 <br />             }<br />             </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />             {<br />                 nodes[pi].setParent(pj);<br />                 nodes[pi].numChildren</span><span style="color: #000000;">+=</span><span style="color: #000000;">nodes[pj].numChildren;<br />                 nodes[pj].numChildren</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />             }<br />             numPartions</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br />             </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />     }<br />     <br /> }</span></div> <br /> <br /> 浠庤竟闆嗛噷鏉冩渶灝忕殑杈癸紝鎴戜滑鍙堝彲浠ュ熷姪鏈灝忓煎爢鏉ュ畬鎴愩傛湁浜嗙埗鎸囬拡鏍戜互鍙婃渶灝忓煎爢錛岀幇瀹濳ruskal綆楁硶灝卞緢綆鍗曚簡錛?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;">@Override<br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Edge[] kruskal() {<br />         Edge[] edges</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Edge[numVertexes</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br />         ParentTree ptree</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> ParentTree(numVertexes);<br />         MinHeap</span><span style="color: #000000;"><</span><span style="color: #000000;">Edge</span><span style="color: #000000;">></span><span style="color: #000000;"> heap</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> MinHeap</span><span style="color: #000000;"><</span><span style="color: #000000;">Edge</span><span style="color: #000000;">></span><span style="color: #000000;">(Edge.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">,numEdges);<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;"><</span><span style="color: #000000;">numVertexes;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />         {<br />             </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Edge e</span><span style="color: #000000;">=</span><span style="color: #000000;">firstEdge(i);isEdge(e);e</span><span style="color: #000000;">=</span><span style="color: #000000;">nextEdge(e))<br />             {<br />                 heap.insert(e);<br />             }<br />         }<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> index</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(ptree.numPartions()</span><span style="color: #000000;">></span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         {<br />             Edge e</span><span style="color: #000000;">=</span><span style="color: #000000;">heap.removeMin();<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(ptree.union(fromVertex(e),toVertex(e)))<br />             {<br />                 edges[index</span><span style="color: #000000;">++</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">e;<br />             }<br />         }<br />         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(index</span><span style="color: #000000;"><</span><span style="color: #000000;">numVertexes</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         {<br />             </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> IllegalArgumentException(</span><span style="color: #000000;">"</span><span style="color: #000000;">Not a connected graph</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> edges;<br />         <br />     }</span></div> OK錛屽埌姝わ紝鍥捐鐨勫ぇ姒傜殑綆楁硶綆楁槸澶嶄範瀹屼簡銆?br /> <br /> <img src ="http://www.tkk7.com/javacap/aggbug/167884.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2007-12-14 23:48 <a href="http://www.tkk7.com/javacap/archive/2007/12/14/167884.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍥懼強鍏剁畻娉曞涔狅紙Java瀹炵幇) 浜岋細鎷撴墤鎺掑簭錛屾渶鐭礬寰勯棶棰?/title><link>http://www.tkk7.com/javacap/archive/2007/12/14/167812.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Fri, 14 Dec 2007 13:00:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2007/12/14/167812.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/167812.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2007/12/14/167812.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/167812.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/167812.html</trackback:ping><description><![CDATA[     鎽樿: 鍥?鎷撴墤鎺掑簭 鑰冭檻涓涓換鍔″畨鎺掔殑渚嬪瓙錛屾瘮濡傛湁寰堝浠誨姟T1,T2,.... 榪欎簺浠誨姟鍙堟槸鐩鎬簰鍏寵仈鐨勶紝姣斿Tj瀹屾垚鍓嶅繀欏昏姹俆i宸插畬鎴愶紝榪欐牱T1,T2....搴忓垪鍏充簬榪欐牱鐨勫厛鍐蟲潯浠舵瀯鎴愪竴涓浘錛屽叾涓鏋淭i蹇呴』瑕佸厛浜嶵j瀹屾垚錛岄偅涔?lt;Ti,Tj>灝辨槸璇ュ浘涓殑涓鏉¤礬寰勶紝璺緞闀垮害涓?鐨勫氨鏄竴鏉¤竟銆?鎷撴墤鎺掑簭灝辨槸鎶婅繖浜涗換鍔℃寜鐓у畬鎴愮殑鍏堝悗欏哄簭鎺掑垪鍑烘潵銆傛樉鐒訛紝榪欐牱鐨勯『搴忓彲鑳戒笉鏄敮涓鐨勶紝姣斿Tk錛孴...  <a href='http://www.tkk7.com/javacap/archive/2007/12/14/167812.html'>闃呰鍏ㄦ枃</a><img src ="http://www.tkk7.com/javacap/aggbug/167812.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2007-12-14 21:00 <a href="http://www.tkk7.com/javacap/archive/2007/12/14/167812.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍥懼強鍏剁畻娉曞涔狅紙Java瀹炵幇) 涓錛氬瓨鍌ㄧ粨鏋勶紝娣卞害浼樺厛鍛ㄦ父錛屽箍搴︿紭鍏堝懆娓?/title><link>http://www.tkk7.com/javacap/archive/2007/12/14/167764.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Fri, 14 Dec 2007 06:46:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2007/12/14/167764.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/167764.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2007/12/14/167764.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/167764.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/167764.html</trackback:ping><description><![CDATA[     鎽樿: 寰堟棭灝辨兂鎬葷粨涓涓嬩簡錛屼竴鐩存病鏈夋椂闂達紝OK錛岃繘鍏ユ棰樸? 涓 鍥劇殑鍩烘湰姒傚康鍙婂瓨鍌ㄧ粨鏋?鍥綠鏄敱欏剁偣鐨勬湁絀烽泦鍚堬紝浠ュ強欏剁偣涔嬮棿鐨勫叧緋葷粍鎴愶紝欏剁偣鐨勯泦鍚堣涓篤錛岄《鐐逛箣闂寸殑鍏崇郴鏋勬垚杈圭殑闆嗗悎E G=(V,E). 璇翠竴鏉¤竟浠巚1,榪炴帴鍒皏2,閭d箞鏈塿1Ev2(E鏄疺涓婄殑涓涓叧緋伙級銆?銆?lt;v1,v2>∈E. 鍥炬湁鏈夊悜鍥撅紝鏃犲悜鍥句箣鍒嗭紝鏃犲悜鍥劇殑涓鏉¤竟鐩稿綋浜庢湁鍚戝浘鐨勪腑涓ゆ潯杈癸紝鍗沖鏋滄棤鍚戝浘...  <a href='http://www.tkk7.com/javacap/archive/2007/12/14/167764.html'>闃呰鍏ㄦ枃</a><img src ="http://www.tkk7.com/javacap/aggbug/167764.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2007-12-14 14:46 <a href="http://www.tkk7.com/javacap/archive/2007/12/14/167764.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鎺掑簭綆楁硶澶嶄範錛圝ava瀹炵幇錛?浜岋級錛?褰掑茍鎺掑簭錛屽爢鎺掑簭錛屾《寮忔帓搴忥紝鍩烘暟鎺掑簭http://www.tkk7.com/javacap/archive/2007/12/14/167618.htmlDoubleHDoubleHThu, 13 Dec 2007 17:03:00 GMThttp://www.tkk7.com/javacap/archive/2007/12/14/167618.htmlhttp://www.tkk7.com/javacap/comments/167618.htmlhttp://www.tkk7.com/javacap/archive/2007/12/14/167618.html#Feedback8http://www.tkk7.com/javacap/comments/commentRss/167618.htmlhttp://www.tkk7.com/javacap/services/trackbacks/167618.html闃呰鍏ㄦ枃

DoubleH 2007-12-14 01:03 鍙戣〃璇勮
]]>
鎺掑簭綆楁硶澶嶄範錛圝ava瀹炵幇錛?涓錛夛細 鎻掑叆錛屽啋娉★紝閫夋嫨錛孲hell,蹇熸帓搴?/title><link>http://www.tkk7.com/javacap/archive/2007/12/13/167364.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Wed, 12 Dec 2007 17:08:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2007/12/13/167364.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/167364.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2007/12/13/167364.html#Feedback</comments><slash:comments>13</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/167364.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/167364.html</trackback:ping><description><![CDATA[涓轟簡渚夸簬綆$悊錛屽厛寮曞叆涓熀紜綾伙細<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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms;<br /> <br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  *<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Sorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Comparable</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> {<br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from ,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len);<br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array)<br />     {<br />         sort(array,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,array.length);<br />     }<br />     </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> swap(E[] array,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from ,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> to)<br />     {<br />         E tmp</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[from];<br />         array[from]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[to];<br />         array[to]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">tmp;<br />     }<br /> <br /> }</span></div> 涓 鎻掑叆鎺掑簭<br /> 璇ョ畻娉曞湪鏁版嵁瑙勬ā灝忕殑鏃跺欏崄鍒嗛珮鏁堬紝璇ョ畻娉曟瘡嬈℃彃鍏ョK+1鍒板墠K涓湁搴忔暟緇勪腑涓涓悎閫備綅緗紝K浠?寮濮嬪埌N-1,浠庤屽畬鎴愭帓搴忥細<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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms;<br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> InsertSorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Comparable</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Sorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</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);"> (non-Javadoc)<br />      * @see algorithms.Sorter#sort(E[], int, int)<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len) {<br />          E tmp</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">null</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);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">from</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);">;i</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">from</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, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />           {<br />               tmp</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[i];<br />               </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i;<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, 0);">></span><span style="color: rgb(0, 0, 0);">from;j</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">)<br />               {<br />                   </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(tmp.compareTo(array[j</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);"><</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />                   {<br />                       array[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[j</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, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />               }<br />               array[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">tmp;<br />           }<br />     }<br />         <br />     <br /> <br /> }</span></div> <br /> 浜?鍐掓場鎺掑簭<br /> 榪欏彲鑳芥槸鏈綆鍗曠殑鎺掑簭綆楁硶浜嗭紝綆楁硶鎬濇兂鏄瘡嬈′粠鏁扮粍鏈寮濮嬫瘮杈冪浉閭諱袱鍏冪礌錛屾妸絎琲灝忕殑鍐掓場鍒版暟緇勭殑絎琲涓綅緗俰浠?涓鐩村埌N-1浠庤屽畬鎴愭帓搴忋傦紙褰撶劧涔熷彲浠ヤ粠鏁扮粍寮濮嬬寮濮嬫瘮杈冪浉閭諱袱鍏冪礌錛屾妸絎琲澶х殑鍐掓場鍒版暟緇勭殑絎琋-i涓綅緗俰浠?涓鐩村埌N-1浠庤屽畬鎴愭帓搴忋?<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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms;<br /> <br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  *<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> BubbleSorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Comparable</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Sorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> DWON</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> bubble_down(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />     {<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">from;i</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">from</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, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />         {<br />             </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">len</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);">;j</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">i;j</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">)<br />             {<br />                 </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(array[j].compareTo(array[j</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);"><</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />                 {<br />                     swap(array,j</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);">,j);<br />                 }<br />             }<br />         }<br />     }<br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> bubble_up(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br />     {<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">len</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);">;i</span><span style="color: rgb(0, 0, 0);">>=</span><span style="color: rgb(0, 0, 0);">from;i</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">)<br />         {<br />             </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">from;j</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">i;j</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />             {<br />                 </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(array[j].compareTo(array[j</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);">></span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />                 {<br />                     swap(array,j,j</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 />             }<br />         }<br />     }<br />     @Override<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len) {<br />         <br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(DWON)<br />         {<br />             bubble_down(array,from,len);<br />         }<br />         </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />         {<br />             bubble_up(array,from,len);<br />         }<br />     }<br />     <br /> }</span></div> <br /> 涓夛紝閫夋嫨鎺掑簭<br /> 閫夋嫨鎺掑簭鐩稿浜庡啋娉℃潵璇達紝瀹冧笉鏄瘡嬈″彂鐜伴嗗簭閮戒氦鎹紝鑰屾槸鍦ㄦ壘鍒板叏灞絎琲灝忕殑鏃跺欒涓嬭鍏冪礌浣嶇疆錛屾渶鍚庤窡絎琲涓厓绱犱氦鎹紝浠庤屼繚璇佹暟緇勬渶緇堢殑鏈夊簭銆?br /> 鐩稿涓庢彃鍏ユ帓搴忔潵璇達紝閫夋嫨鎺掑簭姣忔閫夊嚭鐨勯兘鏄叏灞絎琲灝忕殑錛屼笉浼氳皟鏁村墠i涓厓绱犱簡銆?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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms;<br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  *<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> SelectSorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Comparable</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Sorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</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);"> (non-Javadoc)<br />      * @see algorithms.Sorter#sort(E[], int, int)<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     @Override<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len) {<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</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, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />         {<br />             </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> smallest</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i;<br />             </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">from;<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, 0);"><</span><span style="color: rgb(0, 0, 0);">from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">len;j</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />             {<br />                 </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(array[j].compareTo(array[smallest])</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />                 {<br />                     smallest</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">j;<br />                 }<br />             }<br />             swap(array,i,smallest);<br />                    <br />         }<br /> <br />     }<br />   <br /> }<br /> </span></div> 鍥?Shell鎺掑簭<br /> Shell鎺掑簭鍙互鐞嗚В涓烘彃鍏ユ帓搴忕殑鍙樼錛屽畠鍏呭垎鍒╃敤浜嗘彃鍏ユ帓搴忕殑涓や釜鐗圭偣錛?br /> 1錛夊綋鏁版嵁瑙勬ā灝忕殑鏃跺欓潪甯擱珮鏁?br /> 2錛夊綋緇欏畾鏁版嵁宸茬粡鏈夊簭鏃剁殑鏃墮棿浠d環涓篛(N)<br /> 鎵浠ワ紝Shell鎺掑簭姣忔鎶婃暟鎹垎鎴愯嫢涓皬鍧楋紝鏉ヤ嬌鐢ㄦ彃鍏ユ帓搴忥紝鑰屼笖涔嬪悗鍦ㄨ繖鑻ヤ釜灝忓潡鎺掑ソ搴忕殑鎯呭喌涓嬫妸瀹冧滑鍚堟垚澶т竴鐐圭殑灝忓潡錛岀戶緇嬌鐢ㄦ彃鍏ユ帓搴忥紝涓嶅仠鐨勫悎騫跺皬鍧楋紝鐭ラ亾鏈鍚庢垚涓涓潡錛屽茍浣跨敤鎻掑叆鎺掑簭銆?br /> <br /> 榪欓噷姣忔鍒嗘垚鑻ュ共灝忓潡鏄氳繃“澧為噺” 鏉ユ帶鍒剁殑錛屽紑濮嬫椂澧為噺浜ゅぇ錛屾帴榪慛/2,浠庤屼嬌寰楀垎鍓插嚭鏉ユ帴榪慛/2涓皬鍧楋紝閫愭笎鐨勫噺灝?#8220;澧為噺“鏈緇堝埌鍑忓皬鍒?銆?br /> <br /> 涓鐩磋緝濂界殑澧為噺搴忓垪鏄?^k-1,2^(k-1)-1,.....7,3,1,榪欐牱鍙嬌Shell鎺掑簭鏃墮棿澶嶆潅搴﹁揪鍒癘(N^1.5)<br /> 鎵浠ユ垜鍦ㄥ疄鐜癝hell鎺掑簭鐨勬椂鍊欓噰鐢ㄨ澧為噺搴忓垪<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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms;<br /> <br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ShellSorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Comparable</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Sorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</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);"> (non-Javadoc)<br />      * Our delta value choose 2^k-1,2^(k-1)-1,<img src="http://www.tkk7.com/Images/dot.gif" alt="" /><img src="http://www.tkk7.com/Images/dot.gif" alt="" />.7,3,1.<br />      * complexity is O(n^1.5)<br />      * @see algorithms.Sorter#sort(E[], int, int)<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     @Override<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len) {<br />         <br />         </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">1.calculate  the first delta value;</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);">int</span><span style="color: rgb(0, 0, 0);"> value</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);">((value</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);">*</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);">len)<br />         {<br />             value</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(value</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);">*</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);">1</span><span style="color: rgb(0, 0, 0);">;<br />         <br />         }<br />     <br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> delta</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">value;delta</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);">;delta</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(delta</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);">/</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);">1</span><span style="color: rgb(0, 0, 0);">)<br />         {<br />             </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">delta;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />             {<br />                 modify_insert_sort(array,from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">i,len</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">i,delta);<br />             }<br />         }<br /> <br />     }<br />     <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> modify_insert_sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> delta) {<br />           </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(len</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, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />           E tmp</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">null</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);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">delta;i</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">from</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, 0);">+=</span><span style="color: rgb(0, 0, 0);">delta)<br />           {<br />               tmp</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[i];<br />               </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i;<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, 0);">></span><span style="color: rgb(0, 0, 0);">from;j</span><span style="color: rgb(0, 0, 0);">-=</span><span style="color: rgb(0, 0, 0);">delta)<br />               {<br />                   </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(tmp.compareTo(array[j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">delta])</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />                   {<br />                       array[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">delta];<br />                   }<br />                   </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />               }<br />               array[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">tmp;<br />           }<br /> <br />     }<br /> }</span></div> <br /> 浜?蹇熸帓搴?br /> 蹇熸帓搴忔槸鐩墠浣跨敤鍙兘鏈騫挎硾鐨勬帓搴忕畻娉曚簡銆?br /> 涓鑸垎濡備笅姝ラ錛?br /> 1錛夐夋嫨涓涓灑綰藉厓绱狅紙鏈夊緢瀵歸夋硶錛屾垜鐨勫疄鐜伴噷閲囩敤鍘諱腑闂村厓绱犵殑綆鍗曟柟娉曪級<br /> 2錛変嬌鐢ㄨ鏋㈢航鍏冪礌鍒嗗壊鏁扮粍錛屼嬌寰楁瘮璇ュ厓绱犲皬鐨勫厓绱犲湪瀹冪殑宸﹁竟錛屾瘮瀹冨ぇ鐨勫湪鍙寵竟銆傚茍鎶婃灑綰藉厓绱犳斁鍦ㄥ悎閫傜殑浣嶇疆銆?br /> 3錛夋牴鎹灑綰藉厓绱犳渶鍚庣‘瀹氱殑浣嶇疆錛屾妸鏁扮粍鍒嗘垚涓夐儴鍒嗭紝宸﹁竟鐨勶紝鍙寵竟鐨勶紝鏋㈢航鍏冪礌鑷繁錛屽宸﹁竟鐨勶紝鍙寵竟鐨勫垎鍒掑綊璋冪敤蹇熸帓搴忕畻娉曞嵆鍙?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, 255);">package</span><span style="color: rgb(0, 0, 0);"> algorithms;<br /> <br /> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />  * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> yovn<br />  *<br />  </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> QuickSorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Comparable</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Sorter</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">E</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);"> (non-Javadoc)<br />      * @see algorithms.Sorter#sort(E[], int, int)<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     @Override<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len) {<br />         q_sort(array,from,from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">len</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 /> <br />     <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> q_sort(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> to) {<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(to</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">from</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, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br />         </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pivot</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">selectPivot(array,from,to);<br /> <br />         <br />         <br />         pivot</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">partion(array,from,to,pivot);<br />         <br />         q_sort(array,from,pivot</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 />         q_sort(array,pivot</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);">,to);<br />         <br />     }<br /> <br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> partion(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> to, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pivot) {<br />         E tmp</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[pivot];<br />         array[pivot]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[to];</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">now to's position is available</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);">while</span><span style="color: rgb(0, 0, 0);">(from</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">to)<br />         {<br />             </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(from</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">to</span><span style="color: rgb(0, 0, 0);">&&</span><span style="color: rgb(0, 0, 0);">array[from].compareTo(tmp)</span><span style="color: rgb(0, 0, 0);"><=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)from</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);">if</span><span style="color: rgb(0, 0, 0);">(from</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">to)<br />             {<br />                 array[to]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[from];</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">now from's position is available</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">                to</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">;<br />             }<br />             </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(from</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">to</span><span style="color: rgb(0, 0, 0);">&&</span><span style="color: rgb(0, 0, 0);">array[to].compareTo(tmp)</span><span style="color: rgb(0, 0, 0);">>=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)to</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);">if</span><span style="color: rgb(0, 0, 0);">(from</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">to)<br />             {<br />                 array[from]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">array[to];</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">now to's position is available now </span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">                from</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">;<br />             }<br />         }<br />         array[from]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">tmp;<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> from;<br />     }<br /> <br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> selectPivot(E[] array, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> from, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> to) {<br />     <br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (from</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">to)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />     }<br /> <br /> }</span></div> <br /> 榪樻湁褰掑茍鎺掑簭錛屽爢鎺掑簭錛屾《寮忔帓搴忥紝鍩烘暟鎺掑簭錛屼笅嬈″湪褰掔撼銆?br /> <br /> <img src ="http://www.tkk7.com/javacap/aggbug/167364.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2007-12-13 01:08 <a href="http://www.tkk7.com/javacap/archive/2007/12/13/167364.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鏍戠殑闈為掑綊閬嶅巻 綺楃硻鐗?http://www.tkk7.com/javacap/archive/2007/10/11/152073.htmlDoubleHDoubleHThu, 11 Oct 2007 08:05:00 GMThttp://www.tkk7.com/javacap/archive/2007/10/11/152073.htmlhttp://www.tkk7.com/javacap/comments/152073.htmlhttp://www.tkk7.com/javacap/archive/2007/10/11/152073.html#Feedback0http://www.tkk7.com/javacap/comments/commentRss/152073.htmlhttp://www.tkk7.com/javacap/services/trackbacks/152073.html/**
 * 
 
*/

import java.util.Stack;
import java.util.Vector;

/**
 * 
@author yovn
 *
 
*/
public class TreeDemo {
    
    
static interface NodeVistor
    {
         
<T> void visit(BinaryTreeNode<T> node);
    }
    
static class BinaryTree<T>
    {
        BinaryTreeNode
<T> root;
        
        
public BinaryTree(BinaryTreeNode<T> root) {
            
this.root=root;
        }

        
//no recursion ,pre-order
        void NLRVisit(NodeVistor visitor)
        {
            BinaryTreeNode
<T> pointer=root;
            Stack
<BinaryTreeNode<T>> stack=new Stack<BinaryTreeNode<T>>();
            
while(!stack.isEmpty()||pointer!=null)
            {
                
if(pointer!=null)
                {
                    visitor.visit(pointer);
                    
if(pointer.rightChild!=null)
                    {
                        stack.push(pointer.rightChild);
                    }
                    pointer
=pointer.leftChild;
                }
                
//go to right child
                else
                {
                    pointer
=stack.pop();
                    
                }
            }
        }
        
        
//no recursion , in-order
        void LNRVisit(NodeVistor visitor)
        {
            BinaryTreeNode
<T> pointer=root;
            Stack
<BinaryTreeNode<T>> stack=new Stack<BinaryTreeNode<T>>();
            
while(!stack.isEmpty()||pointer!=null)
            {
                
if(pointer!=null)
                {
                    stack.push(pointer);
                    
                    pointer
=pointer.leftChild;
                }
                
//no left child here, then visit root and then go to right child
                else
                {
                    pointer
=stack.pop();
                    visitor.visit(pointer);
                    pointer
=pointer.rightChild;
                    
                }
            }
        }
        
        
        
//no recursion ,post-order,this one is the most complex one.
        
//we need know from which side ,it back(left or right)
        void LRNVisit(NodeVistor visitor)
        {
            
if(root==null)return;
            BinaryTreeNode
<T> pointer=root;
            Stack
<BinaryTreeNode<T>> stack=new Stack<BinaryTreeNode<T>>();
            
while(true)
            {
                
                
//mark left 
                while(pointer!=null)
                {
                    stack.push(pointer);
                    pointer
=pointer.leftChild;
                }
                
                
                pointer
=stack.pop();
                
                
while(pointer.visitedRight)//back from right child, so we can visit it now.
                {
                    visitor.visit(pointer);
                    
if(stack.isEmpty())return;
                    pointer
=stack.pop();
                }
            
                pointer.visitedRight
=true;
                stack.push(pointer);
                
                pointer
=pointer.rightChild;
                
                
            }
            
        }
        
        
        
void levelOrder(NodeVistor visitor)
        {
            
if(root==null)return;
            BinaryTreeNode
<T> pointer=root;
            Vector
<BinaryTreeNode<T>> queue=new Vector<BinaryTreeNode<T>>();
            
            queue.add(pointer);
            
while(!queue.isEmpty())
            {
                BinaryTreeNode
<T> node=queue.remove(0);
                visitor.visit(node);
                
if(node.leftChild!=null)
                {
                    queue.add(node.leftChild);
                }
                
if(node.rightChild!=null)
                {
                    queue.add(node.rightChild);
                }
                
            }
            
        }
        
    }
    
static class BinaryTreeNode<T>
    {
        
        BinaryTreeNode(T data)
        {
            
this.data=data;
        }
        T data;
        
boolean visitedRight;
        BinaryTreeNode
<T> leftChild;
        BinaryTreeNode
<T> rightChild;
    }

    
/**
     * 
     
*/
    
public TreeDemo() {
        
// TODO Auto-generated constructor stub
    }

    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        BinaryTreeNode
<String> root=new BinaryTreeNode<String>("A");
        root.leftChild
=new BinaryTreeNode<String>("B");
        root.rightChild
=new BinaryTreeNode<String>("C");
        
        
        root.leftChild.leftChild
=new BinaryTreeNode<String>("D");
        
        root.rightChild.leftChild
=new BinaryTreeNode<String>("E");
        
        root.rightChild.rightChild
=new BinaryTreeNode<String>("F");
        
        root.rightChild.leftChild.rightChild
=new BinaryTreeNode<String>("G");
        
        root.rightChild.rightChild.leftChild
=new BinaryTreeNode<String>("H");
        root.rightChild.rightChild.rightChild
=new BinaryTreeNode<String>("I");
        
        NodeVistor visitor
=new NodeVistor()
        {

            @Override
            
public <T> void visit(BinaryTreeNode<T> node) {
                System.out.print(
"'"+node.data+"'");
                
            }
            
        };
        
        BinaryTree
<String> tree=new BinaryTree<String>(root);

        
        System.out.println(
"pre-order visit");
        tree.NLRVisit(visitor);
        System.out.println();
        System.out.println(
"in-order visit");
        
        tree.LNRVisit(visitor);
        
        System.out.println();
        System.out.println(
"post-order visit");
        
        tree.LRNVisit(visitor);
        
        System.out.println();
        System.out.println(
"level-order visit");
        
        tree.levelOrder(visitor);
    }

}


DoubleH 2007-10-11 16:05 鍙戣〃璇勮
]]>
琛ㄨ揪寮忔眰鍊糐ava綺楃硻鐗?/title><link>http://www.tkk7.com/javacap/archive/2007/10/09/151566.html</link><dc:creator>DoubleH</dc:creator><author>DoubleH</author><pubDate>Tue, 09 Oct 2007 14:48:00 GMT</pubDate><guid>http://www.tkk7.com/javacap/archive/2007/10/09/151566.html</guid><wfw:comment>http://www.tkk7.com/javacap/comments/151566.html</wfw:comment><comments>http://www.tkk7.com/javacap/archive/2007/10/09/151566.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.tkk7.com/javacap/comments/commentRss/151566.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/javacap/services/trackbacks/151566.html</trackback:ping><description><![CDATA[<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;"><br />  * <br />  </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br /> </span><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.yovn.algo;<br /> <br /> </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Stack;<br /> </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Vector;<br /> <br /> </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />  * </span><span style="color: #808080;">@author</span><span style="color: #008000;"> yovn<br />  *<br />  </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Caculator {<br /> <br />     <br />     </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Item<br />     {<br />         </span><span style="color: #0000ff;">boolean</span><span style="color: #000000;"> ops;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> value;<br />         <br />         Character opVal;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> opPriority;<br />     }<br />     <br />     Stack</span><span style="color: #000000;"><</span><span style="color: #000000;">Item</span><span style="color: #000000;">></span><span style="color: #000000;"> opStack</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Stack</span><span style="color: #000000;"><</span><span style="color: #000000;">Item</span><span style="color: #000000;">></span><span style="color: #000000;">();<br />     Vector</span><span style="color: #000000;"><</span><span style="color: #000000;">Item</span><span style="color: #000000;">></span><span style="color: #000000;"> calcStack</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Vector</span><span style="color: #000000;"><</span><span style="color: #000000;">Item</span><span style="color: #000000;">></span><span style="color: #000000;">();<br />     </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />      * <br />      </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Caculator() {<br />         </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated constructor stub</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    }<br />     <br />     <br />     <br />     <br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> calc()<br />     {<br />         Stack</span><span style="color: #000000;"><</span><span style="color: #000000;">Item</span><span style="color: #000000;">></span><span style="color: #000000;"> tmp</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Stack</span><span style="color: #000000;"><</span><span style="color: #000000;">Item</span><span style="color: #000000;">></span><span style="color: #000000;">();<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">calcStack.isEmpty())<br />         {<br />             Item it</span><span style="color: #000000;">=</span><span style="color: #000000;">calcStack.remove(</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br />             <br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">it.ops)<br />             {<br />                 tmp.push(it);<br />             }<br />             </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />             {<br />                 </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> op2</span><span style="color: #000000;">=</span><span style="color: #000000;">tmp.pop().value;<br />                 </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> op1</span><span style="color: #000000;">=</span><span style="color: #000000;">tmp.pop().value;<br />                 Item newItem</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Item();<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                 </span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(it.opVal)<br />                 {<br />                 </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                     newItem.value</span><span style="color: #000000;">=</span><span style="color: #000000;">op1</span><span style="color: #000000;">+</span><span style="color: #000000;">op2;<br />                     <br />                     </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">-</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                     newItem.value</span><span style="color: #000000;">=</span><span style="color: #000000;">op1</span><span style="color: #000000;">-</span><span style="color: #000000;">op2;<br />                     </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">*</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                     <br />                     newItem.value</span><span style="color: #000000;">=</span><span style="color: #000000;">op1</span><span style="color: #000000;">*</span><span style="color: #000000;">op2;<br />                     </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">/</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                     newItem.value</span><span style="color: #000000;">=</span><span style="color: #000000;">op1</span><span style="color: #000000;">/</span><span style="color: #000000;">op2;<br />                     </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 }<br />                 tmp.push(newItem);<br />             }<br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> tmp.pop().value;<br />     }<br />     </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />      * 1)鏁板瓧鐩存帴杈撳嚭<br />      * 2)寮鎷彿鍒欏帇鏍?br />      * 3錛夐棴鎷彿鎶婃爤涓厓绱犱緷嬈¤緭鍑虹洿鍒伴亣鍒板紑鎷彿<br />      * 4錛夎繍綆楃鏃?br />      *     a)寰幆錛屽綋鏍堥潪絀猴紝騫朵笖鏍堥《鍏冪礌涓嶆槸寮鎷彿錛屽茍涓旀爤欏惰繍綆楃浼樺厛綰т笉浣庝簬杈撳叆鐨勮繍綆楃鐨勪紭鍏堢駭錛屽弽澶嶅皢鍏惰緭鍑?br />      *     b錛夋妸杈撳叆榪愮畻絎﹀帇鏍?br />      * 5錛夎緭鍑烘爤鍐呭墿浣欏厓绱?br />      * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> in<br />      * </span><span style="color: #808080;">@return</span><span style="color: #008000;"><br />      </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String transInfixToPosfix(String in)<br />     {<br />         </span><span style="color: #0000ff;">char</span><span style="color: #000000;">[] cin</span><span style="color: #000000;">=</span><span style="color: #000000;">in.toCharArray();<br />         StringBuffer buffer</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuffer();<br />        <br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;"><</span><span style="color: #000000;">cin.length;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />         {<br />             Item newItem</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Item();<br />             newItem.opPriority</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />             newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />             <br />             </span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(cin[i])<br />             {<br />             <br />             </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                 newItem.opPriority</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                 newItem.opVal</span><span style="color: #000000;">=</span><span style="color: #000000;">'</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />                 doOps(buffer, newItem);<br />                 <br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />             </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">-</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                 newItem.opPriority</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                 newItem.opVal</span><span style="color: #000000;">=</span><span style="color: #000000;">'</span><span style="color: #000000;">-</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />                 doOps(buffer, newItem);<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />             </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">*</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                 newItem.opPriority</span><span style="color: #000000;">=</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                 newItem.opVal</span><span style="color: #000000;">=</span><span style="color: #000000;">'</span><span style="color: #000000;">*</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />                 doOps(buffer, newItem);<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />             </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">/</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                 newItem.opPriority</span><span style="color: #000000;">=</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                 newItem.opVal</span><span style="color: #000000;">=</span><span style="color: #000000;">'</span><span style="color: #000000;">/</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />                 doOps(buffer, newItem);<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 <br />             </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                 newItem.opVal</span><span style="color: #000000;">=</span><span style="color: #000000;">'</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />                 opStack.push(newItem);<br />                 <br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />             </span><span style="color: #0000ff;">case</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">'</span><span style="color: #000000;">:<br />                 </span><span style="color: #0000ff;">boolean</span><span style="color: #000000;"> meetClose</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />                 </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">opStack.isEmpty())<br />                 {<br />                     Item item</span><span style="color: #000000;">=</span><span style="color: #000000;">opStack.peek();<br />                     </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(item.ops</span><span style="color: #000000;">&&</span><span style="color: #000000;">item.opVal</span><span style="color: #000000;">!=</span><span style="color: #000000;">'</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">)<br />                     {<br />                         calcStack.add(item);<br />                         opStack.pop();<br />                         buffer.append(item.opVal);<br />                     }<br />                     </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(item.ops)<br />                     {<br />                         opStack.pop();<br />                         meetClose</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />                         </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                     }<br />                 }<br />                 </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">meetClose)<br />                 {<br />                     </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> RuntimeException();<br />                 }<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 <br />             </span><span style="color: #0000ff;">default</span><span style="color: #000000;">:<br />                 </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> j</span><span style="color: #000000;">=</span><span style="color: #000000;">i;<br />                 </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(;j</span><span style="color: #000000;"><</span><span style="color: #000000;">cin.length</span><span style="color: #000000;">&&</span><span style="color: #000000;">cin[j]</span><span style="color: #000000;">>=</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">&&</span><span style="color: #000000;">cin[j]</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;">;j</span><span style="color: #000000;">++</span><span style="color: #000000;">);<br />                 </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(j</span><span style="color: #000000;">==</span><span style="color: #000000;">i)<br />                 {<br />                     </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> RuntimeException(</span><span style="color: #000000;">"</span><span style="color: #000000;">wrong input<img src="http://www.tkk7.com/Images/dot.gif" alt="" />.</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />                 }<br />                 newItem.ops</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />                 newItem.value</span><span style="color: #000000;">=</span><span style="color: #000000;">Integer.parseInt(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> String(cin,i,j</span><span style="color: #000000;">-</span><span style="color: #000000;">i));<br />                 buffer.append(newItem.value);<br />                 calcStack.add(newItem);<br />                 i</span><span style="color: #000000;">=</span><span style="color: #000000;">j</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />                 <br />                 <br />             }<br />         }<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">opStack.isEmpty())<br />         {<br />             Item item</span><span style="color: #000000;">=</span><span style="color: #000000;">opStack.pop();<br />             calcStack.add(item);<br />             buffer.append(item.opVal);<br />             <br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> buffer.toString();<br />         <br />     }<br /> <br /> <br /> <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> doOps(StringBuffer buffer, Item newItem) {<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">opStack.isEmpty())<br />         {<br />             Item item</span><span style="color: #000000;">=</span><span style="color: #000000;">opStack.peek();<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(item.opVal</span><span style="color: #000000;">!=</span><span style="color: #000000;">'</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">&&</span><span style="color: #000000;">item.opPriority</span><span style="color: #000000;">>=</span><span style="color: #000000;">newItem.opPriority)<br />             {<br />                 calcStack.add(item);<br />                 opStack.pop();<br />                 buffer.append(item.opVal);<br />             }<br />             </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />             {<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />             }<br />         }<br />         opStack.push(newItem);<br />     }<br />     <br /> <br />     </span><span style="color: #008000;">/**</span><span style="color: #008000;"><br />      * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> args<br />      </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {<br />         Caculator calc</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Caculator();<br />         <br />         System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">1+2*3+7-(4/2+8)/5=</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">calc.transInfixToPosfix(</span><span style="color: #000000;">"</span><span style="color: #000000;">1+2*3+7-(4/2+8)/5</span><span style="color: #000000;">"</span><span style="color: #000000;">));<br />         System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">value is:</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">calc.calc());<br /> <br />     }<br /> <br /> }<br /> </span></div> <img src ="http://www.tkk7.com/javacap/aggbug/151566.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/javacap/" target="_blank">DoubleH</a> 2007-10-09 22:48 <a href="http://www.tkk7.com/javacap/archive/2007/10/09/151566.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Java2EXE Builder 1.0.0http://www.tkk7.com/javacap/archive/2007/07/15/130447.htmlDoubleHDoubleHSun, 15 Jul 2007 15:32:00 GMThttp://www.tkk7.com/javacap/archive/2007/07/15/130447.htmlhttp://www.tkk7.com/javacap/comments/130447.htmlhttp://www.tkk7.com/javacap/archive/2007/07/15/130447.html#Feedback9http://www.tkk7.com/javacap/comments/commentRss/130447.htmlhttp://www.tkk7.com/javacap/services/trackbacks/130447.html浠ュ墠鐨勫伐鍏瘋繍琛屾椂瑕佹妸鎵鏈夌殑class杞藉叆錛岃繖瀵逛簬澶х偣鐨勯」鐩槸涓嶅悎閫傜殑銆?
榪欎釜鐗堟湰璋冩暣浜嗕竴涓婰oader鐨勬満鍒訛紝淇濇寔EXE璺熺被鏂囦歡嫻戠劧涓浣撶殑鍚屾椂鍙堝彲浠ュ歡榪熷姞杞絚lass.
鍙﹀榪欎釜鐗堟湰鐨勫姞瀵嗗己搴︽彁楂樺緢澶氾紝鐢ㄦ潵浠f浛閭d簺class娣鋒穯杞歡搴旇涔熶笉閿欍傦細錛?nbsp; 闃呰鍏ㄦ枃

DoubleH 2007-07-15 23:32 鍙戣〃璇勮
]]>
KMP fast string findhttp://www.tkk7.com/javacap/archive/2007/07/10/129405.htmlDoubleHDoubleHTue, 10 Jul 2007 10:19:00 GMThttp://www.tkk7.com/javacap/archive/2007/07/10/129405.htmlhttp://www.tkk7.com/javacap/comments/129405.htmlhttp://www.tkk7.com/javacap/archive/2007/07/10/129405.html#Feedback0http://www.tkk7.com/javacap/comments/commentRss/129405.htmlhttp://www.tkk7.com/javacap/services/trackbacks/129405.html**
 
* Demonstrate KMP algorithm in Java
 
*
 
*
 
*/
public class KMP {
    
    
    
public static int indexOf(String target,String pattern)
    {
        
int pLen=pattern.length();
        
int tLen=target.length();
        
        
//the fail function
        int failFunc[]=new int[pLen];
        
        failFunc[
0]=-1;
        
        
//build fail function
        for(int i=1;i<pLen;i++)
        {
            
int j=failFunc[i-1];
            
while(pattern.charAt(i)!=pattern.charAt(j+1)&&j>=0)
            {
                
//recursion 
                j=failFunc[j];
            }
            
if(pattern.charAt(i)==pattern.charAt(j+1))
            {
                failFunc[i]
=j+1;
            }
            
else 
            {
                failFunc[i]
=-1;
            }
        }

        
int pPos=0,tPos=0;
        
        
while(tPos<tLen&&pPos<pLen)
        {
            
if(target.charAt(tPos)==pattern.charAt(pPos))
            {
                
//match ,then do forward
                tPos++;
                pPos
++;
            }
            
else if(pPos==0)
            {
                
//target go forward
                tPos++;
            }
            
else
            {
                
//target postion don't change,pattern go back  
                pPos=failFunc[pPos-1]+1;
            }
        }
        
        
if(pPos<pLen)return -1;
        
else return tPos-pLen;
        
        
        
    }

}



DoubleH 2007-07-10 18:19 鍙戣〃璇勮
]]>
OS Development Enivironment Setuphttp://www.tkk7.com/javacap/archive/2007/05/26/120176.htmlDoubleHDoubleHSat, 26 May 2007 06:25:00 GMThttp://www.tkk7.com/javacap/archive/2007/05/26/120176.htmlhttp://www.tkk7.com/javacap/comments/120176.htmlhttp://www.tkk7.com/javacap/archive/2007/05/26/120176.html#Feedback0http://www.tkk7.com/javacap/comments/commentRss/120176.htmlhttp://www.tkk7.com/javacap/services/trackbacks/120176.html2)Nasm 0.98.39
3)gcc args:
gcc -Wall -O -fomit-frame-pointer -nostdinc -fno-builtin

4)link.ld
ld -T link.ld
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
5)Bootable CD
mkisofs -R -b boot/grub/eltorito.s2 -no-emul-boot     -boot-load-size 4 -boot-info-table -o yovnos.iso iso



DoubleH 2007-05-26 14:25 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 日韩精品无码免费专区午夜| 亚洲手机中文字幕| 亚洲国产高清精品线久久| 日本黄页网站免费| 在线观看免费精品国产| 国产精品嫩草影院免费| 日本成人在线免费观看| 国产在线98福利播放视频免费| 国产精品免费播放| 免费在线一级毛片| 亚洲精品视频在线观看你懂的| 久久99亚洲综合精品首页| 亚洲综合无码精品一区二区三区| 亚洲色WWW成人永久网址| 亚洲AV无码久久精品色欲| 亚洲人成网站在线播放影院在线| 亚洲精品国产成人| 国产亚洲精品影视在线| 亚洲AV成人无码网天堂| 春意影院午夜爽爽爽免费| 你好老叔电影观看免费| 亚洲视频在线观看免费| 日韩一区二区a片免费观看| 午夜毛片不卡高清免费| 亚洲精品专区在线观看| 国产亚洲A∨片在线观看| 久久精品亚洲中文字幕无码麻豆| 亚洲www在线观看| 真人无码作爱免费视频| a级午夜毛片免费一区二区| 最近免费中文字幕大全免费| 99视频在线精品免费观看6| 人人狠狠综合久久亚洲高清| 国产亚洲人成无码网在线观看| 亚洲国产美女视频| 老司机福利在线免费观看| 久久国产乱子精品免费女| 三年片在线观看免费观看高清电影 | 久久精品国产96精品亚洲| 亚洲激情视频图片| eeuss免费天堂影院|