From 6168a24145acb7cbd13f1787f82bbaa59ae9b1e5 Mon Sep 17 00:00:00 2001 From: nibnait Date: Sun, 7 Aug 2016 23:24:29 +0800 Subject: [PATCH 01/36] 160807-first commit --- .idea/compiler.xml | 22 + .idea/copyright/profiles_settings.xml | 3 + .idea/description.html | 1 + .idea/misc.xml | 50 + .idea/modules.xml | 8 + .idea/project-template.xml | 3 + .idea/uiDesigner.xml | 124 ++ .idea/workspace.xml | 1308 +++++++++++++++++ README | 0 algorithms.iml | 11 + out/production/algorithms/Standard/Std.class | Bin 0 -> 410 bytes .../algorithms/Standard/StdOut.class | Bin 0 -> 3901 bytes .../algorithms/Standard/StdRandom.class | Bin 0 -> 1198 bytes .../algorithms/a_Sorting/a_Selection.class | Bin 0 -> 882 bytes .../algorithms/a_Sorting/b_Insertion.class | Bin 0 -> 848 bytes .../algorithms/a_Sorting/c_Shell.class | Bin 0 -> 916 bytes out/production/algorithms/aaaTest/Main.class | Bin 0 -> 532 bytes .../algorithms/aaaTest/RandomTest.class | Bin 0 -> 527 bytes .../algorithms/others/Odd_forward.class | Bin 0 -> 818 bytes src/Standard/Std.java | 18 + src/Standard/StdOut.java | 333 +++++ src/Standard/StdRandom.java | 73 + src/a_Sorting/a_Selection.java | 41 + src/a_Sorting/b_Insertion.java | 35 + src/a_Sorting/c_Shell.java | 45 + src/aaaTest/Main.java | 25 + src/aaaTest/RandomTest.java | 15 + src/others/Odd_forward.java | 41 + 28 files changed, 2156 insertions(+) create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/description.html create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/project-template.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/workspace.xml create mode 100644 README create mode 100644 algorithms.iml create mode 100644 out/production/algorithms/Standard/Std.class create mode 100644 out/production/algorithms/Standard/StdOut.class create mode 100644 out/production/algorithms/Standard/StdRandom.class create mode 100644 out/production/algorithms/a_Sorting/a_Selection.class create mode 100644 out/production/algorithms/a_Sorting/b_Insertion.class create mode 100644 out/production/algorithms/a_Sorting/c_Shell.class create mode 100644 out/production/algorithms/aaaTest/Main.class create mode 100644 out/production/algorithms/aaaTest/RandomTest.class create mode 100644 out/production/algorithms/others/Odd_forward.class create mode 100644 src/Standard/Std.java create mode 100644 src/Standard/StdOut.java create mode 100644 src/Standard/StdRandom.java create mode 100644 src/a_Sorting/a_Selection.java create mode 100644 src/a_Sorting/b_Insertion.java create mode 100644 src/a_Sorting/c_Shell.java create mode 100644 src/aaaTest/Main.java create mode 100644 src/aaaTest/RandomTest.java create mode 100644 src/others/Odd_forward.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..96cc43ef --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 00000000..e7bedf33 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 00000000..db5f1295 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..ee122540 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..815612d3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 00000000..1f08b887 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 00000000..e96534fb --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..d4825128 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,1308 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + Android + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + project + + + true + + + + DIRECTORY + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1470275964338 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + algorithms + + + + + + + + stdlib + + + + + + + + \ No newline at end of file diff --git a/README b/README new file mode 100644 index 00000000..e69de29b diff --git a/algorithms.iml b/algorithms.iml new file mode 100644 index 00000000..c90834f2 --- /dev/null +++ b/algorithms.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/algorithms/Standard/Std.class b/out/production/algorithms/Standard/Std.class new file mode 100644 index 0000000000000000000000000000000000000000..41cfee28c3900a255ac93f64e575f8fcf70cdf8c GIT binary patch literal 410 zcmYjMO;5r=6r62K%SWY%p9y&IhCR@WHw_mPlO~NEFkCn-ZKF$|m{Ra}c{1_f5Aa7B zXBQLgrf=SxH*enV*Z0RKfNPvLVdKQZsfRNG_f{3E8VT5gtEqrZmCLCtm9BqDr+QW!fp#*fWRb}-izZceLzm5~d=c;lvp9~mt&jrNEM}tk zYg|xOkI^-xi#l_EFF*rLRt>ZaqlZ%(Im96B4M$-w><`Cb|6=@S(>3iD mqtD?3v~g%G!~K|HIFEp*W7Y&zba+RHYr4#AvPV={IQ{{say;<> literal 0 HcmV?d00001 diff --git a/out/production/algorithms/Standard/StdOut.class b/out/production/algorithms/Standard/StdOut.class new file mode 100644 index 0000000000000000000000000000000000000000..59e9a92910db6474e1dcad3ab7c892524dcd6aba GIT binary patch literal 3901 zcmbW4TXYju6o&tkrkykm2&FArF1AH6w9w*(Yb#KCk(Sb`31F!TLy|&Dn#5#+sCdWw z{fc+-iuxvM=o00@r7o8b>ZAIoKB|xEa=Fxh&rH+lFsMrw-LvP+KHuJFpS{m%KmYUn zlK?Kln?W>TtP(BQ7ep8-5#u7#A~GVfBCZjU6Ok8Th$x8IFXCDe2Si*a;(8G`h&U+Z z8%4WG!_7g|VNAp=0o*Fj+eF+hpAQ9aN07F3+afMnkO|;U4R=+n|ETYrNBKX9yK#?v zxVIAb;eHJdXn3#^58+`Aj|9N)OYoSW9@p?h5Kkhl5bE00d3mHKx^u8|OOHbJz?i;Y zZ%gUvJ#7&qmrUPCiUhQy_t&ryZ2 zGcr5NBi_MQjAnA+_$&1V{fOZVNBFbdCGN%WL0d^2G!U^nB z&k26_1f3Ihn-eCf$uL}u z9-GnY&CuM8gJj(3Vq9V~HkBARS8R!9ZMe`_ytBP&Ka%znX+OJYRvGiI_MkCu)!wvU zNc)wv-&{1Spn2(+Xj3a?LC^7|@g&j|e?d|{7tJc|C?-qWSs|y5O$AA-A}!>iSw$Yj zWRd?jS`BHnq}91-QttQ2uN?MDhu}AlAz9)v)HJsgQM=uHxs3>^CRD3MCbG_&hV8sw zFLp}2&y*BQp-(rgEfh_h1^t}ZaR^Jf41qwXZGKf zwv)s53f7f%%xgaRGFDUi<3}LH-aN0BN7_*=q<#W*!^bh#96Gc5tRlD-^NN^%NU5WL zeI|WgX5Loy>~N?R2&k2eXcgwGHCV;>i&aNy;nLYxVGAPwufeOalh0lJse=Cx_|#eT zzoVQ%>b$?KyK#h2F>b70FoA`$W>4U3{{$MU8~q=mX}GKgizaYxWCDvN4(r^gBLeCY zl&ek5P9J8fel)1fj{R(8He$37AYMAY4%&{E9K(6&ko+54%BG?K literal 0 HcmV?d00001 diff --git a/out/production/algorithms/Standard/StdRandom.class b/out/production/algorithms/Standard/StdRandom.class new file mode 100644 index 0000000000000000000000000000000000000000..a56aef6478c002aa2a10bb59e164d2bb87115278 GIT binary patch literal 1198 zcmZWoOHUI~6#nj%cG}Vht$;8^s66VlXbXxjT1|}AqzMmW4GCf6w4Ky}>68pp!p@ES z0~aR7jV|1n_)36Cf_wjrdt0bC3s zf=e>8e&mAa!yI4F`*Ar)n^$Bkc&PwS+wC?_T$OQ+17>AhmvMu^XKPli*!w>FQ>=#}EcF6ha(lLb!=Ve z9!l1(W*W65O}0)uaV`<-k+2-SuG@Tr&;$QYord}UyiReV$JNTFUUe9PmcHi{n=Nqy zLu%*z1fuNNhE>n=Ba=C(WH~rbryrx6Ky}>~vd&eGc_ zlKmK=IMe}P6bh|4O@cDKc!JRUCx3!;0MC00Sx@-@0^F?&UWP{y4J)q@K1Sbq+^Y$A7#&O+UppNi;*nSX!2xskq+!uS5KlFkP<4yM9LSait7a+{e;AR&_(e> f?Ou{8Aqn&!BeH&o=x0d$s`Q?9m{w`ZNyz*L`?1F< literal 0 HcmV?d00001 diff --git a/out/production/algorithms/a_Sorting/a_Selection.class b/out/production/algorithms/a_Sorting/a_Selection.class new file mode 100644 index 0000000000000000000000000000000000000000..74d1b175ed53f9f70ea86fe09fd9e47255b4a50f GIT binary patch literal 882 zcmZuvOHUI~6#i~!relYJ*4CCnTOOj&4$z9hN93_FF=XT`XA$MT!Z|FDN7oKV)HpZ~1 zNZGiBh>3y?2PqS)HrB9iBdLs{jS?ORByVhxVAoyk*IR6xgJSV+Q~FKz*?dK{*CAnl zq(sM~`$Og%eNID3)e|>hk7sa##~);0z+|+{I2O4Gv|C;+tGG&8NGRm^_+hASWoJiQ>2`1 z(MjsXj|eCAofBPr`up3-WzS%oXw-i>pZQt~`f~#QIa+hXoF|_J%y39c{O!xs*Z9{T z(*G1I0l&?F(+v5hp>vQM%ue$)*#t}#?i0+?$Bp0-GSouIGM<2O2~!C12NNQG0po%M K1ab_8F!vAbp`Bm= literal 0 HcmV?d00001 diff --git a/out/production/algorithms/a_Sorting/b_Insertion.class b/out/production/algorithms/a_Sorting/b_Insertion.class new file mode 100644 index 0000000000000000000000000000000000000000..67d67d6daf2426797173b1e8592e456387af3674 GIT binary patch literal 848 zcmZuu&2AD=7(Ewe7#XKP!9staR_hN;6>Q?JwHp%?610h!ZdlX{Oz0rYkYUgl=ySN} zrY?15V@w~wH}C~~h(^V8r^K=-WbSvqpL@RZefP)TZ|4A>;)w|Z>n3hv!^9ojHIcxk ziF>$j;DLdM0*PJMbHhD>c)4;Qpgs4yjzGHMdd{oKu-= zAf!O7RToJ8r;OTAbh%ZpsEp|IQ`Tj0;CQ|8NMNBE%Fer&ax_zEHvLJ^abCD;j?A3E zs?ucP29gHK7AnXauq|xik%cAYJho6pOkKh<#QA9vxZ+zSqhiv@Vv;9C zjvN+IKsM@;zvN*g%Q~I89Bz?4srEVR_6ZvQz_xw9t34~OouFLYD}6-KE>-Q4)~%jk z(Jm33K|hX9Pp7|*W8!rB)Bem$@o22|Xa_6w)+nRMvxH6B3Q-LkkrbOvEXE61W}OZj lD@1C{?MRg3s$!Kf!CRnY?GJPzej_0+P`uz3K}xmB(mw~HohJYQ literal 0 HcmV?d00001 diff --git a/out/production/algorithms/a_Sorting/c_Shell.class b/out/production/algorithms/a_Sorting/c_Shell.class new file mode 100644 index 0000000000000000000000000000000000000000..cae8258a4ee384048f07efd50d396313ac10c86c GIT binary patch literal 916 zcmZuw-A)rh7(LVOk6jlEt&|FjfFhPfTTBQy{Dj1WgrthG1Y@|sX}h6|+mXhk4dnAsB0p0w+pK)UR=yl0(e)eAReHDD%FZr5b6B|~4u zy`&a3`~!h(S?*NYVdS@VOSPR!!wZ5%QZ=RD637*{%X{*zECrHqB??)!7`Kw)t~z`T zEFh#nVtZY{is@o{iY;uf�`}<5JdVGVofvQG)?jUnP>YH&5k$PoY(5cfy*t=BpX( zp17jYW8(%=CT487$eEb6@eq$}45^@Kql7tu!T;tG7`Pl}qq^tSBHj&~w(2shbG-Ts zy6q-qg*_*I72W8tUf<^>D67WdrQ264cyR~sJPC!KQCt6rd0Nj8aHrfEji8HEn`6Kal=vm1j?oP+O8qi=9ip5i%+! zoh%l4`pJ>SAcm2Nd*m*7n8~nCCzi!6vZvHt_PYIsMg(}}o-S$2&ZrR_!8V+RR$o5u z{&KVrSanv$MvqV&ThNBI_n2^pv?7;NFCHOFN4Y1^54G-b_vc|!9Cv@X)aw`l8NRt6 zNuG?yU!Y9jHlq=?9Hm5uLdF^K5%62!J6NS%V_fD-c#dhj#B4muJWos!A4Z;n9n6s@ wPbLbFhf+PZ$du<;IyT9k=kN~kVVHlQ3z0sDAqt24N822_$Qm9RclrI`9QD zZV>uUi%HLq+Ojt$l5?khIqt|PQ1KtA#m~W9VBeKew*7frf0RMkAj^bSz^--Ok$jQ$ zu?&axRvZQ4u(48+(U5)lf6@X%3M9Hs0kbt-M1B7uQ2J>_eXZCmq+l30v2cotfinx| zs0vuyGYDii!d`FW_hUY)70YlSqX9=5cnc!>j3X&q>o(O~)^-J-F@Kda0fX5l^Qc1O z64gn7n}fl7hf$qY(CSz35O1q=ns;giS}^Hb>jLc31Jc;TJ`vSCM3Qts+4wYFN7Fn< z*F9&6grn(8B$c?zSK-rg4c7j>rJ;-nvO?B9y)>Nucpi=Ak zWBDw-i3~?xJBd{|YNWGdJR*DRpJxFf1&m%(z-~wLco5tv?ZRCRxYU{saKG=Kf$2ZYu2>|_jg-sPNMn5Uue`V zT~v|fcJ(np9xUE#%siqXn&+<&FX?lhcP=gFVKa7?4OnLcXB literal 0 HcmV?d00001 diff --git a/out/production/algorithms/others/Odd_forward.class b/out/production/algorithms/others/Odd_forward.class new file mode 100644 index 0000000000000000000000000000000000000000..69411b099f4df54986435782144f044e1d023c6f GIT binary patch literal 818 zcmZuv%}x_h6#nk~blRbnN-Mt|6%cGY2~CVf6O<51Oqx_@Q6;He;BBYE)Y3N7A}_#8 z=)xE_Y}~k#7}E#P=WydA7!|)ekVqmkbLX7%-ShLEbMMzbpGN?0!?h8EW8*5W*)Vb4 z#yl1*ELylB5W63AgW;Nho-b?)7#lsmB`{tIx~(UByNy=(R5m(9CMvzA>}<*~Q1%&U z40nQoK(^8w?zF-|$@BfKm%VUbhW;vZ?aH7lFqyAc+VZt5b!2zDR2_yvcY9UU5^b(B zS3qAGZZlKn0#`an0j*BYRCOquuO7>ORNum+fLR`}Dih?(h35ixwYL{GTaN<8Iem`L zilUH&g}8;KB$jbAi5bjV$R|-ikq!Re12g<1?ls!2=8&Tyhpx{K*^2M&5i$F0Ny&V@ ztollx_aL*uzU&KF+%0pT!#8;z>d`?MasCsuN|b`qEPjIcNJWFqe=GuRj8RXXI#8x) zC2$GjL{#t(7<_>j>DU*$rW;<(Fuj_|6^nT_%kpaRxL0wHq3I8D?&uJA+}z4B^tV`c zM~1)Vyn~Z-4l(UI3Vwrxn{$p~9Ox%UC*Kb=adh%cMJrx<#A_vDl!8MR^O)ukXK)L% zSjQa7$ibujjCh^9TUdy^i{yBh_b`pi%(94kn4qR9-pC+J>0kp_Fv+f_Xk}skR`+yT z{6a=d{e;Pn)p>AdeLrNh8d_@B>>KZ48AmYm4+Kt0o(^y6-0)ISNkr*QXk+VY=yMFv GkoybxHj>By literal 0 HcmV?d00001 diff --git a/src/Standard/Std.java b/src/Standard/Std.java new file mode 100644 index 00000000..1bc011cc --- /dev/null +++ b/src/Standard/Std.java @@ -0,0 +1,18 @@ +package Standard; + +/** + * Created by nibnait on 2016/8/5. + */ +public class Std { + + public static void swap(int a[], int i, int j){ + + int temp; + temp = a[i]; + a[i] = a[j]; + a[j] = temp; +/* a[i] ^= a[j]; + a[j] ^= a[i]; + a[i] ^= a[j];*/ + } +} diff --git a/src/Standard/StdOut.java b/src/Standard/StdOut.java new file mode 100644 index 00000000..249ea49b --- /dev/null +++ b/src/Standard/StdOut.java @@ -0,0 +1,333 @@ +package Standard; + +/****************************************************************************** + * Compilation: javac StdOut.java + * Execution: java StdOut + * Dependencies: none + * + * Writes data of various types to standard output. + * + ******************************************************************************/ + +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.util.Locale; + +/** + * This class provides methods for printing strings and numbers to standard output. + *

+ * Getting started. + * To use this class, you must have StdOut.class in your + * Java classpath. If you used our autoinstaller, you should be all set. + * Otherwise, download + * StdOut.java + * and put a copy in your working directory. + *

+ * Here is an example program that uses StdOut: + *

+ *   public class TestStdOut {
+ *       public static void main(String[] args) {
+ *           int a = 17;
+ *           int b = 23;
+ *           int sum = a + b;
+ *           StdOut.println("Hello, World");
+ *           StdOut.printf("%d + %d = %d\n", a, b, sum);
+ *       }
+ *   }
+ *  
+ *

+ * Differences with System.out. + * The behavior of StdOut is similar to that of {@link System#out}, + * but there are a few subtle differences: + *

    + *
  • StdOut coerces the character-set encoding to UTF-8, + * which is a standard character encoding for Unicode. + *
  • StdOut coerces the locale to {@link Locale#US}, + * for consistency with {@link StdIn}, {@link Double#parseDouble(String)}, + * and floating-point literals. + *
  • StdOut flushes standard output after each call to + * print() so that text will appear immediately in the terminal. + *
+ *

+ * Reference. + * For additional documentation, + * see Section 1.5 of + * Introduction to Programming in Java: An Interdisciplinary Approach + * by Robert Sedgewick and Kevin Wayne. + * + * @author Robert Sedgewick + * @author Kevin Wayne + */ +public final class StdOut { + + // force Unicode UTF-8 encoding; otherwise it's system dependent + private static final String CHARSET_NAME = "UTF-8"; + + // assume language = English, country = US for consistency with StdIn + private static final Locale LOCALE = Locale.US; + + // send output here + private static PrintWriter out; + + // this is called before invoking any methods + static { + try { + out = new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true); + } + catch (UnsupportedEncodingException e) { + System.out.println(e); + } + } + + // don't instantiate + private StdOut() { } + + /** + * Closes standard output. + */ + public static void close() { + out.close(); + } + + /** + * Terminates the current line by printing the line-separator string. + */ + public static void println() { + out.println(); + } + + /** + * Prints an object to this output stream and then terminates the line. + * + * @param x the object to print + */ + public static void println(Object x) { + out.println(x); + } + + /** + * Prints a boolean to standard output and then terminates the line. + * + * @param x the boolean to print + */ + public static void println(boolean x) { + out.println(x); + } + + /** + * Prints a character to standard output and then terminates the line. + * + * @param x the character to print + */ + public static void println(char x) { + out.println(x); + } + + /** + * Prints a double to standard output and then terminates the line. + * + * @param x the double to print + */ + public static void println(double x) { + out.println(x); + } + + /** + * Prints an integer to standard output and then terminates the line. + * + * @param x the integer to print + */ + public static void println(float x) { + out.println(x); + } + + /** + * Prints an integer to standard output and then terminates the line. + * + * @param x the integer to print + */ + public static void println(int x) { + out.println(x); + } + + /** + * Prints a long to standard output and then terminates the line. + * + * @param x the long to print + */ + public static void println(long x) { + out.println(x); + } + + /** + * Prints a short integer to standard output and then terminates the line. + * + * @param x the short to print + */ + public static void println(short x) { + out.println(x); + } + + /** + * Prints a byte to standard output and then terminates the line. + *

+ * To write binary data, see {@link BinaryStdOut}. + * + * @param x the byte to print + */ + public static void println(byte x) { + out.println(x); + } + + /** + * Flushes standard output. + */ + public static void print() { + out.flush(); + } + + /** + * Prints an object to standard output and flushes standard output. + * + * @param x the object to print + */ + public static void print(Object x) { + out.print(x); + out.flush(); + } + + /** + * Prints a boolean to standard output and flushes standard output. + * + * @param x the boolean to print + */ + public static void print(boolean x) { + out.print(x); + out.flush(); + } + + /** + * Prints a character to standard output and flushes standard output. + * + * @param x the character to print + */ + public static void print(char x) { + out.print(x); + out.flush(); + } + + /** + * Prints a double to standard output and flushes standard output. + * + * @param x the double to print + */ + public static void print(double x) { + out.print(x); + out.flush(); + } + + /** + * Prints a float to standard output and flushes standard output. + * + * @param x the float to print + */ + public static void print(float x) { + out.print(x); + out.flush(); + } + + /** + * Prints an integer to standard output and flushes standard output. + * + * @param x the integer to print + */ + public static void print(int x) { + out.print(x); + out.flush(); + } + + /** + * Prints a long integer to standard output and flushes standard output. + * + * @param x the long integer to print + */ + public static void print(long x) { + out.print(x); + out.flush(); + } + + /** + * Prints a short integer to standard output and flushes standard output. + * + * @param x the short integer to print + */ + public static void print(short x) { + out.print(x); + out.flush(); + } + + /** + * Prints a byte to standard output and flushes standard output. + * + * @param x the byte to print + */ + public static void print(byte x) { + out.print(x); + out.flush(); + } + + /** + * Prints a formatted string to standard output, using the specified format + * string and arguments, and then flushes standard output. + * + * + * @param format the format string + * @param args the arguments accompanying the format string + */ + public static void printf(String format, Object... args) { + out.printf(LOCALE, format, args); + out.flush(); + } + + /** + * Prints a formatted string to standard output, using the locale and + * the specified format string and arguments; then flushes standard output. + * + * @param locale the locale + * @param format the format string + * @param args the arguments accompanying the format string + */ + public static void printf(Locale locale, String format, Object... args) { + out.printf(locale, format, args); + out.flush(); + } + + //-------------输出数组 begin-------------------------------- + + public static void print(int[] a){ + for (int i = 0; i < a.length; i++) { + if (i == a.length - 1) { + out.println(a[i]); + }else { + out.print(a[i]+", "); + } + } + } + + //-------------输出数组 end---------------------------------- + + + /** + * Unit tests some of the methods in StdOut. + */ + public static void main(String[] args) { + + // write to stdout + StdOut.println("Test"); + StdOut.println(17); + StdOut.println(true); + StdOut.printf("%.6f\n", 1.0/7.0); + } + +} diff --git a/src/Standard/StdRandom.java b/src/Standard/StdRandom.java new file mode 100644 index 00000000..074e1bd7 --- /dev/null +++ b/src/Standard/StdRandom.java @@ -0,0 +1,73 @@ +package Standard; + +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Created by nibnait on 2016/8/5. + */ +public class StdRandom { + + private static Random random = new Random(); //伪随机数发生器 + + private static double random(){ + return random.nextDouble(); + } + + /** + * 随机返回 [0,n)之间的一个int值 + * @param n + * @return + */ + public static int random(int n) { + if (n <= 0) throw new IllegalArgumentException("Parameter N must be positive"); + return random.nextInt(n); + } + + /** + * 随机返回 [a,b)之间的一个double值 + * @param a + * @param b + * @return + */ + public static double random(double a, double b) { + if (!(a < b)) throw new IllegalArgumentException("Invalid range"); + return a + random() * (b-a); + } + + /** + * 随机返回 [a,b)之间的一个int值 + * @param a + * @param b + * @return + */ + public static int random(int a, int b) { + if (b <= a) throw new IllegalArgumentException("Invalid range"); + if ((long) b - a >= Integer.MAX_VALUE) throw new IllegalArgumentException("Invalid range"); + return a + random(b - a); + } + + /** + * 返回一个[0,100)范围的随机int型数组 + * @param a + * @return + */ + public static int[] random(int[] a){ + for (int i = 0; i < a.length; i++) { + a[i] = random(100); + } + return a; + } +/* public static int[] random(int[] a){ + int length = a.length; + IntStream intStream = random.ints(0,100); //创建一个int型 [0,100)范围的数字流 + + List list = intStream.limit(length).boxed().collect(Collectors.toList()); + for (int i = 0; i < length; i++) { + a[i] = list.get(i); + } + return a; + }*/ +} diff --git a/src/a_Sorting/a_Selection.java b/src/a_Sorting/a_Selection.java new file mode 100644 index 00000000..c8ce3553 --- /dev/null +++ b/src/a_Sorting/a_Selection.java @@ -0,0 +1,41 @@ +package a_Sorting; + +import Standard.Std; +import Standard.StdOut; +import Standard.StdRandom; + +/** + * Created by nibnait on 2016/8/5. + */ +public class a_Selection { + public static void main(String[] args) { + int[] a = new int[10]; + a = StdRandom.random(a); + StdOut.print(a); + + a = Selection_Sort(a); + StdOut.print(a); + } + + /** + * 不断的选择剩余元素中的最小者 + * 比较 N*(N-1)/2次 + * 交换 N-1次 + * + * 时间复杂度:O(N^2) + */ + public static int[] Selection_Sort(int[] a) { + + int N = a.length; + int min = 0; + for (int i = 0; i < N - 1; i++) { + for (int j = i + 1; j < N; j++) { + if (a[min] > a[j]) { + min = j; + } + } + Std.swap(a, i, min); + } + return a; + } +} diff --git a/src/a_Sorting/b_Insertion.java b/src/a_Sorting/b_Insertion.java new file mode 100644 index 00000000..ea60cce2 --- /dev/null +++ b/src/a_Sorting/b_Insertion.java @@ -0,0 +1,35 @@ +package a_Sorting; + +import Standard.Std; +import Standard.StdOut; +import Standard.StdRandom; + +/** + * Created by nibnait on 2016/8/7. + */ +public class b_Insertion { + public static void main(String[] args) { + int[] a = new int[10]; + a = StdRandom.random(a); + StdOut.print(a); + + a = Insertion_Sort(a); + StdOut.print(a); + } + + /** + * 适用于元素基本(接近)有序的数组, + */ + public static int[] Insertion_Sort(int[] a) { + + int length = a.length; + + for (int i = 1; i < length; i++) { + for (int j = i; j > 0 && a[j]=1){ + for (int i = h; i < length; i++) {//将数组整理成【h有序数组】, + for (int j = i; j > 0 && a[j] 0 && IsOdd(a[j]) && !IsOdd(a[j-1]); j--) { + Std.swap(a,j,j-1); + } + }*/ + + //时间复杂度:O(n)的方法: + //两个指针 + for (int i = 0; i < a.length; i++) { + if (!IsOdd(a[i])) { + for (int j = a.length - 1; j > 0 && i < j; j--) { + if (IsOdd(a[j])) { + Std.swap(a, i, j); + } + } + } + } + StdOut.print(a); + } + + private static boolean IsOdd(int i) { + return i % 2 == 1; + } + + +} From 374cd0c4df6580143d148bd43153972394b5a9ed Mon Sep 17 00:00:00 2001 From: nibnait Date: Mon, 8 Aug 2016 23:34:31 +0800 Subject: [PATCH 02/36] =?UTF-8?q?160808=20=E9=B1=BC=E9=A6=99=E8=82=89?= =?UTF-8?q?=E4=B8=9D=EF=BC=8C=E6=9C=89=E7=82=B9=E5=92=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 6 + .idea/workspace.xml | 228 ++++++++++++------ .../algorithms/a_Sorting/c_Shell.class | Bin 916 -> 908 bytes .../algorithms/a_Sorting/d_Merge.class | Bin 0 -> 1256 bytes out/production/algorithms/aaaTest/Main.class | Bin 532 -> 415 bytes .../algorithms/others/Matrix_Print1.class | Bin 0 -> 1553 bytes src/a_Sorting/d_Merge.java | 60 +++++ src/aaaTest/Main.java | 9 +- src/others/Matrix_Print1.java | 43 ++++ 9 files changed, 265 insertions(+), 81 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 out/production/algorithms/a_Sorting/d_Merge.class create mode 100644 out/production/algorithms/others/Matrix_Print1.class create mode 100644 src/a_Sorting/d_Merge.java create mode 100644 src/others/Matrix_Print1.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d4825128..844cf42c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,12 @@ - + + + + + + @@ -33,7 +38,7 @@ - + @@ -42,11 +47,23 @@ - + - - + + + + + + + + + + + + + + @@ -54,6 +71,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -63,6 +100,9 @@ + + @@ -132,7 +174,7 @@ - + @@ -150,6 +192,7 @@ + @@ -171,7 +214,6 @@ - @@ -289,15 +331,15 @@ - - + + - - - + - - - + - - - + - - - + - - - - - - + + + + + - - - - - + + + + + @@ -836,11 +878,15 @@ + + + + - @@ -854,7 +900,7 @@ - + @@ -863,7 +909,7 @@ - + @@ -886,7 +932,7 @@ - @@ -895,6 +941,36 @@

* Getting started. - * To use this class, you must have StdOut.class in your + * To use this class, you must have stdOut.class in your * Java classpath. If you used our autoinstaller, you should be all set. * Otherwise, download - * StdOut.java + * stdOut.java * and put a copy in your working directory. *

- * Here is an example program that uses StdOut: + * Here is an example program that uses stdOut: *

  *   public class TestStdOut {
  *       public static void main(String[] args) {
  *           int a = 17;
  *           int b = 23;
  *           int sum = a + b;
- *           StdOut.println("Hello, World");
- *           StdOut.printf("%d + %d = %d\n", a, b, sum);
+ *           stdOut.println("Hello, World");
+ *           stdOut.printf("%d + %d = %d\n", a, b, sum);
  *       }
  *   }
  *  
*

* Differences with System.out. - * The behavior of StdOut is similar to that of {@link System#out}, + * The behavior of stdOut is similar to that of {@link System#out}, * but there are a few subtle differences: *

    - *
  • StdOut coerces the character-set encoding to UTF-8, + *
  • stdOut coerces the character-set encoding to UTF-8, * which is a standard character encoding for Unicode. - *
  • StdOut coerces the locale to {@link Locale#US}, + *
  • stdOut coerces the locale to {@link Locale#US}, * for consistency with {@link StdIn}, {@link Double#parseDouble(String)}, * and floating-point literals. - *
  • StdOut flushes standard output after each call to + *
  • stdOut flushes standard output after each call to * print() so that text will appear immediately in the terminal. *
*

@@ -59,7 +59,7 @@ * @author Robert Sedgewick * @author Kevin Wayne */ -public final class StdOut { +public final class stdOut { // force Unicode UTF-8 encoding; otherwise it's system dependent private static final String CHARSET_NAME = "UTF-8"; @@ -81,7 +81,7 @@ public final class StdOut { } // don't instantiate - private StdOut() { } + private stdOut() { } /** * Closes standard output. @@ -319,15 +319,15 @@ public static void print(int[] a){ /** - * Unit tests some of the methods in StdOut. + * Unit tests some of the methods in stdOut. */ public static void main(String[] args) { // write to stdout - StdOut.println("Test"); - StdOut.println(17); - StdOut.println(true); - StdOut.printf("%.6f\n", 1.0/7.0); + stdOut.println("Test"); + stdOut.println(17); + stdOut.println(true); + stdOut.printf("%.6f\n", 1.0/7.0); } } diff --git a/src/Standard/StdRandom.java b/src/Standard/stdRandom.java similarity index 98% rename from src/Standard/StdRandom.java rename to src/Standard/stdRandom.java index 074e1bd7..d7e7c639 100644 --- a/src/Standard/StdRandom.java +++ b/src/Standard/stdRandom.java @@ -8,7 +8,7 @@ /** * Created by nibnait on 2016/8/5. */ -public class StdRandom { +public class stdRandom { private static Random random = new Random(); //伪随机数发生器 diff --git a/src/a_Sorting/a_Selection.java b/src/a_Sorting/a_Selection.java index c8ce3553..6c9cb112 100644 --- a/src/a_Sorting/a_Selection.java +++ b/src/a_Sorting/a_Selection.java @@ -1,8 +1,8 @@ package a_Sorting; -import Standard.Std; -import Standard.StdOut; -import Standard.StdRandom; +import Standard.std; +import Standard.stdOut; +import Standard.stdRandom; /** * Created by nibnait on 2016/8/5. @@ -10,11 +10,11 @@ public class a_Selection { public static void main(String[] args) { int[] a = new int[10]; - a = StdRandom.random(a); - StdOut.print(a); + a = stdRandom.random(a); + stdOut.print(a); a = Selection_Sort(a); - StdOut.print(a); + stdOut.print(a); } /** @@ -34,7 +34,7 @@ public static int[] Selection_Sort(int[] a) { min = j; } } - Std.swap(a, i, min); + std.exch(a, i, min); } return a; } diff --git a/src/a_Sorting/b_Insertion.java b/src/a_Sorting/b_Insertion.java index ea60cce2..a8397c32 100644 --- a/src/a_Sorting/b_Insertion.java +++ b/src/a_Sorting/b_Insertion.java @@ -1,8 +1,8 @@ package a_Sorting; -import Standard.Std; -import Standard.StdOut; -import Standard.StdRandom; +import Standard.std; +import Standard.stdOut; +import Standard.stdRandom; /** * Created by nibnait on 2016/8/7. @@ -10,11 +10,11 @@ public class b_Insertion { public static void main(String[] args) { int[] a = new int[10]; - a = StdRandom.random(a); - StdOut.print(a); + a = stdRandom.random(a); + stdOut.print(a); a = Insertion_Sort(a); - StdOut.print(a); + stdOut.print(a); } /** @@ -26,7 +26,7 @@ public static int[] Insertion_Sort(int[] a) { for (int i = 1; i < length; i++) { for (int j = i; j > 0 && a[j]=1){ for (int i = h; i < length; i++) {//将数组整理成【h有序数组】, for (int j = i; j > 0 && a[j]= 0; k--) { + sink(a, k, N); + } + + while (N > 0) { + std.exch(a, 0, N--); //将堆的最大元素a[0]和a[N]交换, + sink(a, 0, N); //调整堆, 知道堆空 + } + + return a; + } + + //自下向上的堆有序化【下沉】 + private static void sink(int[] a, int k, int N) { + while (2 * k < N) { + int j = 2 * k+1; //a[j]为 a[k]的左子结点 + if (j < N && a[j] < a[j + 1]) { + j++; //如果左子结点比右子结点大, 则j++ + } + if (a[k] > a[j]) { //如果a[k] 比他的最大的子结点还大,则无需调整 + break; + } else { //否则 交换a[k] 和a[j] + std.exch(a, j, k); + k = j; // 然后 继续 a[k]的子结点和a[k]的孙结点进行比较 + } + } + } +} \ No newline at end of file diff --git a/src/aaaTest/Main.java b/src/aaaTest/Main.java index 0ec7f6cc..f1d5b39d 100644 --- a/src/aaaTest/Main.java +++ b/src/aaaTest/Main.java @@ -1,14 +1,6 @@ package aaaTest; -import Standard.Std; -import Standard.StdOut; - -import java.util.List; -import java.util.Random; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - public class Main { public static void main(String[] args) { diff --git a/src/aaaTest/RandomTest.java b/src/aaaTest/RandomTest.java index 4b5bed43..54aeeaa8 100644 --- a/src/aaaTest/RandomTest.java +++ b/src/aaaTest/RandomTest.java @@ -1,7 +1,7 @@ package aaaTest; -import Standard.StdOut; -import Standard.StdRandom; +import Standard.stdOut; +import Standard.stdRandom; /** * Created by nibnait on 2016/8/6. @@ -10,6 +10,6 @@ public class RandomTest { public static void main(String[] args) { int[] a = new int[10]; - StdOut.print(StdRandom.random(a)); + stdOut.print(stdRandom.random(a)); } } diff --git a/src/others/BitMap1.java b/src/others/BitMap1.java index d82e2c72..bbff4b33 100644 --- a/src/others/BitMap1.java +++ b/src/others/BitMap1.java @@ -1,6 +1,6 @@ package others; -import Standard.StdOut; +import Standard.stdOut; /** * Created by nibnait on 2016/8/9. @@ -13,7 +13,7 @@ public static void main(String[] args) { for(i=0;i<5;){ if(b[a[i]]==1){ - StdOut.print("数组含有重复值"); + stdOut.print("数组含有重复值"); break; } else{ @@ -23,7 +23,7 @@ public static void main(String[] args) { } if(i==5){ - StdOut.print("数组含没有重复值"); + stdOut.print("数组含没有重复值"); } } } diff --git a/src/others/Odd_forward.java b/src/others/Odd_forward.java index 5d41e4d1..d1469383 100644 --- a/src/others/Odd_forward.java +++ b/src/others/Odd_forward.java @@ -1,7 +1,7 @@ package others; -import Standard.Std; -import Standard.StdOut; +import Standard.std; +import Standard.stdOut; /** * 奇数放在偶数的前面。(插入排序的变异) @@ -11,11 +11,11 @@ public class Odd_forward { public static void main(String[] args) { int[] a = {1, 2, 2, 3, 4, 5}; - StdOut.print(a); + stdOut.print(a); /*for (int i = 1; i < a.length; i++) { for (int j = i; j > 0 && IsOdd(a[j]) && !IsOdd(a[j-1]); j--) { - Std.swap(a,j,j-1); + std.swap(a,j,j-1); } }*/ @@ -25,12 +25,12 @@ public static void main(String[] args) { if (!IsOdd(a[i])) { for (int j = a.length - 1; j > 0 && i < j; j--) { if (IsOdd(a[j])) { - Std.swap(a, i, j); + std.exch(a, i, j); } } } } - StdOut.print(a); + stdOut.print(a); } private static boolean IsOdd(int i) { From c34f5a9ed4783bed29c09229494ca3bbd7a885e6 Mon Sep 17 00:00:00 2001 From: nibnait Date: Sat, 20 Aug 2016 04:25:54 +0800 Subject: [PATCH 05/36] =?UTF-8?q?=E4=BB=80=E4=B9=88=E7=8A=B6=E5=86=B5?= =?UTF-8?q?=EF=BC=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 376 ++++++++++-------- .../algorithms/a_Sorting/c_Shell.class | Bin 908 -> 902 bytes .../algorithms/a_Sorting/d_Merge.class | Bin 1164 -> 1164 bytes .../algorithms/a_Sorting/e_Quick.class | Bin 1033 -> 1018 bytes .../algorithms/a_Sorting/f_Deap.class | Bin 1023 -> 1041 bytes .../{aaaTest => zzzTest}/Main.class | Bin 415 -> 415 bytes .../{aaaTest => zzzTest}/RandomTest.class | Bin 527 -> 527 bytes src/a_Sorting/c_Shell.java | 19 +- src/a_Sorting/d_Merge.java | 2 +- src/a_Sorting/e_Quick.java | 23 +- src/a_Sorting/f_Deap.java | 16 +- src/{aaaTest => zzzTest}/Main.java | 2 +- src/{aaaTest => zzzTest}/RandomTest.java | 2 +- 13 files changed, 252 insertions(+), 188 deletions(-) rename out/production/algorithms/{aaaTest => zzzTest}/Main.class (69%) rename out/production/algorithms/{aaaTest => zzzTest}/RandomTest.class (75%) rename src/{aaaTest => zzzTest}/Main.java (87%) rename src/{aaaTest => zzzTest}/RandomTest.java (93%) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index fe976448..d18b3f8f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,20 +2,18 @@ - - - - + + + + + + + - - - - - - + @@ -53,15 +51,57 @@ - - + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -92,14 +132,15 @@ @@ -167,10 +208,30 @@ + + + + + + + + + + + + + + + + + - @@ -203,7 +264,7 @@ @@ -221,7 +282,7 @@ - - - - - - - - - - - - - - + @@ -295,7 +350,7 @@ - + @@ -307,14 +362,14 @@ - + - - + - - + - - - + - - - + - - - - - - + + + + + - - - - + + + + @@ -860,11 +915,21 @@ + + + + + + + + + + - @@ -872,30 +937,30 @@ - - - + + + - - - - - + + + + - + - - + - + - + + + @@ -911,7 +976,15 @@ - @@ -920,17 +993,7 @@

+ * Created by nibnait on 2016/9/2. + */ +public class BST, Value> { + + private Node root; + + public int size() { + return size(root); + } + + private int size(Node x) { + if (x == null) { + return 0; + } else { + return x.N; + } + } + + public Value get(Key key) { + //在以x为根结点的子树中查找 key对应的Value值 + //如果没有,则返回null + return get(root, key); + } + + private Value get(Node x, Key key) { + if (x == null) { + return null; + } + + int cmp = x.key.compareTo(key); + if (cmp > 0) { + return get(x.left, key); + } else if (cmp < 0) { + return get(x.right, key); + } else { + return (Value) x.value; + } + } + + public void put(Key key, Value value) { + //查找key, 存在在更新此key对应的value,否则为其创建新结点 + this.root = put(root, key, value); + } + + private Node put(Node x, Key key, Value value) { + if (x == null) { + return new Node(key, value, 1); + } + + int cmp = x.key.compareTo(key); + if (cmp > 0) { + x.left = put(x.left, key, value); + } else if (cmp < 0) { + x.right = put(x.right, key, value); + } else { + x.value = value; + } + x.N = size(x.left) + size(x.right) + 1; + return x; + } + + public Key min() { + return (Key) min(root).key; + } + + private Node min(Node x) { + + if (x == null) { + return x; + } + return x.left; + } + + public Key max() { + return (Key) max(root).key; + } + + private Node max(Node x) { + + if (x == null) { + return x; + } + return x.right; + } + + public Key floor(Key key) { + Node x = floor(root, key); + if (x == null) { + return null; + } + return (Key) x.key; + } + + private Node floor(Node x, Key key) { + + if (x == null) { + return null; + } + int cmp = x.key.compareTo(key); + //如果x的key大于 要查找的key + //则floor(x, key)一定在x的左子树中。 + if (cmp == 0) { + return x; + } + if (cmp > 0) { + return floor(x.left, key); + } + //如果x的key小于 要查找的key + //那么 只有当x的右子树中存在小于等于给定key的结点时, floor(x, key)才会出现在x的右子树中 + //否则x就是floor(x, key) 即小于等于key的最大键。 + Node t = floor(x.right, key); + if (t != null) { + return t; + } else { + return x; + } + } + + //向上取整 + public Key ceiling(Key key) { + Node x = ceiling(root, key); + if (x == null) { + return null; + } + return (Key) x.key; + } + + private Node ceiling(Node x, Key key) { + + if (x == null) { + return null; + } + int cmp = x.key.compareTo(key); + if (cmp == 0) { + return x; + } + //向上取整:如果x的key比 要查找的key小, 则ceiling(x, key) 一定在x的右子树中 + if (cmp < 0) { + return ceiling(x.right, key); + } + Node t = ceiling(x.left, key); + if (t != null) { + return t; + } else { + return x; + } + } + + //查找排名为k的键 + private Key select(int k) { + return (Key) select(root, k).key; + } + + private Node select(Node x, int k) { + if (x == null) { + return null; + } + int t = size(x.left); + if (t > k) { + return select(x.left, k); + } else if (t < k) { + return select(x.right, k - t - 1); + } else { + return x; + } + } + + //求给定key的排名 + public int rank(Key key) { + return rank(root, key); + } + + private int rank(Node x, Key key) { + + if (x == null){ + return 0; + } + int cmp = x.key.compareTo(key); + if (cmp > 0){ + return rank(x.left, key); + }else if (cmp < 0){ + return rank(x.right, key) + size(x.left) + 1; + }else { + return size(x.left); + } + } + + public void deleteMin(){ + root = deleteMin(root); + } + + private Node deleteMin(Node x) { + + if (x.left == null){ + return x.right; + } + x.left = deleteMin(x.left); + x.N = size(x.left) + size(x.right) + 1; + return x; + } + + public void deleteMax(){ + root = deleteMax(root); + } + + private Node deleteMax(Node x) { + if (x.right == null){ + return x.left; + } + x.right = deleteMax(x.right); + x.N = size(x.left) + size(x.right) + 1; + return x; + } + + public void delete(Key key){ + root = delete(root, key); + } + + private Node delete(Node x, Key key) { + + if (x == null){ + return null; + } + int cmp = x.key.compareTo(key); + if (cmp > 0){ + x.left = delete(x.left, key); + }else if (cmp < 0){ + x.right = delete(x.right, key); + }else { // 找到了 + if (x.right == null){ + return x.left; + } + if (x.left == null){ + return x.right; + } + Node t = x; // t 即为待删除的结点 + x = min(t.right); //x可以说是t向上取整(第一个比t大的结点) + x.right = deleteMin(t.right); + x.left = t.left; + } + x.N = size(x.left) + size(x.right) + 1; //以x为根结点的结点总数 + return x; + } + +} diff --git a/src/Algorithms_4thEdition/b_Searching/Node.java b/src/Algorithms_4thEdition/b_Searching/Node.java new file mode 100644 index 00000000..1002a7b7 --- /dev/null +++ b/src/Algorithms_4thEdition/b_Searching/Node.java @@ -0,0 +1,19 @@ +package Algorithms_4thEdition.b_Searching; + +/** + * Created by nibnait on 2016/9/2. + */ +public class Node, Value>{ + + public Key key; //键 + public Value value; //值 + public Node left, right; //指向子树的链接 + public int N; //以该结点为根的子树中结点总数 + + public Node(Key key, Value value, int N){ + this.key = key; + this.value = value; + this.N = N; + } + +} diff --git a/src/Algorithms_4thEdition/b_Searching/NodeSimple.java b/src/Algorithms_4thEdition/b_Searching/NodeSimple.java new file mode 100644 index 00000000..621a7c0e --- /dev/null +++ b/src/Algorithms_4thEdition/b_Searching/NodeSimple.java @@ -0,0 +1,18 @@ +package Algorithms_4thEdition.b_Searching; + +/** + * + * Created by nibnait on 2016/9/2. + */ +public class NodeSimple { + private int key; //键 int + private String value; //值 String + private NodeSimple left, right; //指向子树的链接 + private int N; //以该节点为根的子树中的结点总数 + + public NodeSimple(int key, String value, int n) { + this.key = key; + this.value = value; + N = n; + } +} diff --git a/src/Standard/std.java b/src/Standard/std.java index 42de434c..3c5c5dbc 100644 --- a/src/Standard/std.java +++ b/src/Standard/std.java @@ -11,9 +11,9 @@ public static void exch(int[] a, int i, int j){ temp = a[i]; a[i] = a[j]; a[j] = temp; -/* a[i] ^= a[j]; - a[j] ^= a[i]; - a[i] ^= a[j];*/ +/* Algorithms_4thEdition[i] ^= Algorithms_4thEdition[j]; + Algorithms_4thEdition[j] ^= Algorithms_4thEdition[i]; + Algorithms_4thEdition[i] ^= Algorithms_4thEdition[j];*/ } } diff --git a/src/Standard/stdOut.java b/src/Standard/stdOut.java index cf0dfd6e..7c041d71 100644 --- a/src/Standard/stdOut.java +++ b/src/Standard/stdOut.java @@ -21,28 +21,28 @@ * To use this class, you must have stdOut.class in your * Java classpath. If you used our autoinstaller, you should be all set. * Otherwise, download - * stdOut.java - * and put a copy in your working directory. + * stdOut.java + * and put Algorithms_4thEdition copy in your working directory. *

* Here is an example program that uses stdOut: *

  *   public class TestStdOut {
  *       public static void main(String[] args) {
- *           int a = 17;
+ *           int Algorithms_4thEdition = 17;
  *           int b = 23;
- *           int sum = a + b;
+ *           int sum = Algorithms_4thEdition + b;
  *           stdOut.println("Hello, World");
- *           stdOut.printf("%d + %d = %d\n", a, b, sum);
+ *           stdOut.printf("%d + %d = %d\n", Algorithms_4thEdition, b, sum);
  *       }
  *   }
  *  
*

* Differences with System.out. * The behavior of stdOut is similar to that of {@link System#out}, - * but there are a few subtle differences: + * but there are Algorithms_4thEdition few subtle differences: *