DEV Community

Christina Sharon S
Christina Sharon S

Posted on • Edited on

Bubble Sort and Selection Sort : A Beginner-Friendly Guide

Introduction

Sorting is one of the most fundamental concepts in programming. Among the simplest sorting algorithms are Bubble Sort and Selection Sort.

They are easy to understand and perfect for beginners learning how sorting works.

Bubble Sort

What is Bubble Sort?

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

With each pass, the largest element “bubbles up” to the end of the array.


Example

Input:

arr = [5, 3, 8, 4, 2]
Enter fullscreen mode Exit fullscreen mode

After sorting:

[2, 3, 4, 5, 8]
Enter fullscreen mode Exit fullscreen mode

How It Works

  • Compare adjacent elements
  • Swap if left > right
  • Repeat for entire array
  • After each pass, largest element is placed correctly

Step-by-Step

Pass 1:

  • [5, 3] = swap = [3, 5, 8, 4, 2]
  • [5, 8] = no swap
  • [8, 4] = swap
  • [8, 2] = swap

Result:

[3, 5, 4, 2, 8]
Enter fullscreen mode Exit fullscreen mode

Python Implementation

def bubble_sort(arr):
    n = len(arr)

    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

    return arr


# Example usage
print(bubble_sort([5, 3, 8, 4, 2]))
Enter fullscreen mode Exit fullscreen mode

Time Complexity

  • Worst Case: O(n²)
  • Best Case: O(n) (optimized version)

Key Points

  • Simple to understand
  • Not efficient for large data
  • Useful for learning basics

Selection Sort

What is Selection Sort?

Selection Sort repeatedly selects the smallest element from the unsorted portion and places it at the correct position.

Example

Input:

arr = [5, 3, 8, 4, 2]
Enter fullscreen mode Exit fullscreen mode

Output:

[2, 3, 4, 5, 8]
Enter fullscreen mode Exit fullscreen mode

How It Works

  • Find the minimum element
  • Swap it with the first unsorted element
  • Move boundary forward
  • Repeat

Step-by-Step

Initial:

[5, 3, 8, 4, 2]
Enter fullscreen mode Exit fullscreen mode
  • Find min which is 2 then swap with 5
[2, 3, 8, 4, 5]
Enter fullscreen mode Exit fullscreen mode
  • Next min = 3 (already correct)
  • Next min = 4 then swap with 8

Final:

[2, 3, 4, 5, 8]
Enter fullscreen mode Exit fullscreen mode

Python Implementation

def selection_sort(arr):
    n = len(arr)

    for i in range(n):
        min_index = i

        for j in range(i + 1, n):
            if arr[j] < arr[min_index]:
                min_index = j

        arr[i], arr[min_index] = arr[min_index], arr[i]

    return arr


# Example usage
print(selection_sort([5, 3, 8, 4, 2]))
Enter fullscreen mode Exit fullscreen mode

Key Points

  • Fewer swaps than Bubble Sort
  • Still inefficient for large data
  • Easy to implement

Conclusion

Bubble Sort and Selection Sort are not the fastest algorithms, but they are extremely important for understanding how sorting works.

Top comments (0)