From f28a334dfecd4627fcdd0a3597339b409b85eb78 Mon Sep 17 00:00:00 2001 From: iMeet07 Date: Tue, 3 Oct 2023 22:13:47 +0530 Subject: [PATCH] CycleSort Added Cycle Sort Algorithm --- .DS_Store | Bin 0 -> 8196 bytes algorithms/.DS_Store | Bin 0 -> 10244 bytes algorithms/math/.DS_Store | Bin 0 -> 6148 bytes algorithms/sorting/CycleSort.m | 45 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 .DS_Store create mode 100644 algorithms/.DS_Store create mode 100644 algorithms/math/.DS_Store create mode 100644 algorithms/sorting/CycleSort.m diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8b26fa1cad73e317b391f79167db9f61e89f8a42 GIT binary patch literal 8196 zcmeHM-A)rh6g~qeTM=0V6pSW~y)l6(3NbOpSPMppF(4&af>^h^Ls{9*G`kC^grwJc z%@)OK@!eNi z(;5a01OFuh;{A}uDln_Drck~*u#qVMVh481f->d-(kHDktFfj~x?)U~JrM0mbc#Wg zJL=QI95Ji0rck*9QSLzW$V6u-M2?PrX=x55tI)KD0mHyv21M?D1RMyU47@&np9;8J z3U~?i;z-@Vdj6*@D7Ek@ZA4Y=^R`qC{3sqB{Y={0I}RQ0v^uRWYbn3NH}bI)uSGd0 zTok|NI1fsR?3Ctxx>23FYyz%UfG1Yanys>TGc}s4)Iq&K`(z z%U2!OXXQdnpTv3p878db`=4luq?(r`y1x|?u(=5m1m{%5@Fmza% z=AH^m5zXanCZr-#!ADuFC3F3|qBN7elL+UOU_(X;0GX_otp21+BT3CM&S)x=ok>h! z7$3sBFb}Kn3|_)3cn$C1BYc5x@Pl-cA##=slW}s5Tqn25Ju*vN;t`)bR{m|thM#F1 ziHM7;rUxJ>U(CC_QdRaJezdZy@X1O2(2H-7b8Y#(I4n(LR=D@uw$+uo@!Q^rX*7+w zo)1~TSsC?Wjh%RueQ<#S!%H~Yc#Mfyef=>`;gP3!{5`y~qi`NuZMks43Lh={A3Pimi&#hdy*p8PGfz9RTN=YQz%`qk?A<1Ove$o|6z!7 f8cl^yR%1;e_F(zv7Xg2e5SZ`3+$z%?AO?N`tK*en literal 0 HcmV?d00001 diff --git a/algorithms/.DS_Store b/algorithms/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5e1e7f841a4237c6b196a3f6205af85ffefb7f42 GIT binary patch literal 10244 zcmeHM-D(p-6h51#&4w!W!h#~gf(Rm^rau%!gc#%BT`{5;DltE661v$9$;MD>A(x^z zUJJf~;FaPF_zFIN_dbJuXJ(r2Y&N8{3I%6i=IqX#Z}yz;?9AkBh{!4q8mmMO5!t9L zmrkKcC_K(}qNG$$4$=aiD5NTRRG~h#C$vq6VZbn87%&VN1`GoyfdQP^TXH+4DkNopt39|Ig(Or9cV-f0GUR&D7Z!*ATW-S1tmvP3Ml53*@LB^mP#=!3CFm@ z>W~E`M^Z{SSrSf`da|W56iYlEI7g_HRgluOh5^ICBmDYPlYCx&lw>FGriD6O>BIE^G*+ z+Rc~W*|sBi*a;ecX{K%~>Y2k1UZ8v6cA)sIg3$`y7cpqzY71k`bcODs&qGvPL|}zF zldM};BdjkWhCN_8nl9!HF+)Q{jbn%V82&RXiAHTEUXPLW75JH$DG@mYE|LG4IGt^A z8Y~E2TyW5!9i0Q~VvGY#SNAvd6S~^?6yIVxiGFB02{z9oh8@JRNp8&IcQK12n!3HY z9cV^Ir&z0LB-$F$&@J7<18{z`oguY!JH?*xDe`uX^>iz@_&^_zMQ!|*{phRp8yP>a zFlk(~PyQA-gE!yBTH}@Bkt6Ox8e&K5(KhvCR`Z&OT0H};`;vJL^bADQ!^jRb6LsMC z;ZYhJa2;h4`$}Cf;bM2-cdVJPvw#I{XUPKP0@VYy1=Pd|Al6Zow*-XFRq8UVNzj_Y zPoicbCkdJfJY?VzzX4&QH@wE8-o&(P!+uwtGT&-luiX#x`5#tlEu;4=dYV#}D3eun|RGU0hB} zF>cwzm{)pymOo5t#A5SCtVfMLKe zU>GnA9EX8933mAV{~e@qQ~v)y&La%gFkl!s;S5;mQn^%wk<)%=^S5>f^#v*y)*DGF zAZS>H`^D#Y-0Pqf1F zbdA#=s7mKAXoI(}q}w*89irm*aUh$eQ4li_C$2zoby{LL zamTn;f z47h2&%ohkrb89a+&b1!8g0iq(6l`74NvRmNoQiLtNigoY0}P5$5F-%#5lA#xV+{N% F17FBEYLEZ` literal 0 HcmV?d00001 diff --git a/algorithms/sorting/CycleSort.m b/algorithms/sorting/CycleSort.m new file mode 100644 index 0000000..55c771d --- /dev/null +++ b/algorithms/sorting/CycleSort.m @@ -0,0 +1,45 @@ +function cycleSort(arr) + n = length(arr); + + for cycleStart = 1:n-1 + item = arr(cycleStart); + pos = cycleStart; + + % Find the position to place the current item + for i = cycleStart+1:n + if arr(i) < item + pos = pos + 1; + end + end + + % Skip if the item is already in its correct position + if pos == cycleStart + continue; + end + + % Place the item in its correct position + while item == arr(pos) + pos = pos + 1; + end + temp = arr(pos); + arr(pos) = item; + item = temp; + + % Rotate the rest of the cycle + while pos ~= cycleStart + pos = cycleStart; + for i = cycleStart+1:n + if arr(i) < item + pos = pos + 1; + end + end + + while item == arr(pos) + pos = pos + 1; + end + temp = arr(pos); + arr(pos) = item; + item = temp; + end + end +end