<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Youri01 on Medium]]></title>
        <description><![CDATA[Stories by Youri01 on Medium]]></description>
        <link>https://medium.com/@hamzaofficer2?source=rss-82b2ea9a73d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bvfysu70dBPZt4g3CqMG8w@2x.jpeg</url>
            <title>Stories by Youri01 on Medium</title>
            <link>https://medium.com/@hamzaofficer2?source=rss-82b2ea9a73d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:48:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hamzaofficer2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The 3 Algorithms of the push_swap Project (42 / 1337)]]></title>
            <link>https://medium.com/@hamzaofficer2/the-3-algorithms-of-the-push-swap-project-42-1337-8d56781d4f79?source=rss-82b2ea9a73d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d56781d4f79</guid>
            <category><![CDATA[optimization-algorithms]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[42-network]]></category>
            <dc:creator><![CDATA[Youri01]]></dc:creator>
            <pubDate>Wed, 02 Dec 2026 15:02:15 GMT</pubDate>
            <atom:updated>2026-12-02T15:02:15.398Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>SIMPLE STRATEGY — YOUR OPTIMIZED SELECTION SORT (O(n²)).</li></ol><p>→When I built the simple algorithm, I started from a classic selection sort mindset “keep pulling the smallest remaining number out of Stack A and sending it to Stack B until everything is in order”. Because pushing onto Stack B stacks new items on top the smallest values get pushed first and end up deeper in B while larger ones sit higher up once I push everything back onto Stack A that order flips right back into ascending order with the smallest element sitting on top.<br> <br>Instead of leaving it as a naive, brute-force loop, I tuned how Stack A moves on every single step:<br> <br>- Shortest path rotation →Before rotating Stack A i check where the minimum value sits relative to the middle of the stack. If it is in the top half, rotating forward (ra) gets it there faster if it is in the bottom half reverse-rotating (rra) saves moves.<br> <br>- My “sa” shortcut → Whenever the second element from the top is already the minimum and sits above the current top element doing a single swap (sa) brings the minimum right to the top in one move instead of burning extra rotations and fixing things later. It also helps clean up local order along the way.<br> <br>- Early exit when A becomes sorted “I don’t blindly grind through every element if the remaining part of Stack A is already sorted” As soon as Stack A is sorted i stop extracting minimums and push whatever is waiting in Stack B back over. That is why this approach feels so sharp on low-disorder inputs (under 0.2 disorder) it finishes early instead of doing unnecessary work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cQjMV_kYCKMRGBHOeJFo9Q.png" /></figure><p>2. MEDIUM STRATEGY — SLIDING-WINDOW CHUNKS (n√n).</p><p>→ For medium, moving one minimum at a time gets too expensive as the stack grows, so the idea shifts to working in chunks sized around the square root of n. First every value gets mapped to its rank from 0 to n — 1 so negative numbers huge gaps or messy values don’t complicate the math.<br> <br>The algorithm runs in two main phases:<br> <br>- Phase 1 (Sliding window from A to B): i picture a window of width roughly sqrt(n) over the ranks and slide that window forward as elements get pushed to Stack B. If the top element of Stack A falls inside the active window, I push it to Stack B if its rank is even below the current window start (meaning it’s one of the smaller elements in that wave) i rotate Stack B (rb) to push it toward the bottom anything outside the window simply gets rotated past (ra) in Stack A.<br> <br>- Phase 2 (Pulling back largest-first to A) → Because Phase 1 leaves Stack B roughly organized with smaller ranks lower and larger ranks closer to the top or reachable within about sqrt(n) moves Phase 2 simply finds the highest remaining rank in Stack B, rotates Stack B using whichever direction (rb or rrb) is shorter, and pushes it back to Stack A (pa). Since the largest values go back to Stack A first, they sink below smaller values pushed later, leaving Stack A sorted ascending.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pYEM8ZVA085PmdZdl88D6w.png" /></figure><p>3. COMPLEX STRATEGY — BITWISE RADIX SORT ON RANKS (O(n log n)).</p><p>→ When disorder is high (0.5 or above) or you want predictable scaling that doesn’t depend on how scrambled the input looks, complex uses LSD (Least Significant Digit) radix sort in base 2 over the normalized ranks (0 to n — 1).<br> <br>Instead of comparing values directly or hunting for minimums and maximums, it inspects one binary bit at a time, starting from the least significant bit (bit 0) up to the highest bit needed for the largest rank.<br> <br>- Bit by bit pass: For a given bit position, the algorithm steps through the current elements of Stack A. If that bit is 0, the number is pushed to Stack B (pb) if that bit is 1 it stays in Stack A and rotates to the back (ra).<br> <br>- Preserving relative order: After scanning the pass, everything sent to Stack B is pushed back onto Stack A (pa). Pushing onto a stack reverses order once, and pushing back onto Stack A reverses it a second time so the 0-bit group keeps its relative order and lands ahead of the 1-bit group.<br> <br>- Skipping wasted work: If all checked elements share the same bit value on a pass or if Stack A becomes sorted early, it skips unnecessary moves rather than wasting operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rfw-GqDMNlMgt_jWDje_Ow.png" /></figure><p>4. COMPARISON / BENCHMARK.</p><p>→ Implementing the --bench flag, it turned out to be the most useful tool for analyzing how each sorting strategy actually behaves under the hood. Because the subject requires benchmark metrics to be printed to <strong>stderr</strong> while keeping <strong>stdout</strong> strictly dedicated to the sorting instructions, I could pipe the output straight into ./checker to verify correctness while reading the exact performance breakdown at the same time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*63G9-GGpMlQONeAijpoyzg.png" /></figure><p>—&gt; I hope this project and its documentation will be helpful to everyone i also hope it provides enough detail to guide students through the different concepts and challenges of Push_swap.</p><p>Good luck to all students, and keep learning!</p><p>With respect, (Yuuri01)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d56781d4f79" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>