<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged javaprogramming</title>
<link>https://coderlegion.com/tag/javaprogramming</link>
<description>Powered by Question2Answer</description>
<item>
<title>My Journey as a Java Developer: Learning, Building, and Solving Problems Through Code</title>
<link>https://coderlegion.com/30610/my-journey-as-a-java-developer-learning-building-and-solving-problems-through-code</link>
<description>&lt;p&gt;Software development is more than writing code. It is about understanding problems, finding practical solutions, and building applications that make people's work easier. As a Java Developer, I enjoy exploring how software works behind the scenes, improving my programming skills, and turning ideas into useful applications.&lt;/p&gt;
&lt;p&gt;My journey in development is driven by curiosity, continuous learning, and the desire to become better at what I do every day.&lt;/p&gt;
&lt;h3 id=&quot;why-i-chose-java-development&quot;&gt;Why I Chose Java Development&lt;/h3&gt;
&lt;p&gt;Java is one of the technologies that interests me because of its versatility, object-oriented approach, and ability to support reliable and scalable applications. From backend systems to enterprise-level software, Java provides a strong foundation for building applications that can grow and adapt to different requirements.&lt;/p&gt;
&lt;p&gt;What I enjoy most is the process of understanding how a problem works and figuring out how to solve it through code. Sometimes the solution is simple, while other times it requires breaking the problem into smaller parts, testing different approaches, and learning from mistakes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;For me, programming is not just about making something work. It is about understanding why it works and finding ways to make it better.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;my-approach-to-writing-code&quot;&gt;My Approach to Writing Code&lt;/h3&gt;
&lt;p&gt;I believe that good software starts with clear thinking and well structured code. Writing code that works is important, but writing code that other developers can understand, maintain, and improve is equally valuable.&lt;/p&gt;
&lt;p&gt;As I continue developing my skills, I focus on several important areas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Java programming:&lt;/strong&gt; Understanding core concepts and applying them to practical problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming:&lt;/strong&gt; Using classes, objects, encapsulation, inheritance, and polymorphism to organize code effectively.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Backend development:&lt;/strong&gt; Learning how application logic, databases, and APIs work together to deliver useful functionality.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem-solving:&lt;/strong&gt; Breaking complex challenges into manageable tasks and exploring efficient solutions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clean code:&lt;/strong&gt; Writing readable, maintainable code that makes future improvements easier.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These areas help me build a stronger foundation and approach development tasks with more confidence.&lt;/p&gt;
&lt;h3 id=&quot;learning-beyond-the-basics&quot;&gt;Learning Beyond the Basics&lt;/h3&gt;
&lt;p&gt;Technology continues to evolve, and I believe developers should remain curious and open to learning. There is always something new to explore, whether it is a better programming technique, a different architectural approach, or a tool that improves the development process.&lt;/p&gt;
&lt;p&gt;I enjoy experimenting with code, exploring new concepts, and learning from both successful projects and mistakes. Debugging is also an important part of this process. When something does not work as expected, I try to understand the root cause instead of simply looking for a quick fix.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Every bug can teach you something, and every project gives you an opportunity to improve.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I also recognize the importance of understanding the fundamentals before depending too heavily on frameworks or tools. Strong fundamentals make it easier to learn new technologies and adapt to different project requirements.&lt;/p&gt;
&lt;h3 id=&quot;building-reliable-and-scalable-applications&quot;&gt;Building Reliable and Scalable Applications&lt;/h3&gt;
&lt;p&gt;One of my main interests is developing applications that are reliable, efficient, and easy to maintain. A good application should solve the immediate problem while also being flexible enough to support future changes.&lt;/p&gt;
&lt;p&gt;This requires more than writing individual methods or classes. It involves thinking about application structure, data handling, error management, performance, and how different components communicate with each other.&lt;/p&gt;
&lt;p&gt;As a Java Developer, I want to keep strengthening my understanding of backend architecture, database integration, REST APIs, testing, and software design principles. These skills are valuable for building applications that can handle real-world requirements.&lt;/p&gt;
&lt;p&gt;I believe the best way to improve is to put knowledge into practice. Reading documentation and studying examples are useful, but building projects helps turn theoretical concepts into practical experience.&lt;/p&gt;
&lt;h3 id=&quot;challenges-are-part-of-the-process&quot;&gt;Challenges Are Part of the Process&lt;/h3&gt;
&lt;p&gt;Every developer faces challenges. Sometimes a problem seems straightforward until you start implementing the solution. Other times, an unexpected error forces you to rethink your approach.&lt;/p&gt;
&lt;p&gt;I see these situations as opportunities to develop better problem-solving habits. Instead of giving up when something becomes difficult, I try to investigate the issue, test possible solutions, and understand what went wrong.&lt;/p&gt;
&lt;p&gt;This mindset helps me become more patient and systematic when approaching technical problems.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Becoming a better developer is not about knowing everything. It is about being willing to learn, adapt, and keep improving.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;growing-with-the-developer-community&quot;&gt;Growing With the Developer Community&lt;/h3&gt;
&lt;p&gt;Software development is a continuous learning journey, and the developer community plays an important role in that process. Developers learn from sharing experiences, discussing different approaches, and helping one another overcome challenges.&lt;/p&gt;
&lt;p&gt;I enjoy connecting with people who are passionate about technology, exploring their ideas, and learning how they approach software engineering problems.&lt;/p&gt;
&lt;p&gt;I also believe that sharing knowledge is a useful way to reinforce what we learn. Explaining a concept, documenting a solution, or discussing a coding challenge can help both the writer and the reader gain a better understanding.&lt;/p&gt;
&lt;p&gt;Through platforms like CoderLegion, I hope to share my experiences, exchange ideas, and connect with other developers who enjoy building useful things with technology.&lt;/p&gt;
&lt;h3 id=&quot;my-goals-as-a-java-developer&quot;&gt;My Goals as a Java Developer&lt;/h3&gt;
&lt;p&gt;My goal is to continue growing as a software developer by strengthening my Java knowledge, improving my problem-solving abilities, and gaining more practical experience in building real-world applications.&lt;/p&gt;
&lt;p&gt;I want to explore better software design practices, understand scalable backend systems, and develop solutions that are useful, reliable, and maintainable.&lt;/p&gt;
&lt;p&gt;I know that becoming a skilled developer takes time, practice, and consistency. There will always be new technologies to learn and new challenges to solve, and that is part of what makes this field interesting to me.&lt;/p&gt;
&lt;h3 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;/h3&gt;
&lt;p&gt;My journey as a Java Developer is built around learning, experimenting, solving problems, and creating better software. I enjoy the challenge of turning ideas into working applications and discovering new ways to write efficient, maintainable code.&lt;/p&gt;
&lt;p&gt;I do not believe growth happens overnight. It comes from consistent effort, practical experience, curiosity, and the willingness to learn from mistakes.&lt;/p&gt;
&lt;p&gt;I look forward to continuing this journey, building meaningful projects, sharing what I learn, and connecting with developers who share the same passion for technology.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Always learning, always building, and always looking for a better way to solve problems.&lt;/strong&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/30610/my-journey-as-a-java-developer-learning-building-and-solving-problems-through-code</guid>
<pubDate>Sat, 10 Oct 2026 14:52:56 +0000</pubDate>
</item>
<item>
<title>What if your wardrobe could teach you Sorting Algorithms?👗</title>
<link>https://coderlegion.com/28251/what-if-your-wardrobe-could-teach-you-sorting-algorithms</link>
<description>&lt;p&gt;You open your wardrobe.&lt;/p&gt;
&lt;p&gt;T-shirts mixed with jeans.&lt;br&gt;
Dresses hiding behind hoodies.&lt;br&gt;
Everything is everywhere. 😭&lt;/p&gt;
&lt;p&gt;You need to sort it.&lt;/p&gt;
&lt;p&gt;But here's the catch...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;There isn't just one way to organize a messy wardrobe.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;And that's exactly what happens in DSA.&lt;/p&gt;
&lt;p&gt;An unsorted array is basically a messy wardrobe.&lt;br&gt;
The goal is the same: &lt;strong&gt;put everything in order.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;But every sorting algorithm has its own strategy.&lt;/p&gt;
&lt;p&gt;Let's meet them. 👀&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;1-bubble-sort-compare-the-neighbors&quot;&gt;1. Bubble Sort — Compare the neighbors&lt;/h3&gt;
&lt;p&gt;Imagine checking your clothes &lt;strong&gt;two at a time&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If the two neighboring clothes are in the wrong order:&lt;/p&gt;
&lt;p&gt;🔄 Swap them.&lt;/p&gt;
&lt;p&gt;Then move to the next pair.&lt;/p&gt;
&lt;p&gt;Keep doing this until the largest item gradually reaches the end.&lt;/p&gt;
&lt;p&gt;That's why it's called &lt;strong&gt;Bubble Sort&lt;/strong&gt;—elements &quot;bubble&quot; toward their correct positions.&lt;/p&gt;
&lt;h4 id=&quot;-java&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class BubbleSort {

    public static void main(String[] args) {

        int[] arr = {5, 3, 8, 4, 2};

        for (int i = 0; i &amp;lt; arr.length - 1; i++) {

            for (int j = 0; j &amp;lt; arr.length - i - 1; j++) {

                if (arr[j] &amp;gt; arr[j + 1]) {

                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time:&lt;/strong&gt; O(n²)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(1)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Bubble = Compare neighbors + Swap&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-2-selection-sort-find-the-right-one&quot;&gt;🎯 2. Selection Sort — Find the right one&lt;/h3&gt;
&lt;p&gt;Now you change your strategy.&lt;/p&gt;
&lt;p&gt;Instead of repeatedly comparing neighbors, you look through the entire unsorted section.&lt;/p&gt;
&lt;p&gt;You find the &lt;strong&gt;smallest item&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Then...&lt;/p&gt;
&lt;p&gt;📍 Put it in its correct position.&lt;/p&gt;
&lt;p&gt;Repeat.&lt;/p&gt;
&lt;p&gt;It's like saying:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&quot;First, I'll find the smallest shirt. Then the next smallest. Then the next...&quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Selection Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-1&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class SelectionSort {

    public static void main(String[] args) {

        int[] arr = {64, 25, 12, 22, 11};

        for (int i = 0; i &amp;lt; arr.length - 1; i++) {

            int minIndex = i;

            for (int j = i + 1; j &amp;lt; arr.length; j++) {

                if (arr[j] &amp;lt; arr[minIndex]) {
                    minIndex = j;
                }
            }

            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time:&lt;/strong&gt; O(n²)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(1)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Selection = Find minimum + Place it&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-3-insertion-sort-insert-it-where-it-belongs&quot;&gt;🃏 3. Insertion Sort — Insert it where it belongs&lt;/h3&gt;
&lt;p&gt;Now imagine arranging clothes one by one.&lt;/p&gt;
&lt;p&gt;You already have a few clothes arranged.&lt;/p&gt;
&lt;p&gt;You pick the next one.&lt;/p&gt;
&lt;p&gt;Look at the sorted section.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&quot;Where does this belong?&quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You make space and insert it there.&lt;/p&gt;
&lt;p&gt;Just like arranging playing cards in your hand. 🃏&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Insertion Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-2&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class InsertionSort {

    public static void main(String[] args) {

        int[] arr = {5, 3, 4, 1, 2};

        for (int i = 1; i &amp;lt; arr.length; i++) {

            int key = arr[i];
            int j = i - 1;

            while (j &amp;gt;= 0 &amp;amp;&amp;amp; arr[j] &amp;gt; key) {

                arr[j + 1] = arr[j];
                j--;
            }

            arr[j + 1] = key;
        }

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Best:&lt;/strong&gt; O(n)&lt;br&gt;
&lt;strong&gt;Worst:&lt;/strong&gt; O(n²)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(1)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Insertion = Take + Shift + Insert&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-4-merge-sort-don-t-organize-everything-at-once&quot;&gt;✂️ 4. Merge Sort — Don't organize everything at once&lt;/h2&gt;
&lt;p&gt;You look at the wardrobe.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&quot;There are WAY too many clothes. I'm splitting this.&quot;&lt;/em&gt; 😭&lt;/p&gt;
&lt;p&gt;Divide the clothes into two sections.&lt;/p&gt;
&lt;p&gt;Then divide those sections again.&lt;/p&gt;
&lt;p&gt;Keep dividing until each section is tiny.&lt;/p&gt;
&lt;p&gt;Now sort them.&lt;/p&gt;
&lt;p&gt;Then merge the sorted sections back together.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Divide → Sort → Merge&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Merge Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-3&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class MergeSort {

    static void mergeSort(int[] arr, int left, int right) {

        if (left &amp;lt; right) {

            int mid = left + (right - left) / 2;

            mergeSort(arr, left, mid);
            mergeSort(arr, mid + 1, right);

            merge(arr, left, mid, right);
        }
    }

    static void merge(int[] arr, int left, int mid, int right) {

        int[] temp = new int[right - left + 1];

        int i = left;
        int j = mid + 1;
        int k = 0;

        while (i &amp;lt;= mid &amp;amp;&amp;amp; j &amp;lt;= right) {

            if (arr[i] &amp;lt;= arr[j]) {
                temp[k++] = arr[i++];
            } else {
                temp[k++] = arr[j++];
            }
        }

        while (i &amp;lt;= mid) {
            temp[k++] = arr[i++];
        }

        while (j &amp;lt;= right) {
            temp[k++] = arr[j++];
        }

        for (int x = 0; x &amp;lt; temp.length; x++) {
            arr[left + x] = temp[x];
        }
    }

    public static void main(String[] args) {

        int[] arr = {8, 3, 5, 4, 7, 6, 1, 2};

        mergeSort(arr, 0, arr.length - 1);

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time:&lt;/strong&gt; O(n log n)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(n)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Merge = Divide + Sort + Merge&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-5-quick-sort-choose-a-pivot&quot;&gt;⚡ 5. Quick Sort — Choose a pivot&lt;/h2&gt;
&lt;p&gt;Now you want a faster strategy.&lt;/p&gt;
&lt;p&gt;You pick one item as your &lt;strong&gt;pivot&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Everything smaller goes to one side.&lt;/p&gt;
&lt;p&gt;Everything larger goes to the other.&lt;/p&gt;
&lt;p&gt;Now you've created smaller problems.&lt;/p&gt;
&lt;p&gt;Repeat the same process.&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Quick Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-4&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class QuickSort {

    static void quickSort(int[] arr, int low, int high) {

        if (low &amp;lt; high) {

            int pivotIndex = partition(arr, low, high);

            quickSort(arr, low, pivotIndex - 1);
            quickSort(arr, pivotIndex + 1, high);
        }
    }

    static int partition(int[] arr, int low, int high) {

        int pivot = arr[high];

        int i = low - 1;

        for (int j = low; j &amp;lt; high; j++) {

            if (arr[j] &amp;lt; pivot) {

                i++;

                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        int temp = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = temp;

        return i + 1;
    }

    public static void main(String[] args) {

        int[] arr = {10, 7, 8, 9, 1, 5};

        quickSort(arr, 0, arr.length - 1);

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Average:&lt;/strong&gt; O(n log n)&lt;br&gt;
&lt;strong&gt;Worst:&lt;/strong&gt; O(n²)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(log n) average recursion stack&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Quick = Pivot + Partition&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-6-heap-sort-priority-comes-first&quot;&gt;🌳 6. Heap Sort — Priority comes first&lt;/h2&gt;
&lt;p&gt;Now imagine you want the &lt;strong&gt;highest-priority clothing item&lt;/strong&gt; to always be easy to find.&lt;/p&gt;
&lt;p&gt;You organize the items using a special structure:&lt;/p&gt;
&lt;p&gt;🌳 &lt;strong&gt;Heap&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The largest element can stay at the top of a &lt;strong&gt;Max Heap&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Take it.&lt;/p&gt;
&lt;p&gt;Put it in its final position.&lt;/p&gt;
&lt;p&gt;Restore the heap.&lt;/p&gt;
&lt;p&gt;Repeat.&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Heap Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-5&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class HeapSort {

    static void heapSort(int[] arr) {

        int n = arr.length;

        // Build Max Heap
        for (int i = n / 2 - 1; i &amp;gt;= 0; i--) {
            heapify(arr, n, i);
        }

        // Move maximum to the end
        for (int i = n - 1; i &amp;gt; 0; i--) {

            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            heapify(arr, i, 0);
        }
    }

    static void heapify(int[] arr, int n, int i) {

        int largest = i;

        int left = 2 * i + 1;
        int right = 2 * i + 2;

        if (left &amp;lt; n &amp;amp;&amp;amp; arr[left] &amp;gt; arr[largest]) {
            largest = left;
        }

        if (right &amp;lt; n &amp;amp;&amp;amp; arr[right] &amp;gt; arr[largest]) {
            largest = right;
        }

        if (largest != i) {

            int temp = arr[i];
            arr[i] = arr[largest];
            arr[largest] = temp;

            heapify(arr, n, largest);
        }
    }

    public static void main(String[] args) {

        int[] arr = {12, 11, 13, 5, 6, 7};

        heapSort(arr);

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time:&lt;/strong&gt; O(n log n)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(1)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Heap = Build heap + Extract repeatedly&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-7-counting-sort-stop-comparing-start-counting-&quot;&gt;🔢 7. Counting Sort — Stop comparing. Start counting.&lt;/h2&gt;
&lt;p&gt;Here's where things get interesting.&lt;/p&gt;
&lt;p&gt;Imagine your clothes can only have sizes:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1, 2, 3, 4, 5&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Instead of comparing every item...&lt;/p&gt;
&lt;p&gt;You simply count how many times each size appears.&lt;/p&gt;
&lt;p&gt;That's the idea behind &lt;strong&gt;Counting Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-6&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class CountingSort {

    static void countingSort(int[] arr) {

        int max = arr[0];

        for (int num : arr) {
            if (num &amp;gt; max) {
                max = num;
            }
        }

        int[] count = new int[max + 1];

        for (int num : arr) {
            count[num]++;
        }

        int index = 0;

        for (int i = 0; i &amp;lt; count.length; i++) {

            while (count[i] &amp;gt; 0) {

                arr[index] = i;
                index++;

                count[i]--;
            }
        }
    }

    public static void main(String[] args) {

        int[] arr = {4, 2, 2, 8, 3, 3, 1};

        countingSort(arr);

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time:&lt;/strong&gt; O(n + k)&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(k)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Counting = Count frequencies&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;⚠️ Works well when the range of values is reasonably small.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-8-radix-sort-one-digit-at-a-time&quot;&gt;📚 8. Radix Sort — One digit at a time&lt;/h2&gt;
&lt;p&gt;Now imagine numbers are your clothes.&lt;/p&gt;
&lt;p&gt;Instead of comparing the whole number...&lt;/p&gt;
&lt;p&gt;You organize them &lt;strong&gt;digit by digit&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;First:&lt;/p&gt;
&lt;p&gt;➡️ Ones digit&lt;/p&gt;
&lt;p&gt;Then:&lt;/p&gt;
&lt;p&gt;➡️ Tens digit&lt;/p&gt;
&lt;p&gt;Then:&lt;/p&gt;
&lt;p&gt;➡️ Hundreds digit&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;170, 45, 75, 90, 802, 24, 2, 66&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The algorithm gradually builds the final ordering.&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Radix Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-7&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class RadixSort {

    static void radixSort(int[] arr) {

        int max = arr[0];

        for (int num : arr) {
            if (num &amp;gt; max) {
                max = num;
            }
        }

        for (int exp = 1; max / exp &amp;gt; 0; exp *= 10) {

            countingSort(arr, exp);
        }
    }

    static void countingSort(int[] arr, int exp) {

        int n = arr.length;

        int[] output = new int[n];
        int[] count = new int[10];

        for (int num : arr) {

            int digit = (num / exp) % 10;

            count[digit]++;
        }

        for (int i = 1; i &amp;lt; 10; i++) {

            count[i] += count[i - 1];
        }

        for (int i = n - 1; i &amp;gt;= 0; i--) {

            int digit = (arr[i] / exp) % 10;

            output[count[digit] - 1] = arr[i];

            count[digit]--;
        }

        for (int i = 0; i &amp;lt; n; i++) {

            arr[i] = output[i];
        }
    }

    public static void main(String[] args) {

        int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};

        radixSort(arr);

        for (int num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time:&lt;/strong&gt; O(d × (n + k))&lt;br&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(n + k)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Radix = Digit by digit&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-9-bucket-sort-group-first-sort-later&quot;&gt;🪣 9. Bucket Sort — Group first, sort later&lt;/h2&gt;
&lt;p&gt;One last strategy.&lt;/p&gt;
&lt;p&gt;Instead of throwing every piece of clothing into one giant pile...&lt;/p&gt;
&lt;p&gt;You create buckets.&lt;/p&gt;
&lt;p&gt;🪣 Bucket 1 → small values&lt;br&gt;
🪣 Bucket 2 → medium values&lt;br&gt;
🪣 Bucket 3 → large values&lt;/p&gt;
&lt;p&gt;Then sort each bucket individually.&lt;/p&gt;
&lt;p&gt;Finally...&lt;/p&gt;
&lt;p&gt;Combine them.&lt;/p&gt;
&lt;p&gt;That's &lt;strong&gt;Bucket Sort.&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&quot;-java-8&quot;&gt;💻 Java&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;import java.util.*;

public class BucketSort {

    static void bucketSort(float[] arr) {

        int n = arr.length;

        List&amp;lt;Float&amp;gt;[] buckets = new ArrayList[n];

        for (int i = 0; i &amp;lt; n; i++) {
            buckets[i] = new ArrayList&amp;lt;&amp;gt;();
        }

        for (float num : arr) {

            int index = (int) (num * n);

            buckets[index].add(num);
        }

        for (List&amp;lt;Float&amp;gt; bucket : buckets) {
            Collections.sort(bucket);
        }

        int index = 0;

        for (List&amp;lt;Float&amp;gt; bucket : buckets) {

            for (float num : bucket) {

                arr[index++] = num;
            }
        }
    }

    public static void main(String[] args) {

        float[] arr = {
            0.42f, 0.32f, 0.23f,
            0.52f, 0.25f, 0.47f, 0.51f
        };

        bucketSort(arr);

        for (float num : arr) {
            System.out.print(num + &quot; &quot;);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Average:&lt;/strong&gt;O(n + k)&lt;br&gt;
&lt;strong&gt;Worst:&lt;/strong&gt; O(n²)&lt;/p&gt;
&lt;p&gt;🧠 &lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Bucket = Distribute → Sort → Combine&lt;/strong&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-so-which-algorithm-would-you-use-&quot;&gt;🧠 So... which algorithm would YOU use?&lt;/h2&gt;
&lt;p&gt;Now look back at your messy wardrobe.&lt;/p&gt;
&lt;p&gt;Same problem.&lt;/p&gt;
&lt;p&gt;Different strategies.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Algorithm    &lt;/th&gt;&lt;th&gt;Wardrobe Strategy        &lt;/th&gt;&lt;th&gt;Main Idea&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Bubble    &lt;/td&gt;&lt;td&gt;Compare neighbors        &lt;/td&gt;&lt;td&gt;Swap&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;🎯 Selection &lt;/td&gt;&lt;td&gt;Find the right item      &lt;/td&gt;&lt;td&gt;Select&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;🃏 Insertion &lt;/td&gt;&lt;td&gt;Place one item at a time &lt;/td&gt;&lt;td&gt;Insert&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;✂️ Merge     &lt;/td&gt;&lt;td&gt;Split the wardrobe       &lt;/td&gt;&lt;td&gt;Divide &amp;amp; Merge&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;⚡ Quick      &lt;/td&gt;&lt;td&gt;Pick a reference item    &lt;/td&gt;&lt;td&gt;Pivot&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;🌳 Heap      &lt;/td&gt;&lt;td&gt;Prioritize items         &lt;/td&gt;&lt;td&gt;Heap&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;🔢 Counting  &lt;/td&gt;&lt;td&gt;Count categories         &lt;/td&gt;&lt;td&gt;Frequency&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;📚 Radix     &lt;/td&gt;&lt;td&gt;Sort digit by digit      &lt;/td&gt;&lt;td&gt;Digits&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;🪣 Bucket    &lt;/td&gt;&lt;td&gt;Make groups first        &lt;/td&gt;&lt;td&gt;Distribution&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And here's the part I want you to remember:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sorting isn't just about arranging numbers.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It's about learning that &lt;strong&gt;the same problem can have completely different solutions.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;And in DSA, the real question isn't:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&quot;Can I sort this?&quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It's:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;&quot;How efficiently can I sort this?&quot;&lt;/strong&gt; 👀&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That's where &lt;strong&gt;Time Complexity&lt;/strong&gt; enters the story.&lt;/p&gt;
&lt;p&gt;Because if your wardrobe has 10 clothes, almost anything works.&lt;/p&gt;
&lt;p&gt;But what if it has &lt;strong&gt;10 lakh clothes?&lt;/strong&gt; 😭&lt;/p&gt;
&lt;p&gt;Suddenly, your choice of algorithm matters.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/28251/what-if-your-wardrobe-could-teach-you-sorting-algorithms</guid>
<pubDate>Wed, 23 Sep 2026 05:05:37 +0000</pubDate>
</item>
</channel>
</rss>