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.
What is the Print Preview Shortcut Key?
Learn the keyboard shortcut key for Print Preview in Windows, Microsoft Word, Excel, and Web browsers like Chrome.
Find and Replace Shortcut Key (Windows & Mac)
Learn the keyboard shortcut for Find and Replace in MS Word, Excel, and Notepad. Use Ctrl+H for Windows and Cmd+Shift+H for Mac.
What is the 'static' Keyword in Java?
Learn about the static keyword in Java. Understand how static variables, methods, and blocks are used for memory management at the class level.
Basic Structure of a C Program
Learn the basic structure of a C program. Understand the use of preprocessor directives (#include), the main() function, and curly braces { }.
Difference Between System Software and Application Software
Learn the exact difference between System Software and Application Software. Understand how Operating Systems run the hardware, while Apps help the user.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast — free, no signup required.