AntInLoop

Bubble Sort Algorithm Code Implementations

Complete Bubble Sort Algorithm code implementations in C++, Python, Java, and JavaScript with time and space complexity analysis on AntInLoop.

Read Bubble Sort Algorithm Guide ยท Launch Interactive Visualizer

C++ Implementation

// Bubble Sort Implementation in C++
#include <iostream>
#include <vector>
#include <algorithm>

class BubbleSort {
public:
    static std::vector<int> bubbleSort(const std::vector<int>& arr) {
        int n = arr.size();
        std::vector<int> sortedArray = arr;
        
        for (int i = 0; i < n - 1; i++) {
            bool swapped = false;
            
            for (int j = 0; j < n - i - 1; j++) {
                if (sortedArray[j] > sortedArray[j + 1]) {
                    std::swap(sortedArray[j], sortedArray[j + 1]);
                    swapped = true;
                }
            }
            
            if (!swapped) break;
        }
        
        return sortedArray;
    }
};

CPP Implementation

#include <iostream>
#include <vector>
using namespace std;

void bubbleSort(vector<int>& arr) {
    int n = arr.size();
    for (int i = 0; i < n - 1; ++i) {
        bool swapped = false;
        for (int j = 0; j < n - i - 1; ++j) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
                swapped = true;
            }
        }
        if (!swapped) break;
    }
}

int main() {
    vector<int> data = {5, 2, 8, 3, 1};
    bubbleSort(data);
    for (int x : data) cout << x << " ";
    return 0;
}

JAVA Implementation

// Bubble Sort Implementation in Java
public class BubbleSort {
    public static int[] bubbleSort(int[] arr) {
        int n = arr.length;
        int[] sortedArray = arr.clone();
        
        for (int i = 0; i < n - 1; i++) {
            boolean swapped = false;
            
            for (int j = 0; j < n - i - 1; j++) {
                if (sortedArray[j] > sortedArray[j + 1]) {
                    swap(sortedArray, j, j + 1);
                    swapped = true;
                }
            }
            
            if (!swapped) break;
        }
        
        return sortedArray;
    }
    
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

JAVASCRIPT Implementation

// Bubble Sort Implementation in JavaScript
function bubbleSort(arr) {
  const n = arr.length;
  const sortedArray = [...arr]; // Create a copy to avoid mutating original
  
  // Traverse through all array elements
  for (let i = 0; i < n - 1; i++) {
    let swapped = false; // Flag to optimize algorithm
    
    // Last i elements are already in place
    for (let j = 0; j < n - i - 1; j++) {
      // Swap if the element found is greater than the next element
      if (sortedArray[j] > sortedArray[j + 1]) {
        [sortedArray[j], sortedArray[j + 1]] = [sortedArray[j + 1], sortedArray[j]];
        swapped = true;
      }
    }
    
    // If no two elements were swapped, array is sorted
    if (!swapped) break;
  }
  
  return sortedArray;
}

PYTHON Implementation

# Bubble Sort Implementation in Python
def bubble_sort(arr):
    n = len(arr)
    sorted_array = arr.copy()
    
    for i in range(n - 1):
        swapped = False
        
        for j in range(n - i - 1):
            if sorted_array[j] > sorted_array[j + 1]:
                sorted_array[j], sorted_array[j + 1] = sorted_array[j + 1], sorted_array[j]
                swapped = True
        
        if not swapped:
            break
    
    return sorted_array