πŸš€ Sorting Algorithms in DSA – Bubble Sort & Selection Sort


πŸ’‘ Did You Know?

Imagine arranging student marks from lowest to highest…

πŸ‘‰ Doing it manually takes time ❌
πŸ‘‰ Sorting algorithms do it efficiently ✅

That’s why Sorting Algorithms are important in DSA πŸš€


πŸ”° Introduction

Sorting means arranging data in a specific order.

πŸ‘‰ Data can be arranged:

  • Ascending order (small → large)

  • Descending order (large → small)

Sorting helps:

  • Faster searching

  • Better data management

  • Efficient problem solving


πŸ”₯ Types of Sorting Algorithms

In this guide, you will learn:

  1. Bubble Sort

  2. Selection Sort


πŸ”Ή 1. Bubble Sort

πŸ‘‰ Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order.


πŸ–Ό️ Visualization




πŸ’» Bubble Sort Program in C

#include <stdio.h>   // Header file for input/output functions like printf()

int main() {         // Main function starts execution of program

    int arr[5] = {50, 20, 40, 10, 30}; // Array with unsorted elements
    int temp;                          // Variable used for swapping values

    for(int i = 0; i < 5; i++) {       // Outer loop for number of passes

        for(int j = 0; j < 4 - i; j++) { // Inner loop for comparisons

            if(arr[j] > arr[j+1]) {    // Check if current element is greater than next

                temp = arr[j];         // Store current element in temp
                arr[j] = arr[j+1];     // Replace current with next element
                arr[j+1] = temp;       // Place stored value in next position
            }
        }
    }

    printf("Sorted Array:\n");         // Print heading

    for(int i = 0; i < 5; i++) {       // Loop through sorted array
        printf("%d ", arr[i]);         // Print each element
    }

    return 0;                          // End program successfully
}

πŸ“€ Output

10 20 30 40 50

⚡ Time Complexity

πŸ‘‰ Best Case: O(n)
πŸ‘‰ Worst Case: O(n²)


πŸ’‘ Real-Life Example

πŸ‘‰ Arranging exam marks from lowest to highest.


πŸ”Ή 2. Selection Sort

πŸ‘‰ Selection Sort finds the smallest element and places it at the correct position.


πŸ–Ό️ Visualization




πŸ’» Selection Sort Program in C

#include <stdio.h>   // Header file for input/output functions

int main() {         // Main function starts execution

    int arr[5] = {64, 25, 12, 22, 11}; // Array with unsorted elements
    int minIndex, temp;                // Variables for minimum index and swapping

    for(int i = 0; i < 4; i++) {       // Outer loop for selection process

        minIndex = i;                  // Assume current element is minimum

        for(int j = i + 1; j < 5; j++) { // Inner loop to find smallest element

            if(arr[j] < arr[minIndex]) { // Check if smaller element exists
                minIndex = j;            // Update minimum index
            }
        }

        temp = arr[i];                 // Store current element in temp
        arr[i] = arr[minIndex];        // Replace current with minimum element
        arr[minIndex] = temp;          // Put stored value at minimum position
    }

    printf("Sorted Array:\n");         // Display heading

    for(int i = 0; i < 5; i++) {       // Loop through sorted array
        printf("%d ", arr[i]);         // Print each element
    }

    return 0;                          // End program successfully
}

πŸ“€ Output

11 12 22 25 64

⚡ Time Complexity

πŸ‘‰ Best Case: O(n²)
πŸ‘‰ Worst Case: O(n²)





⚠️ Common Mistakes

❌ Wrong loop conditions
❌ Forgetting swapping logic
❌ Confusing ascending and descending order


πŸ’‘ Pro Tip

πŸ‘‰ Bubble Sort is easier for beginners.

πŸ‘‰ Selection Sort performs fewer swaps and is slightly more efficient.


πŸ§ͺ Practice Questions (with Answers)

1. What is sorting?

πŸ‘‰ Answer:
Sorting means arranging data in a specific order.


2. Which sorting algorithm repeatedly swaps adjacent elements?

πŸ‘‰ Answer:
Bubble Sort.


3. Which sorting algorithm selects minimum element?

πŸ‘‰ Answer:
Selection Sort.


4. What is the worst-case complexity of Bubble Sort?

πŸ‘‰ Answer:
O(n²)


πŸ’Ž Final Words

Sorting algorithms are the foundation of efficient programming and problem solving.

πŸ‘‰ Once you master sorting:

  • Searching becomes faster

  • Logic improves

  • Advanced DSA becomes easier

πŸš€ Keep practicing and keep growing!


πŸ“’ Support & Share

πŸ’¬ If you found this helpful, share it with your friends!

πŸš€ Follow Learning Growth Hub for more powerful DSA tutorials.


πŸ“Œ Keep Learning. Keep Coding. Keep Growing.

Written by Krishna Popat
🌱 Founder – Learning Growth Hub


Comments

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”