π 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:
Bubble Sort
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
Post a Comment