Bubble Sort Program with Explanation and Code

2025-11-19 bubble sort sorting algorithms simple sorting adjacent swap algorithm beginner programs array sorting python bubble sort c bubble sort javascript sorting php sorting program csharp bubble sort stable sort

Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if needed. This page provides a clear explanation, detailed steps, and full program code in Python, C, JavaScript, PHP, and C#. Perfect for beginners and interview preparation.

Program Explanation

What is Bubble Sort?

Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly scans through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the array becomes fully sorted.

Steps to Solve

  1. Start from the first element of the array.
  2. Compare each pair of adjacent elements.
  3. If an element is greater than the next one, swap them.
  4. Continue scanning until the end of the array.
  5. After each pass, the largest element moves to the end.
  6. Repeat the process for remaining elements.
  7. Stop when no swaps are needed (array is sorted).

Key Points

  • Bubble Sort is easy to implement.
  • Best-case time complexity: O(n)
  • Average/Worst-case time complexity: O(n²)
  • Stable sorting algorithm.
  • Useful for small datasets.
  • Optimized Bubble Sort stops if no swaps occur in a pass.

Program Code


arr = list(map(int, input("Enter numbers: ").split()))

for i in range(len(arr)):
    swapped = False

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

    if not swapped:
        break

print("Sorted array:", arr)
      

#include <stdio.h>

int main() {
    int n;
    printf("Enter number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter elements: ");
    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    for (int i = 0; i < n - 1; i++) {
        int swapped = 0;

        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = 1;
            }
        }

        if (!swapped)
            break;
    }

    printf("Sorted array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}
      

let arr = prompt("Enter numbers:").split(" ").map(Number);

for (let i = 0; i < arr.length; i++) {
  let swapped = false;

  for (let j = 0; j < arr.length - i - 1; j++) {
    if (arr[j] > arr[j + 1]) {
      [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      swapped = true;
    }
  }

  if (!swapped) break;
}

console.log("Sorted array:", arr);
      

using System;

class Program {
    static void Main() {
        Console.Write("Enter numbers: ");
        int[] arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);

        for (int i = 0; i < arr.Length - 1; i++) {
            bool swapped = false;

            for (int j = 0; j < arr.Length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]);
                    swapped = true;
                }
            }

            if (!swapped)
                break;
        }

        Console.Write("Sorted array: ");
        foreach (int x in arr)
            Console.Write(x + " ");
    }
}
      

<?php

$arr = array_map('intval', explode(" ", readline("Enter numbers: ")));
$n = count($arr);

for ($i = 0; $i < $n - 1; $i++) {
    $swapped = false;

    for ($j = 0; $j < $n - $i - 1; $j++) {
        if ($arr[$j] > $arr[$j + 1]) {
            $temp = $arr[$j];
            $arr[$j] = $arr[$j + 1];
            $arr[$j + 1] = $temp;
            $swapped = true;
        }
    }

    if (!$swapped)
        break;
}

echo "Sorted array: " . implode(" ", $arr);

?>
      
1