In computer science, sorting means arranging data in a specific order (like ascending from smallest to largest). Bubble Sort is the simplest, most fundamental sorting algorithm taught to beginners. It gets its name because, just like air bubbles rise to the surface of water, the largest numbers 'bubble up' to the end of the array during the sorting process.
Algorithm Concept: Repeatedly comparing and swapping adjacent elements.
Naming: The largest elements 'bubble' to the end of the list first.
Worst Case Time Complexity: O(n²).
Best Case Time Complexity: O(n) (If the array is already sorted and an optimized flag is used).
Space Complexity: O(1) (It sorts in place, requiring no extra memory).
The algorithm works by repeatedly stepping through the array and comparing adjacent pairs of elements.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
// Outer loop for the number of passes
for (i = 0; i < n - 1; i++) {
// Inner loop for comparing adjacent elements
// The largest element bubbles up to the end in each pass
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array in ascending order: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
While Bubble Sort is very easy to understand and code, it is highly inefficient for large datasets. Because it relies on nested loops (a loop inside a loop) to compare every element, its worst-case time complexity is O(n²). This means sorting 10,000 items could take 100 million operations!
Bubble sort is a simple sorting algorithm that steps through an array, compares adjacent elements, and swaps them if they are in the wrong order.
It is called bubble sort because, with each pass through the array, the largest unsorted element 'bubbles up' to its correct position at the end of the array.
No, bubble sort is one of the slowest sorting algorithms with a time complexity of O(n²), making it unsuitable for large datasets.
Evolution of Computers: The 5 Generations
Learn about the evolution of computers from massive room-sized vacuum tubes to modern AI. Explore the 5 generations of computer history.
What are Output Devices? (Definition and Examples)
Learn the definition of computer output devices. Find clear examples like monitors, printers, speakers, and projectors, explained for students.
What is a Web Browser? (With Examples)
Learn what a web browser is and see popular examples of web browsers like Google Chrome, Safari, Mozilla Firefox, and Microsoft Edge.
Fibonacci Series in C (Code and Logic)
Learn how to write a Fibonacci series program in C. Understand the logic with both iterative and recursive methods with complete code examples.
What is Firewall Authentication?
Learn what firewall authentication is in computer networks. Understand how it blocks unauthorized hackers while allowing verified users to access internal company data.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast — free, no signup required.